include/punk_observe/po_tenant.h

filter: Statementstmt 98.0% 51 / 52 Branchbran 84.6% 44 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.3% 95 / 104 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_maplayout.h ↑ Summary include/punk_observe/po_panel.h »
1 /* po_tenant.h - the tenant seam, kept honest.
2 *
3 * Phase 0 rooted every path at the store root and put the tenant id in every
4 * segment header, so this engine is single-tenant in POLICY and never in
5 * PATH. This file does not resolve tenants - that is the hosted product's
6 * job. It keeps the seam from rotting in the meantime.
7 *
8 * THE DEFAULT IS A CONSTANT, WHICH IS THE POINT.
9 *
10 * The self-hosted shape is the whole shape: one tenant, named by
11 * configuration, resolved by returning it. The hosted shape is the same code
12 * with a callback. If the constant case went through a different path than
13 * the callback case, the hosted deployment would be a rewrite rather than a
14 * configuration, and everything the seam was built for would be spent.
15 *
16 * A TENANT ID IS NEVER TAKEN FROM ANYTHING A CLIENT CAN INFLUENCE.
17 *
18 * Not a header, not a query parameter, not a path segment. That is not a
19 * check performed here; it is the absence of a function that would do it. A
20 * code path that reads a tenant from a request cannot be added later by
21 * accident if it was never written, and this file is where somebody would go
22 * looking for one.
23 *
24 * So the only two sources are a configured constant and a host callback, and
25 * whatever either produces is validated against the same fixed character
26 * class before a single byte of it reaches a path.
27 */
28 #ifndef PO_TENANT_H
29 #define PO_TENANT_H
30  
31 #include "punk_observe/po_compat.h"
32 #include "punk_observe/po_store.h"
33 #include "punk_observe/po_hash.h"
34  
35 #define PO_TN_OK 0
36 #define PO_TN_EMPTY 1
37 #define PO_TN_TOO_LONG 2
38 #define PO_TN_BAD_CHAR 3
39 #define PO_TN_TRAVERSAL 4
40  
41 54 static const char *po_tn_reason(int rc) {
42 54 switch (rc) {
43 28 case PO_TN_OK: return "ok";
44 4 case PO_TN_EMPTY: return "a tenant id cannot be empty";
45 3 case PO_TN_TOO_LONG: return "a tenant id is at most 64 characters";
46 15 case PO_TN_BAD_CHAR: return "a tenant id is [A-Za-z0-9_-] only";
47 4 case PO_TN_TRAVERSAL: return "a tenant id cannot be . or ..";
48 0 default: return "refused";
49 }
50 }
51  
52 /* The boundary check, with a REASON.
53 *
54 * `po_tenant_ok` in po_store.h answers yes or no, which is all a path
55 * builder needs. A resolver that returned something unusable deserves to be
56 * told which rule it broke: the person debugging it wrote the resolver, and
57 * "invalid tenant" is not a bug report.
58 *
59 * `.` and `..` are called out separately even though the character class
60 * already excludes the dot. The class is the check; naming traversal is what
61 * makes the intent survive somebody widening the class later. */
62 79 static int po_tenant_check(const char *id, size_t len) {
63 size_t i;
64 79 ▶ if (!id || !len) return PO_TN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!id || !len) return PO_TN_EMPTY; 79 0 79
if (!id || !len) return PO_TN_EMPTY; 3 76 79
65 76 ▶ if (len > PO_TENANT_MAX) return PO_TN_TOO_LONG;
Branch: 1 branch
BranchTrueFalseTotal
if (len > PO_TENANT_MAX) return PO_TN_TOO_LONG; 2 74 76
66 74 ▶ if ((len == 1 && id[0] == '.')
Branch: 2 branches
BranchTrueFalseTotal
if ((len == 1 && id[0] == '.') 3 71 74
if ((len == 1 && id[0] == '.') 2 1 3
67 74 ▶ || (len == 2 && id[0] == '.' && id[1] == '.')) return PO_TN_TRAVERSAL;
Branch: 3 branches
BranchTrueFalseTotal
|| (len == 2 && id[0] == '.' && id[1] == '.')) return PO_TN_TRAVERSAL; 3 70 73
|| (len == 2 && id[0] == '.' && id[1] == '.')) return PO_TN_TRAVERSAL; 3 0 3
|| (len == 2 && id[0] == '.' && id[1] == '.')) return PO_TN_TRAVERSAL; 3 0 3
68 473 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 418 55 473
69 418 char c = id[i];
70 435 ▶ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
Branch: 5 branches
BranchTrueFalseTotal
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 395 23 418
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 0 395 395
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 4 19 23
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 3 1 4
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 15 2 17
71 22 ▶ || (c >= '0' && c <= '9') || c == '_' || c == '-'))
Branch: 3 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '_' || c == '-')) 6 16 22
|| (c >= '0' && c <= '9') || c == '_' || c == '-')) 3 3 6
|| (c >= '0' && c <= '9') || c == '_' || c == '-')) 17 2 19
72 15 return PO_TN_BAD_CHAR;
73 }
74 55 return PO_TN_OK;
75 }
76  
77 /* ---- the resolver ---------------------------------------------------------
78 *
79 * A callback, or NULL for the constant. Both go through po_tenant_resolve,
80 * so the validation is on one path rather than on two that can drift. */
81 typedef int (*po_tenant_fn)(void *ud, const char **out, size_t *len);
82  
83 typedef struct {
84 char fixed[PO_TENANT_MAX + 1];
85 size_t fixed_len;
86 po_tenant_fn fn;
87 void *ud;
88 int last_rc;
89 } po_tenant_cfg;
90  
91 /* Configure the constant. Refused here rather than at first use, so a typo in
92 * a config file is a boot failure and not a runtime one. */
93 28 static int po_tenant_set_fixed(po_tenant_cfg *c, const char *id, size_t len) {
94 int rc;
95 28 memset(c, 0, sizeof(*c));
96 28 ▶ if (!id || !len) { id = PO_TENANT_DEFAULT; len = strlen(PO_TENANT_DEFAULT); }
Branch: 2 branches
BranchTrueFalseTotal
if (!id || !len) { id = PO_TENANT_DEFAULT; len = strlen(PO_TENANT_DEFAULT); } 27 1 28
if (!id || !len) { id = PO_TENANT_DEFAULT; len = strlen(PO_TENANT_DEFAULT); } 0 27 27
97 28 rc = po_tenant_check(id, len);
98 28 ▶ if (rc != PO_TN_OK) { c->last_rc = rc; return rc; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc != PO_TN_OK) { c->last_rc = rc; return rc; } 1 27 28
99 27 memcpy(c->fixed, id, len);
100 27 c->fixed[len] = '\0';
101 27 c->fixed_len = len;
102 27 return PO_TN_OK;
103 }
104  
105 7 static void po_tenant_set_fn(po_tenant_cfg *c, po_tenant_fn fn, void *ud) {
106 7 c->fn = fn;
107 7 c->ud = ud;
108 7 }
109  
110 /* Resolve, then validate. In that order and never the reverse: a resolver is
111 * host code, and host code that returns `../other` is a bug this must catch
112 * rather than trust. */
113 26 static int po_tenant_resolve(po_tenant_cfg *c, char *out, size_t cap,
114 size_t *out_len) {
115 26 const char *id = NULL;
116 26 size_t len = 0;
117 int rc;
118  
119 26 ▶ if (c->fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->fn) { 7 19 26
120 7 ▶ if (!c->fn(c->ud, &id, &len) || !id) {
Branch: 2 branches
BranchTrueFalseTotal
if (!c->fn(c->ud, &id, &len) || !id) { 6 1 7
if (!c->fn(c->ud, &id, &len) || !id) { 0 6 6
121 1 c->last_rc = PO_TN_EMPTY;
122 1 return PO_TN_EMPTY;
123 }
124 }
125 19 else { id = c->fixed; len = c->fixed_len; }
126  
127 25 rc = po_tenant_check(id, len);
128 25 c->last_rc = rc;
129 25 ▶ if (rc != PO_TN_OK) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc != PO_TN_OK) return rc; 5 20 25
130 20 ▶ if (len + 1 > cap) { c->last_rc = PO_TN_TOO_LONG; return PO_TN_TOO_LONG; }
Branch: 1 branch
BranchTrueFalseTotal
if (len + 1 > cap) { c->last_rc = PO_TN_TOO_LONG; return PO_TN_TOO_LONG; } 0 20 20
131  
132 20 memcpy(out, id, len);
133 20 out[len] = '\0';
134 20 ▶ if (out_len) *out_len = len;
Branch: 1 branch
BranchTrueFalseTotal
if (out_len) *out_len = len; 20 0 20
135 20 return PO_TN_OK;
136 }
137  
138 /* ---- the segment header check ---------------------------------------------
139 *
140 * A segment carries a hash of the tenant it belongs to. Checked when the file
141 * is OPENED, not when its contents are served: a mis-filed segment found at
142 * open is an error, and one found after it has been read is a disclosure that
143 * already happened. */
144 5 static uint32_t po_tenant_hash(const char *id, size_t len) {
145 5 return po_hash32(id, len);
146 }
147  
148 3 static int po_tenant_owns(uint32_t header_hash, const char *id, size_t len) {
149 3 return header_hash == po_tenant_hash(id, len);
150 }
151  
152 #endif /* PO_TENANT_H */
« include/punk_observe/po_maplayout.h ↑ Summary include/punk_observe/po_panel.h »