include/punk_observe/po_store.h

filter: Statementstmt 100% 38 / 38 Branchbran 74.0% 37 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.2% 75 / 88 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_shared.h ↑ Summary include/punk_observe/po_view.h »
1 /* po_store.h - the root, and the tenant boundary.
2 *
3 * The engine is single-tenant. The hosted site is multi-tenant. The whole
4 * difference has to be ONE argument resolved at the top, or the site is a
5 * rewrite rather than a deployment.
6 *
7 * So every path the store constructs is rooted at po_store->root, and a
8 * multi-tenant deployment sets root = $data/<tenant_id>. There is no tenant
9 * column in a segment, no tenant field in po_rec, and no tenant predicate in
10 * a query.
11 *
12 * THIS IS A SECURITY DECISION, NOT A LAYOUT PREFERENCE. A tenant column means
13 * every query in the executor must remember to filter on it, and the one that
14 * forgets serves another customer's telemetry. A directory means a query for
15 * tenant A physically has no file handle that reaches tenant B's data. The
16 * failure mode of the column design is silent cross-tenant disclosure; the
17 * failure mode of this one is a missing directory.
18 */
19 #ifndef PO_STORE_H
20 #define PO_STORE_H
21  
22 #include "punk_observe/po_compat.h"
23  
24 #define PO_TENANT_MAX 64
25 #define PO_PATH_MAX 4096
26  
27 /* The default tenant, used when no resolver is configured. A self-hosted
28 * install on a private network should not need an ingest key, and this is
29 * what makes "no key" a supported configuration rather than a hole. */
30 #define PO_TENANT_DEFAULT "default"
31  
32 typedef struct {
33 char root[PO_PATH_MAX]; /* $data, or $data/<tenant_id> */
34 char tenant[PO_TENANT_MAX + 1];
35 } po_store;
36  
37 /* A tenant id is an opaque token and nothing else: [A-Za-z0-9_-]{1,64}.
38 *
39 * The allowlist is deliberate. A denylist here would have to know about "..",
40 * "/", "\", NUL, percent-encoding, overlong UTF-8 and whatever the next
41 * traversal trick is; an allowlist knows about none of them and refuses them
42 * all. Note especially that a backslash is refused even though it is not a
43 * separator on POSIX, because browsers fold it to one and the reflected-bytes
44 * class of bug has already cost this ecosystem a CVE.
45 *
46 * Returns 1 if the id is acceptable. It is checked BEFORE any path is
47 * constructed - a validated id is the only thing that reaches a buffer. */
48 39 static int po_tenant_ok(const char *id, size_t len) {
49 size_t i;
50 39 ▶ if (!id || len == 0 || len > PO_TENANT_MAX) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!id || len == 0 || len > PO_TENANT_MAX) return 0; 39 0 39
if (!id || len == 0 || len > PO_TENANT_MAX) return 0; 37 2 39
if (!id || len == 0 || len > PO_TENANT_MAX) return 0; 2 35 37
51 181 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 165 16 181
52 165 char c = id[i];
53 165 ▶ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
Branch: 5 branches
BranchTrueFalseTotal
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 142 23 165
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 141 1 142
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 138 26 164
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 0 138 138
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 6 20 26
54 165 ▶ (c >= '0' && c <= '9') || c == '_' || c == '-') continue;
Branch: 3 branches
BranchTrueFalseTotal
(c >= '0' && c <= '9') || c == '_' || c == '-') continue; 3 3 6
(c >= '0' && c <= '9') || c == '_' || c == '-') continue; 21 2 23
(c >= '0' && c <= '9') || c == '_' || c == '-') continue; 2 19 21
55 19 return 0;
56 }
57 /* "." and ".." are refused by the character class already, but a leading
58 * dot would still make a hidden directory, which is a surprise nobody
59 * needs. The class excludes '.' entirely, so this is belt and braces for
60 * a future edit that widens it. */
61 16 ▶ if (id[0] == '.') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (id[0] == '.') return 0; 0 16 16
62 16 return 1;
63 }
64  
65 /* Resolve a store root. `data` is the configured directory; `tenant` is NULL
66 * or empty for single-tenant, in which case the root IS `data` and no
67 * per-tenant directory is created.
68 *
69 * Returns 1 on success, 0 if the tenant id is refused or the path would not
70 * fit. A refusal never leaves a partially built root behind. */
71 6 static int po_store_init(po_store *s, const char *data,
72 const char *tenant, size_t tlen) {
73 size_t dlen;
74 6 ▶ if (!s || !data) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !data) return 0; 6 0 6
if (!s || !data) return 0; 0 6 6
75 6 dlen = strlen(data);
76 6 ▶ if (dlen == 0 || dlen >= PO_PATH_MAX) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (dlen == 0 || dlen >= PO_PATH_MAX) return 0; 6 0 6
if (dlen == 0 || dlen >= PO_PATH_MAX) return 0; 0 6 6
77  
78 6 memset(s, 0, sizeof(*s));
79  
80 6 ▶ if (!tenant || tlen == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!tenant || tlen == 0) { 5 1 6
if (!tenant || tlen == 0) { 0 5 5
81 1 memcpy(s->tenant, PO_TENANT_DEFAULT, sizeof(PO_TENANT_DEFAULT));
82 1 memcpy(s->root, data, dlen);
83 1 s->root[dlen] = '\0';
84 1 return 1;
85 }
86  
87 5 ▶ if (!po_tenant_ok(tenant, tlen)) { memset(s, 0, sizeof(*s)); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_tenant_ok(tenant, tlen)) { memset(s, 0, sizeof(*s)); return 0; } 2 3 5
88 3 ▶ if (dlen + 1 + tlen >= PO_PATH_MAX) { memset(s, 0, sizeof(*s)); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (dlen + 1 + tlen >= PO_PATH_MAX) { memset(s, 0, sizeof(*s)); return 0; } 0 3 3
89  
90 3 memcpy(s->tenant, tenant, tlen);
91 3 s->tenant[tlen] = '\0';
92  
93 3 memcpy(s->root, data, dlen);
94 3 s->root[dlen] = '/';
95 3 memcpy(s->root + dlen + 1, tenant, tlen);
96 3 s->root[dlen + 1 + tlen] = '\0';
97 3 return 1;
98 }
99  
100 /* Build a path under the store root. `rel` is written by this distribution
101 * and is never user input; the tenant component was validated in
102 * po_store_init and is the only thing here that ever came from outside.
103 *
104 * Returns the length written, or 0 if it would not fit. */
105 2 static size_t po_store_path(const po_store *s, const char *rel,
106 char *out, size_t outlen) {
107 size_t rlen, len;
108 2 ▶ if (!s || !rel || !out) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!s || !rel || !out) return 0; 2 0 2
if (!s || !rel || !out) return 0; 2 0 2
if (!s || !rel || !out) return 0; 0 2 2
109 2 rlen = strlen(s->root);
110 2 len = strlen(rel);
111 2 ▶ if (rlen + 1 + len >= outlen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rlen + 1 + len >= outlen) return 0; 0 2 2
112 2 memcpy(out, s->root, rlen);
113 2 out[rlen] = '/';
114 2 memcpy(out + rlen + 1, rel, len);
115 2 out[rlen + 1 + len] = '\0';
116 2 return rlen + 1 + len;
117 }
118  
119 #endif /* PO_STORE_H */
« include/punk_observe/po_shared.h ↑ Summary include/punk_observe/po_view.h »