include/punk/punk_blob.h

filter: Statementstmt 90.9% 372 / 409 Branchbran 62.5% 260 / 416 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.6% 632 / 825 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_afterres.h ↑ Summary include/punk/punk_scope.h »
1 /* punk_blob.h - content-addressed storage for uploads, on Apophis.
2 *
3 * Punk::Upload answers what an upload costs and stops there. Where the file
4 * then lives is left to the application, so every application invents a
5 * directory layout, a naming scheme and a deduplication story - and the
6 * naming scheme is usually the user's filename, which is how a path traversal
7 * gets written.
8 *
9 * Apophis does the storage half: deterministic UUID v5 identifiers, a sharded
10 * tree, atomic temp-and-rename writes, and dedup that needs no locking
11 * because content addressing is idempotent. This is the tier above it - the
12 * part that decides what an application may hand it and what it may hand
13 * back.
14 *
15 * WHY THE CONTENT IS COMPARED ON A DEDUP HIT.
16 *
17 * Apophis identifies content with UUID v5, which is SHA-1, and its store()
18 * returns immediately when the id already exists without comparing the bytes.
19 * That is the right optimisation under the assumption content addressing
20 * usually carries, that one id means one set of bytes.
21 *
22 * SHA-1 has not supported that assumption since 2017. The attack is three
23 * steps: craft two colliding files; upload A, which is stored and gets
24 * whatever review or sharing the application does; then anyone who later
25 * uploads B is handed a reference that serves A, because nothing was written.
26 *
27 * So a dedup hit READS THE STORED BLOB AND COMPARES IT. Equal costs one read.
28 * Unequal croaks, because it cannot be resolved: the store holds one of the
29 * two files and either answer is wrong for somebody.
30 *
31 * HOW APOPHIS IS REACHED.
32 *
33 * Through its C ABI (ap_abi.h, via ExtUtils::Depends), not through method
34 * calls. Identifying content, sharding it and writing it are all snprintf and
35 * memcpy underneath, and paying a Perl method call for each of them is most
36 * of what blob_send costs. Apophis 0.05+ is a hard prerequisite, so a missing
37 * or too-old table is a BOOT error raised by register, not a per-request
38 * fallback - there is no second implementation to fall back to, and quietly
39 * having one would be worse than the croak.
40 *
41 * What stays on the Perl API: remove, verify, meta. None of them is on the
42 * request path, and remove has a metadata sidecar to unlink as well as the
43 * blob. A second copy of those semantics, exercised once a month by a
44 * maintenance job, is how a rewrite quietly stops deleting something.
45 *
46 * Must be included after punk_context.h (pcx_call_meth), punk_app.h (app_hv)
47 * and punk_static.h (punk_closure).
48 */
49  
50 #ifndef PUNK_BLOB_H
51 #define PUNK_BLOB_H
52  
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <dirent.h>
56 #include <unistd.h>
57 #include <fcntl.h>
58  
59 #include "ap_abi.h" /* Apophis's public ABI, via ExtUtils::Depends */
60  
61 #define PB_PATH_MAX 4096
62  
63 static const ap_abi *PUNK_AP = NULL;
64 static int PUNK_AP_TRIED = 0;
65  
66 /* Resolve (once) Apophis's ABI table, or NULL. PUNK_FAKE_AP_BAD simulates a
67 * version mismatch for the guard test. */
68 178 static const ap_abi *punk_ap_try(pTHX) {
69 178 ▶ if (!PUNK_AP_TRIED) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_AP_TRIED) { 2 176 178
70 2 dSP; int count; UV p = 0;
71 2 PUNK_AP_TRIED = 1;
72 2 ▶ if (pk_require_once(aTHX_ "Apophis", FALSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pk_require_once(aTHX_ "Apophis", FALSE)) { 2 0 2
73 2 SPAGAIN; /* the require may have reallocated the value stack */
74 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 2 2
75 2 count = call_pv("Apophis::_abi_ptr", G_SCALAR | G_EVAL);
76 2 SPAGAIN;
77 /* SvUV, not SvIV: the address is unsigned, and a .so mapped with
78 * the top bit set reads back negative through PTR2IV/SvIV.
79 *
80 * And popped ONCE, into an SV*, because SvUV is a macro that
81 * mentions its argument more than once: SvUV(POPs) pops twice and
82 * reads the address off the wrong slot, so the table came back
83 * as zero and every perl older than the one this was written on
84 * failed the version guard with "upgrade Apophis to 0.05+"
85 * against an Apophis 0.05 that was sitting right there. */
86 2 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 2 0 2
87 2 SV *sv = POPs;
88 2 ▶ if (!SvTRUE(ERRSV)) p = SvUV(sv);
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) p = SvUV(sv); 2 0 2
if (!SvTRUE(ERRSV)) p = SvUV(sv); 2 0 2
89 }
90 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
91 2 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 2 0 2
92 2 const ap_abi *a = INT2PTR(const ap_abi *, p);
93 /* >= and never ==: the table is append-only, so a later one
94 * is a superset whose prefix stays valid. An equality check
95 * would make every Apophis release break this plugin. */
96 2 ▶ if (a && !getenv("PUNK_FAKE_AP_BAD")
Branch: 2 branches
BranchTrueFalseTotal
if (a && !getenv("PUNK_FAKE_AP_BAD") 2 0 2
if (a && !getenv("PUNK_FAKE_AP_BAD") 1 1 2
97 1 ▶ && a->abi_version >= AP_ABI_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
&& a->abi_version >= AP_ABI_VERSION) 1 0 1
98 1 PUNK_AP = a;
99 }
100 }
101 }
102 178 return PUNK_AP;
103 }
104  
105 /* The table, croaking if it is missing or too old. Called by register, so
106 * this is a boot-environment error rather than a surprise mid-request. */
107 178 static const ap_abi *punk_ap(pTHX) {
108 178 const ap_abi *a = punk_ap_try(aTHX);
109 178 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 1 177 178
110 1 croak("Punk::Plugin::Blob needs Apophis with a compatible C ABI "
111 "(AP_ABI_VERSION %d); upgrade Apophis to 0.05+", AP_ABI_VERSION);
112 177 return a;
113 }
114  
115 /* Was an option actually given? Defined, and not the empty string.
116 *
117 * SvCUR is only legal on a PV, and `namespace` is documented to take a
118 * CODEREF - a reference is an IV-bodied SV with no PVX at all. Reading one
119 * silently returns whatever sits at that offset on an ordinary perl and trips
120 * `PL_valid_types_PVX` on a DEBUGGING one, which is an abort rather than a
121 * croak. A reference is by definition not empty, so it answers before the
122 * length is ever looked at. */
123 9 static int pb_given(pTHX_ SV *sv) {
124 STRLEN len;
125 9 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 9 0 9
if (!sv || !SvOK(sv)) return 0; 0 9 9
126 9 ▶ if (SvROK(sv)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) return 1; 2 7 9
127 (void)SvPV_const(sv, len);
128 7 return len != 0;
129 }
130  
131 /* The store's namespace bytes and directory, or croak. */
132 71 static void pb_unpack(pTHX_ SV *store, const unsigned char **ns,
133 const char **dir, STRLEN *dirlen) {
134 71 const ap_abi *A = punk_ap(aTHX);
135 71 ▶ if (!A->store_of(aTHX_ store, ns, dir, dirlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->store_of(aTHX_ store, ns, dir, dirlen)) 0 71 71
136 0 croak("Punk::Plugin::Blob: the store is not a usable Apophis object "
137 "- it needs both a namespace and a store_dir");
138 71 }
139  
140 /* The on-disk path for an id, into a caller's buffer. Returns its length.
141 *
142 * build_path comes from the ABI rather than being two lines of snprintf here,
143 * and that is the point: the sharded layout is Apophis's to change, and a
144 * second copy of the rule would mean that the day it changes, every blob
145 * already on disk becomes unreachable through Punk while staying perfectly
146 * findable through Apophis. */
147 42 static int pb_path(pTHX_ SV *store, const char *id, STRLEN idlen,
148 char *out, size_t outsz) {
149 42 const ap_abi *A = punk_ap(aTHX);
150 const unsigned char *ns; const char *dir; STRLEN dirlen;
151 int n;
152 42 pb_unpack(aTHX_ store, &ns, &dir, &dirlen);
153 42 n = A->build_path(out, outsz, dir, dirlen, id, idlen);
154 /* snprintf semantics: n is the length it WOULD have needed. Apophis's own
155 * callers do not check this, so a store root long enough to overrun
156 * truncates silently - and a truncated path is a different file. */
157 42 ▶ if (n < 0 || (size_t)n >= outsz)
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || (size_t)n >= outsz) 42 0 42
if (n < 0 || (size_t)n >= outsz) 0 42 42
158 0 croak("Punk::Plugin::Blob: the blob path does not fit in %d bytes "
159 "- the store root is too long", (int)outsz);
160 42 return n;
161 }
162  
163 /* Do the bytes already at `path` match what is being offered?
164 *
165 * Compared in chunks rather than by reading the blob into a scalar: the whole
166 * point of this read is a memcmp, and a mismatched first block ends it. -1
167 * means the file could not be read at all. */
168 7 static int pb_same(const char *path, const char *want, STRLEN wantlen) {
169 char buf[65536];
170 struct stat st;
171 7 STRLEN off = 0;
172 7 int fd = open(path, O_RDONLY);
173 7 ▶ if (fd < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return -1; 0 7 7
174 7 ▶ if (fstat(fd, &st) != 0) { close(fd); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(fd, &st) != 0) { close(fd); return -1; } 0 7 7
175 7 ▶ if ((STRLEN)st.st_size != wantlen) { close(fd); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ((STRLEN)st.st_size != wantlen) { close(fd); return 0; } 1 6 7
176 15 ▶ while (off < wantlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < wantlen) { 9 6 15
177 9 size_t chunk = wantlen - off;
178 ssize_t got;
179 9 ▶ if (chunk > sizeof buf) chunk = sizeof buf;
Branch: 1 branch
BranchTrueFalseTotal
if (chunk > sizeof buf) chunk = sizeof buf; 4 5 9
180 9 got = read(fd, buf, chunk);
181 9 ▶ if (got <= 0) { close(fd); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) { close(fd); return -1; } 0 9 9
182 9 ▶ if (memNE(buf, want + off, (size_t)got)) { close(fd); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (memNE(buf, want + off, (size_t)got)) { close(fd); return 0; } 0 9 9
183 9 off += (STRLEN)got;
184 }
185 6 close(fd);
186 6 return 1;
187 }
188  
189 /* Is this a UUID, exactly?
190 *
191 * 8-4-4-4-12 lowercase hex and nothing else. It matters because an id usually
192 * came out of a URL and is about to become a filesystem path through
193 * Apophis's path_for - and send_file says so plainly: "the path is served as
194 * given - if any part of it came from the request, the traversal guard is
195 * yours".
196 *
197 * A whole-shape check rather than a scan for '..' or '/': a rule that
198 * enumerates the bad shapes misses one, and there is exactly one good shape. */
199 112 static int pb_id_ok(const char *s, STRLEN l) {
200 static const int dash[5] = { 8, 13, 18, 23, -1 };
201 STRLEN i;
202 112 int d = 0;
203 112 ▶ if (!s || l != 36) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || l != 36) return 0; 112 0 112
if (!s || l != 36) return 0; 72 40 112
204 1408 ▶ for (i = 0; i < 36; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 36; i++) { 1370 38 1408
205 1370 ▶ if (dash[d] >= 0 && i == (STRLEN)dash[d]) {
Branch: 2 branches
BranchTrueFalseTotal
if (dash[d] >= 0 && i == (STRLEN)dash[d]) { 914 456 1370
if (dash[d] >= 0 && i == (STRLEN)dash[d]) { 152 762 914
206 152 ▶ if (s[i] != '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] != '-') return 0; 0 152 152
207 152 d++;
208 152 continue;
209 }
210 1218 ▶ if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f')))
Branch: 4 branches
BranchTrueFalseTotal
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 1218 0 1218
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 396 822 1218
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 395 1 396
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 1 394 395
211 2 return 0;
212 }
213 38 return 1;
214 }
215  
216 /* The types a blob may be served INLINE as, and nothing else.
217 *
218 * Raster images. The list is short on purpose: every entry is a format a
219 * browser will render in this application's origin, so every entry is a
220 * decision about what an uploader may make it render.
221 *
222 * SVG IS NOT ON IT, which is the whole reason there is a list rather than an
223 * /^image\// test. An SVG is a document that can carry script, so serving one
224 * inline from your own origin is exactly the cross-site scripting this
225 * defends against - and image/svg+xml passes every naive image check ever
226 * written. PDF is not on it either: browsers render PDFs inline and a PDF is
227 * a far larger attack surface than a raster decoder. */
228 7 static const char *pb_inline_ok(const char *s, STRLEN l) {
229 static const char *ok[] = { "image/png", "image/jpeg", "image/gif",
230 "image/webp", "image/avif", NULL };
231 char buf[64];
232 7 STRLEN i, n = 0;
233 int j;
234 7 ▶ if (!s || !l) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !l) return NULL; 7 0 7
if (!s || !l) return NULL; 0 7 7
235 80 ▶ for (i = 0; i < l && n < sizeof buf - 1; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < l && n < sizeof buf - 1; i++) { 74 6 80
for (i = 0; i < l && n < sizeof buf - 1; i++) { 74 0 74
236 74 char c = s[i];
237 74 ▶ if (c == ';') break; /* drop any parameters */
Branch: 1 branch
BranchTrueFalseTotal
if (c == ';') break; /* drop any parameters */ 1 73 74
238 73 ▶ if (c == ' ' || c == '\t') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t') continue; 73 0 73
if (c == ' ' || c == '\t') continue; 0 73 73
239 73 ▶ buf[n++] = (c >= 'A' && c <= 'Z') ? (char)(c | 32) : c;
Branch: 2 branches
BranchTrueFalseTotal
buf[n++] = (c >= 'A' && c <= 'Z') ? (char)(c | 32) : c; 65 8 73
buf[n++] = (c >= 'A' && c <= 'Z') ? (char)(c | 32) : c; 8 57 65
240 }
241 7 buf[n] = '\0';
242 27 ▶ for (j = 0; ok[j]; j++) if (strEQ(buf, ok[j])) return ok[j];
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; ok[j]; j++) if (strEQ(buf, ok[j])) return ok[j]; 3 20 23
for (j = 0; ok[j]; j++) if (strEQ(buf, ok[j])) return ok[j]; 23 4 27
243 4 return NULL;
244 }
245  
246 /* Store bytes, comparing on a dedup hit. Returns the id (+1).
247 *
248 * The whole of Apophis's store() in C: identify, format, shard, stat, and
249 * either compare or write. No Perl frame, and the collision check is now a
250 * chunked compare rather than a fetch into a scalar. */
251 /* Create the directories leading to `path`.
252 *
253 * Apophis's write_atomic does this itself, but the cross-filesystem copy
254 * below cannot use write_atomic - that takes the content as a buffer, which
255 * is the one thing this whole path exists to avoid holding. */
256 1 static void pb_mkparent(pTHX_ const char *path) {
257 char buf[PB_PATH_MAX];
258 char *p;
259 1 STRLEN n = strlen(path);
260 1 ▶ if (n >= sizeof buf) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= sizeof buf) return; 0 1 1
261 1 Copy(path, buf, n + 1, char);
262 1 p = strrchr(buf, '/');
263 1 ▶ if (!p) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return; 0 1 1
264 1 *p = '\0';
265 21 ▶ for (p = buf + 1; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = buf + 1; *p; p++) { 20 1 21
266 20 ▶ if (*p != '/') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '/') continue; 17 3 20
267 3 *p = '\0';
268 3 (void)PerlDir_mkdir(buf, 0700);
269 3 *p = '/';
270 }
271 1 (void)PerlDir_mkdir(buf, 0700);
272 }
273  
274 /* An upload that is already a file: is there a path, and can it be read? */
275 29 static SV *pb_upload_path(pTHX_ SV *what) {
276 SV *p;
277 29 ▶ if (!(what && SvROK(what) && SvOBJECT(SvRV(what)))) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(what && SvROK(what) && SvOBJECT(SvRV(what)))) return NULL; 29 0 29
if (!(what && SvROK(what) && SvOBJECT(SvRV(what)))) return NULL; 29 0 29
if (!(what && SvROK(what) && SvOBJECT(SvRV(what)))) return NULL; 25 4 29
278 4 ▶ if (!pcx_can(aTHX_ what, "path")) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!pcx_can(aTHX_ what, "path")) return NULL; 0 4 4
279 4 p = pcx_call_meth(aTHX_ what, "path", NULL, 0, 1);
280 4 ▶ if (p && SvOK(p) && SvCUR(p)) return p;
Branch: 3 branches
BranchTrueFalseTotal
if (p && SvOK(p) && SvCUR(p)) return p; 4 0 4
if (p && SvOK(p) && SvCUR(p)) return p; 4 0 4
if (p && SvOK(p) && SvCUR(p)) return p; 4 0 4
281 0 ▶ if (p) SvREFCNT_dec(p);
Branch: 1 branch
BranchTrueFalseTotal
if (p) SvREFCNT_dec(p); 0 0 0
282 0 return NULL;
283 }
284  
285 /* Do two files hold the same bytes? Used for the deduplication check when
286 * neither side is in memory. -1 when one of them cannot be read. */
287 3 static int pb_same_file(pTHX_ const char *a, const char *b) {
288 3 PerlIO *fa = PerlIO_open(a, "rb");
289 PerlIO *fb;
290 3 int same = 1;
291 3 ▶ if (!fa) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!fa) return -1; 0 3 3
292 3 fb = PerlIO_open(b, "rb");
293 3 ▶ if (!fb) { PerlIO_close(fa); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!fb) { PerlIO_close(fa); return -1; } 0 3 3
294 8 for (;;) {
295 char ba[65536], bb[65536];
296 11 SSize_t na = PerlIO_read(fa, ba, sizeof ba);
297 11 SSize_t nb = PerlIO_read(fb, bb, sizeof bb);
298 11 ▶ if (na != nb) { same = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (na != nb) { same = 0; break; } 1 10 11
299 10 ▶ if (na <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (na <= 0) break; 2 8 10
300 8 ▶ if (memNE(ba, bb, (size_t)na)) { same = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (memNE(ba, bb, (size_t)na)) { same = 0; break; } 0 8 8
301 }
302 3 PerlIO_close(fa);
303 3 PerlIO_close(fb);
304 3 return same;
305 }
306  
307 /* Store an upload that is already on disk, without ever holding it.
308 *
309 * The upload path gets a large file from the wire to a temp file without it
310 * being resident anywhere. Storing it through ->content would undo all of
311 * that at the last step: the whole point of an id derived
312 * from the contents is that the contents must be READ, and reading them into
313 * a scalar to hash them is the copy that was just removed.
314 *
315 * Apophis ships `identify_fh` for exactly this - the same id the in-memory
316 * path produces, computed in 64KB chunks - and it says why a consumer must
317 * not reimplement it: the chunking and the namespace prefix are easy to get
318 * subtly wrong, and wrong here means ids that disagree with every other path.
319 *
320 * Returns NULL when this is not an on-disk upload, so the caller falls back.
321 */
322 29 static SV *pb_store_file(pTHX_ SV *ca, SV *what) {
323 29 const ap_abi *A = punk_ap(aTHX);
324 const unsigned char *ns; const char *dir; STRLEN dirlen;
325 unsigned char idb[16];
326 char id_str[37], path[PB_PATH_MAX];
327 29 SV *src = pb_upload_path(aTHX_ what);
328 PerlIO *fh;
329 struct stat st;
330  
331 29 ▶ if (!src) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return NULL; 25 4 29
332 4 fh = PerlIO_open(SvPVX(src), "rb");
333 4 ▶ if (!fh) { SvREFCNT_dec(src); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!fh) { SvREFCNT_dec(src); return NULL; } 0 4 4
334  
335 4 pb_unpack(aTHX_ ca, &ns, &dir, &dirlen);
336 4 A->identify_fh(aTHX_ idb, ns, fh);
337 4 PerlIO_close(fh);
338 4 A->format_id(id_str, idb);
339 4 (void)pb_path(aTHX_ ca, id_str, 36, path, sizeof path);
340  
341 4 ▶ if (stat(path, &st) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(path, &st) == 0) { 3 1 4
342 3 int same = pb_same_file(aTHX_ path, SvPVX(src));
343 3 ▶ if (same == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (same == 0) { 1 2 3
344 1 SvREFCNT_dec(src);
345 1 croak("Punk::Plugin::Blob: refusing to store - two DIFFERENT "
346 "files share the id %s. Apophis identifies content with "
347 "SHA-1, so this is either a deliberate collision or a "
348 "corrupted store, and either way the bytes already held "
349 "are not the bytes offered", id_str);
350 }
351 2 ▶ if (same == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (same == 1) { 2 0 2
352 /* The dedup case, and the one this is most obviously worth
353 * having: a file uploaded by a hundred users is stored once, and
354 * the ninety-nine after the first move nothing at all. */
355 2 SvREFCNT_dec(src);
356 2 return newSVpvn(id_str, 36);
357 }
358 /* -1: there but unreadable. Fall through and write. */
359 }
360  
361 { /* Same filesystem: a rename, which is free. Otherwise a copy, still
362 * without the bytes passing through a scalar. Either way the store
363 * only ever sees a COMPLETE file at a content address - a partial
364 * object at an address that claims to be its hash is a lie that
365 * survives every later integrity check. */
366 1 SV *tmp = newSVpv(path, 0);
367 1 sv_catpvs(tmp, ".part");
368 /* BEFORE the rename, not only in the copy branch below: rename fails
369 * with ENOENT when the destination's directory does not exist, which
370 * is every first store into a shard. Without this the rename always
371 * failed and the copy path always ran - so the move was never a move,
372 * on the filesystem where it was supposed to be free. */
373 1 (void)pb_mkparent(aTHX_ path);
374 1 ▶ if (PerlLIO_rename(SvPVX(src), path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_rename(SvPVX(src), path) != 0) { 0 1 1
375 0 PerlIO *in = PerlIO_open(SvPVX(src), "rb");
376 PerlIO *out;
377 0 ▶ if (!in) { SvREFCNT_dec(tmp); SvREFCNT_dec(src); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!in) { SvREFCNT_dec(tmp); SvREFCNT_dec(src); return NULL; } 0 0 0
378 0 out = PerlIO_open(SvPVX(tmp), "wb");
379 0 ▶ if (!out) { PerlIO_close(in); SvREFCNT_dec(tmp);
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { PerlIO_close(in); SvREFCNT_dec(tmp); 0 0 0
380 0 SvREFCNT_dec(src); return NULL; }
381 0 for (;;) {
382 char buf[65536];
383 0 SSize_t n = PerlIO_read(in, buf, sizeof buf);
384 0 ▶ if (n <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; 0 0 0
385 0 ▶ if (PerlIO_write(out, buf, n) != n) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_write(out, buf, n) != n) { 0 0 0
386 0 PerlIO_close(in); PerlIO_close(out);
387 0 (void)PerlLIO_unlink(SvPVX(tmp));
388 0 SvREFCNT_dec(tmp); SvREFCNT_dec(src);
389 0 croak("Punk::Plugin::Blob: short write storing %s", id_str);
390 }
391 }
392 0 PerlIO_close(in);
393 0 PerlIO_close(out);
394 0 ▶ if (PerlLIO_rename(SvPVX(tmp), path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_rename(SvPVX(tmp), path) != 0) { 0 0 0
395 0 (void)PerlLIO_unlink(SvPVX(tmp));
396 0 SvREFCNT_dec(tmp); SvREFCNT_dec(src);
397 0 croak("Punk::Plugin::Blob: cannot place %s", id_str);
398 }
399 }
400 1 SvREFCNT_dec(tmp);
401 }
402  
403 /* It has been moved into the store; the upload's cleanup must not chase
404 * it, exactly as Punk::Upload::save does after its own rename. */
405 1 ▶ if (pcx_can(aTHX_ what, "_disown"))
Branch: 1 branch
BranchTrueFalseTotal
if (pcx_can(aTHX_ what, "_disown")) 1 0 1
406 1 { SV *r = pcx_call_meth(aTHX_ what, "_disown", NULL, 0, 1);
407 1 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 1 0 1
408  
409 1 SvREFCNT_dec(src);
410 1 return newSVpvn(id_str, 36);
411 }
412  
413 29 static SV *pb_store(pTHX_ SV *ca, SV *what) {
414 29 const ap_abi *A = punk_ap(aTHX);
415 { /* an upload already on disk never becomes a scalar */
416 29 SV *id = pb_store_file(aTHX_ ca, what);
417 28 ▶ if (id) return id;
Branch: 1 branch
BranchTrueFalseTotal
if (id) return id; 3 25 28
418 }
419 {
420 const unsigned char *ns; const char *dir; STRLEN dirlen;
421 unsigned char idb[16];
422 char id_str[37], path[PB_PATH_MAX];
423 const char *content;
424 STRLEN clen;
425 struct stat st;
426  
427 25 pb_unpack(aTHX_ ca, &ns, &dir, &dirlen);
428 25 ▶ content = SvPV_const(SvROK(what) ? SvRV(what) : what, clen);
Branch: 1 branch
BranchTrueFalseTotal
content = SvPV_const(SvROK(what) ? SvRV(what) : what, clen); 25 0 25
429  
430 25 A->identify(idb, ns, content, clen);
431 25 A->format_id(id_str, idb);
432 25 (void)pb_path(aTHX_ ca, id_str, 36, path, sizeof path);
433  
434 25 ▶ if (stat(path, &st) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(path, &st) == 0) { 7 18 25
435 7 int same = pb_same(path, content, clen);
436 7 ▶ if (same == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (same == 0) 1 6 7
437 1 croak("Punk::Plugin::Blob: refusing to store - two DIFFERENT "
438 "files share the id %s. Apophis identifies content with "
439 "SHA-1, so this is either a deliberate collision or a "
440 "corrupted store, and either way the bytes already held "
441 "are not the bytes offered", id_str);
442 6 ▶ if (same == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (same == 1) 6 0 6
443 6 return newSVpvn(id_str, 36); /* the ordinary dedup case */
444 /* -1: it is there but unreadable, so the store changed underneath
445 * us. Fall through and write. */
446 }
447  
448 18 A->write_atomic(aTHX_ path, content, clen);
449 18 return newSVpvn(id_str, 36);
450 }
451 }
452  
453 /* What the caller handed over, as the scalar ref Apophis takes (+1).
454 *
455 * A Punk::Upload, a scalar ref, or a string of bytes. NOT a filename: a
456 * string that happens to name a file is content, and guessing otherwise is
457 * how an application stores the path instead of the bytes. */
458 4 static SV *pb_content_ref(pTHX_ SV *what) {
459 4 ▶ if (!(what && SvOK(what)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(what && SvOK(what))) 4 0 4
if (!(what && SvOK(what))) 0 4 4
460 0 croak("Punk::Plugin::Blob: blob_put needs content");
461 4 ▶ if (SvROK(what) && SvTYPE(SvRV(what)) == SVt_PVMG && SvOBJECT(SvRV(what))) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(what) && SvTYPE(SvRV(what)) == SVt_PVMG && SvOBJECT(SvRV(what))) { 0 4 4
if (SvROK(what) && SvTYPE(SvRV(what)) == SVt_PVMG && SvOBJECT(SvRV(what))) { 0 0 0
if (SvROK(what) && SvTYPE(SvRV(what)) == SVt_PVMG && SvOBJECT(SvRV(what))) { 0 0 0
462 /* something blessed: an upload, if it can hand over its bytes */
463 0 SV *bytes = pcx_call_meth(aTHX_ what, "content", NULL, 0, 1);
464 0 ▶ if (bytes) return newRV_noinc(bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (bytes) return newRV_noinc(bytes); 0 0 0
465 0 croak("Punk::Plugin::Blob: blob_put takes a Punk::Upload, a scalar "
466 "reference, or a string of bytes");
467 }
468 4 ▶ if (SvROK(what)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(what)) { 0 4 4
469 0 ▶ if (SvTYPE(SvRV(what)) > SVt_PVMG)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(what)) > SVt_PVMG) 0 0 0
470 0 croak("Punk::Plugin::Blob: blob_put takes a Punk::Upload, a "
471 "scalar reference, or a string of bytes");
472 0 return newRV_inc(SvRV(what)); /* already a scalar ref */
473 }
474 4 return newRV_noinc(newSVsv(what));
475 }
476  
477 /* ---- the sweep --------------------------------------------------------------
478 *
479 * Deduplication means a blob may have several references, so deleting one
480 * cannot mean unlinking the bytes: two users uploaded the same file, one
481 * deletes their copy, and the other must still have theirs. Unlinking on the
482 * first delete is data loss that looks like a successful operation.
483 *
484 * Refcounting would work and puts an atomic read-modify-write on the upload
485 * path - a lock, in a component whose whole appeal is that content addressing
486 * needs none. So nothing unlinks during a request, and a sweep asks the
487 * application which ids are still referenced and removes the rest. Unswept
488 * orphans cost disk, and disk is cheaper than a support ticket about a
489 * missing file.
490 *
491 * THE DANGEROUS PART IS THE ANSWER, NOT THE WALK. A `live` callback that dies
492 * or returns nothing is indistinguishable from "no blobs are referenced", and
493 * acting on that unlinks the whole store.
494 */
495  
496 31 static void pb_sweep_dir(pTHX_ const char *dir, HV *live, double now,
497 double grace, int dry, UV *removed, UV *kept) {
498 31 DIR *d = opendir(dir);
499 struct dirent *e;
500 31 ▶ if (!d) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return; 0 31 31
501 106 ▶ while ((e = readdir(d))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = readdir(d))) { 75 31 106
502 char path[2048];
503 struct stat st;
504 75 STRLEN nl = strlen(e->d_name);
505  
506 /* Only things shaped like a blob. A temp file from an interrupted
507 * write, or anything a human left here, is left alone: a sweep that
508 * removes what it does not recognise eventually removes something
509 * that mattered. */
510 81 ▶ if (!pb_id_ok(e->d_name, nl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pb_id_ok(e->d_name, nl)) continue; 63 12 75
511 12 ▶ if ((size_t)snprintf(path, sizeof path, "%s/%s", dir, e->d_name)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)snprintf(path, sizeof path, "%s/%s", dir, e->d_name) 0 12 12
512 0 >= sizeof path) continue;
513 12 ▶ if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) continue; 12 0 12
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) continue; 0 12 12
514  
515 12 ▶ if (hv_exists(live, e->d_name, (I32)nl)) { (*kept)++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(live, e->d_name, (I32)nl)) { (*kept)++; continue; } 5 7 12
516  
517 /* A blob written between the caller's snapshot of live ids and this
518 * walk is in neither, and is not an orphan. The grace period is what
519 * stops that race collecting a file whose row was committed a moment
520 * later. */
521 7 ▶ if (grace > 0 && now - (double)st.st_mtime < grace) {
Branch: 2 branches
BranchTrueFalseTotal
if (grace > 0 && now - (double)st.st_mtime < grace) { 1 6 7
if (grace > 0 && now - (double)st.st_mtime < grace) { 1 0 1
522 1 (*kept)++;
523 1 continue;
524 }
525 6 ▶ if (!dry && unlink(path) != 0) { (*kept)++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!dry && unlink(path) != 0) { (*kept)++; continue; } 5 1 6
if (!dry && unlink(path) != 0) { (*kept)++; continue; } 0 5 5
526 6 (*removed)++;
527 }
528 31 closedir(d);
529 }
530  
531 /* The whole store: two shard levels, then the blob. */
532 8 static void pb_sweep(pTHX_ const char *root, HV *live, double grace, int dry,
533 UV *removed, UV *kept) {
534 8 DIR *d1 = opendir(root);
535 struct dirent *e1;
536 8 double now = (double)time(NULL);
537 8 ▶ if (!d1) croak("Punk::Plugin::Blob: cannot read the store at '%s'", root);
Branch: 1 branch
BranchTrueFalseTotal
if (!d1) croak("Punk::Plugin::Blob: cannot read the store at '%s'", root); 0 8 8
538 55 ▶ while ((e1 = readdir(d1))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e1 = readdir(d1))) { 47 8 55
539 char l1[2048];
540 DIR *d2;
541 struct dirent *e2;
542 47 ▶ if (e1->d_name[0] == '.') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (e1->d_name[0] == '.') continue; 16 31 47
543 31 ▶ if ((size_t)snprintf(l1, sizeof l1, "%s/%s", root, e1->d_name)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)snprintf(l1, sizeof l1, "%s/%s", root, e1->d_name) 0 31 31
544 0 >= sizeof l1) continue;
545 31 d2 = opendir(l1);
546 31 ▶ if (!d2) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!d2) continue; 0 31 31
547 124 ▶ while ((e2 = readdir(d2))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e2 = readdir(d2))) { 93 31 124
548 char l2[2048];
549 93 ▶ if (e2->d_name[0] == '.') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (e2->d_name[0] == '.') continue; 62 31 93
550 31 ▶ if ((size_t)snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name) 0 31 31
551 0 >= sizeof l2) continue;
552 31 pb_sweep_dir(aTHX_ l2, live, now, grace, dry, removed, kept);
553 }
554 31 closedir(d2);
555 }
556 8 closedir(d1);
557 8 }
558  
559 /* Ask the application which ids are still referenced, and refuse to act on an
560 * answer that might not be one. Returns a hash of live ids (+1), or croaks. */
561 11 static HV *pb_live_set(pTHX_ SV *cb, int allow_empty) {
562 11 dSP;
563 11 HV *live = newHV();
564 11 AV *got = (AV *)sv_2mortal((SV *)newAV());
565 11 AV *items = got;
566 int count, i;
567 11 IV n = 0;
568  
569 11 ENTER; SAVETMPS;
570 11 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 11 11
571 11 PUTBACK;
572 11 count = call_sv(cb, G_ARRAY | G_EVAL);
573 11 SPAGAIN;
574 20 ▶ for (i = count - 1; i >= 0; i--) av_store(got, (SSize_t)i, newSVsv(POPs));
Branch: 1 branch
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) av_store(got, (SSize_t)i, newSVsv(POPs)); 9 11 20
575 11 PUTBACK;
576 11 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 11 0 11
if (SvTRUE(ERRSV)) { 1 10 11
577 /* croak BEFORE the FREETMPS rather than after it holding a pointer
578 * into what it just freed: an SV mortalised inside this scope is gone
579 * the moment its temporaries are released, and reading the message
580 * out of it afterwards is a use-after-free that usually still says
581 * the right thing. perl unwinds the scope. */
582 1 SvREFCNT_dec((SV *)live);
583 2 ▶ croak("Punk::Plugin::Blob: the sweep's `live` callback died, so "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Plugin::Blob: the sweep's `live` callback died, so " 1 0 1
584 "NOTHING was removed - an answer that failed is not an answer "
585 "that there are no live blobs: %s", SvPV_nolen(ERRSV));
586 }
587 10 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 8 2 10
588  
589 /* One arrayref, or a plain list - a model returns the first and a
590 * hand-written callback the second.
591 *
592 * BORROWED, not copied back into `got`: emptying `got` to refill it would
593 * free the arrayref SV, which holds the only reference to the array being
594 * read from, and the loop below would then walk freed memory. */
595 10 ▶ if (count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) { 7 3 10
596 7 SV **only = av_fetch(got, 0, 0);
597 7 ▶ if (only && *only && SvROK(*only)
Branch: 3 branches
BranchTrueFalseTotal
if (only && *only && SvROK(*only) 7 0 7
if (only && *only && SvROK(*only) 7 0 7
if (only && *only && SvROK(*only) 1 6 7
598 1 ▶ && SvTYPE(SvRV(*only)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*only)) == SVt_PVAV) { 1 0 1
599 1 items = (AV *)SvRV(*only);
600 1 count = (int)(av_len(items) + 1);
601 }
602 }
603  
604 19 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 9 10 19
605 9 SV **x = av_fetch(items, (SSize_t)i, 0);
606 STRLEN l;
607 const char *s;
608 9 ▶ if (!(x && *x && SvOK(*x))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(x && *x && SvOK(*x))) continue; 9 0 9
if (!(x && *x && SvOK(*x))) continue; 9 0 9
if (!(x && *x && SvOK(*x))) continue; 1 8 9
609 8 s = SvPV_const(*x, l);
610 8 ▶ if (!pb_id_ok(s, l)) continue; /* not an id: cannot be live */
Branch: 1 branch
BranchTrueFalseTotal
if (!pb_id_ok(s, l)) continue; /* not an id: cannot be live */ 0 8 8
611 8 (void)hv_store(live, s, (I32)l, newSViv(1), 0);
612 8 n++;
613 }
614  
615 10 ▶ if (n == 0 && !allow_empty) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 && !allow_empty) { 3 7 10
if (n == 0 && !allow_empty) { 2 1 3
616 2 SvREFCNT_dec((SV *)live);
617 2 croak("Punk::Plugin::Blob: the sweep's `live` callback returned no "
618 "blob ids, so NOTHING was removed. An empty answer is "
619 "indistinguishable from a query that failed, and acting on it "
620 "would unlink the whole store. Pass allow_empty => 1 if the "
621 "application really does reference no blobs");
622 }
623 8 return live;
624 }
625  
626 /* Call a plain coderef, returning its scalar result (+1) or NULL. */
627 10 static SV *pb_call_cv(pTHX_ SV *cv, SV **argv, int n) {
628 10 dSP;
629 10 SV *r = NULL;
630 int count, i;
631 10 ENTER; SAVETMPS;
632 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
633 10 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 10 0 10
EXTEND(SP, n); 0 10 10
634 20 ▶ for (i = 0; i < n; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(argv[i]); 10 10 20
635 10 PUTBACK;
636 10 count = call_sv(cv, G_SCALAR);
637 10 SPAGAIN;
638 10 ▶ if (count > 0) r = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) r = SvREFCNT_inc(POPs); 10 0 10
639 10 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 10 0 10
640 10 return r;
641 }
642  
643 /* ---- the helpers, as C closures ---------------------------------------------
644 *
645 * Each captures the Apophis store built at registration, so a request never
646 * constructs one. cap = [store, root].
647 */
648  
649 /* The store this request belongs to, borrowed.
650 *
651 * cap = [store-or-undef, root, namespace, cache].
652 *
653 * A STRING namespace is one store, built at registration: every tenant shares
654 * ids, and a file held by two of them is stored once.
655 *
656 * A CODEREF namespace is one store per tenant. It cannot be built at
657 * registration because it depends on the request, so it is built on first use
658 * and cached per worker - the cost is one Apophis object per tenant this
659 * worker has served, which is a handful of scalars.
660 *
661 * Which of the two an application wants is the trade in "Deduplication
662 * crosses tenants", and it is deliberately not defaulted. */
663 25 static SV *pb_store_for(pTHX_ CV *cv, SV *c) {
664 25 AV *cap = punk_clos_cap(aTHX_ cv);
665 SV **s, **ns, **cache, **root;
666 SV *nsv;
667 HV *seen;
668 HE *have;
669  
670 25 ▶ if (!cap) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) return NULL; 0 25 25
671 25 s = av_fetch(cap, 0, 0);
672 25 ▶ if (s && *s && SvROK(*s)) return *s; /* one store for everyone */
Branch: 3 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s)) return *s; /* one store for everyone */ 25 0 25
if (s && *s && SvROK(*s)) return *s; /* one store for everyone */ 25 0 25
if (s && *s && SvROK(*s)) return *s; /* one store for everyone */ 15 10 25
673  
674 10 ns = av_fetch(cap, 2, 0);
675 10 root = av_fetch(cap, 1, 0);
676 10 cache = av_fetch(cap, 3, 0);
677 10 ▶ if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV)) 10 0 10
if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV)) 10 0 10
if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV)) 10 0 10
if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV)) 0 10 10
678 0 return NULL;
679 10 ▶ if (!(cache && *cache && SvROK(*cache))) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cache && *cache && SvROK(*cache))) return NULL; 10 0 10
if (!(cache && *cache && SvROK(*cache))) return NULL; 10 0 10
if (!(cache && *cache && SvROK(*cache))) return NULL; 0 10 10
680 10 seen = (HV *)SvRV(*cache);
681  
682 { /* $namespace->($c) */
683 SV *argv[1];
684 10 ▶ argv[0] = c ? c : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[0] = c ? c : &PL_sv_undef; 10 0 10
685 10 nsv = pb_call_cv(aTHX_ *ns, argv, c ? 1 : 0);
686 }
687 10 ▶ if (!(nsv && SvOK(nsv) && SvCUR(nsv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(nsv && SvOK(nsv) && SvCUR(nsv))) { 10 0 10
if (!(nsv && SvOK(nsv) && SvCUR(nsv))) { 10 0 10
if (!(nsv && SvOK(nsv) && SvCUR(nsv))) { 1 9 10
688 1 ▶ if (nsv) SvREFCNT_dec(nsv);
Branch: 1 branch
BranchTrueFalseTotal
if (nsv) SvREFCNT_dec(nsv); 1 0 1
689 1 croak("Punk::Plugin::Blob: the `namespace` callback gave back "
690 "nothing - it decides which tenant's ids these are, so an "
691 "empty answer would silently put this request in somebody "
692 "else's namespace");
693 }
694 9 sv_2mortal(nsv);
695  
696 9 have = hv_fetch_ent(seen, nsv, 0, 0);
697 9 ▶ if (have) return HeVAL(have);
Branch: 1 branch
BranchTrueFalseTotal
if (have) return HeVAL(have); 7 2 9
698  
699 {
700 SV *argv[4], *store;
701 2 argv[0] = sv_2mortal(newSVpvs("namespace"));
702 2 argv[1] = nsv;
703 2 argv[2] = sv_2mortal(newSVpvs("store_dir"));
704 2 ▶ argv[3] = (root && *root) ? *root : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[3] = (root && *root) ? *root : &PL_sv_undef; 2 0 2
argv[3] = (root && *root) ? *root : &PL_sv_undef; 2 0 2
705 2 store = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Apophis")), "new",
706 argv, 4, 1);
707 2 ▶ if (!(store && SvROK(store)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(store && SvROK(store))) 2 0 2
if (!(store && SvROK(store))) 0 2 2
708 0 croak("Punk::Plugin::Blob: Apophis->new gave back no store for "
709 "namespace '%" SVf "'", SVfARG(nsv));
710 2 (void)hv_store_ent(seen, nsv, store, 0);
711 2 return store;
712 }
713 }
714  
715 XS_INTERNAL(pb_put_cb);
716 5 XS_INTERNAL(pb_put_cb) {
717 5 dXSARGS;
718 5 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); 5 0 5
719 4 ▶ SV *what = items > 1 ? ST(1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *what = items > 1 ? ST(1) : NULL; 4 0 4
720 SV *ref;
721 4 ▶ if (!store) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!store) XSRETURN_EMPTY; 0 4 4
722 4 ref = sv_2mortal(pb_content_ref(aTHX_ what));
723 4 ST(0) = sv_2mortal(pb_store(aTHX_ store, ref));
724 4 XSRETURN(1);
725 }
726  
727 XS_INTERNAL(pb_exists_cb);
728 2 XS_INTERNAL(pb_exists_cb) {
729 2 dXSARGS;
730 2 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); 2 0 2
731 2 ▶ SV *id = items > 1 ? ST(1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *id = items > 1 ? ST(1) : NULL; 2 0 2
732 2 STRLEN l = 0;
733 4 ▶ const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 2 0 2
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 2 0 2
734 char path[PB_PATH_MAX];
735 struct stat st;
736 2 ▶ if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); }
Branch: 2 branches
BranchTrueFalseTotal
if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 2 0 2
if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 0 2 2
737 2 (void)pb_path(aTHX_ store, s, l, path, sizeof path);
738 2 ▶ ST(0) = sv_2mortal(newSViv(stat(path, &st) == 0 && S_ISREG(st.st_mode)));
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = sv_2mortal(newSViv(stat(path, &st) == 0 && S_ISREG(st.st_mode))); 1 1 2
ST(0) = sv_2mortal(newSViv(stat(path, &st) == 0 && S_ISREG(st.st_mode))); 1 0 1
739 2 XSRETURN(1);
740 }
741  
742 XS_INTERNAL(pb_path_cb);
743 2 XS_INTERNAL(pb_path_cb) {
744 2 dXSARGS;
745 2 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); 2 0 2
746 2 ▶ SV *id = items > 1 ? ST(1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *id = items > 1 ? ST(1) : NULL; 2 0 2
747 2 STRLEN l = 0;
748 4 ▶ const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 2 0 2
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 2 0 2
749 2 ▶ if (!store) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!store) XSRETURN_EMPTY; 0 2 2
750 /* blob_path croaks where blob_send answers 404: a program calls this one
751 * deliberately, so a bad id here is a bug rather than a stray request. */
752 2 ▶ if (!pb_id_ok(s, l))
Branch: 1 branch
BranchTrueFalseTotal
if (!pb_id_ok(s, l)) 1 1 2
753 1 ▶ croak("Punk::Plugin::Blob: '%s' is not a blob id", s ? s : "(undef)");
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Plugin::Blob: '%s' is not a blob id", s ? s : "(undef)"); 1 0 1
754 {
755 char path[PB_PATH_MAX];
756 1 int n = pb_path(aTHX_ store, s, l, path, sizeof path);
757 1 ST(0) = sv_2mortal(newSVpvn(path, n));
758 }
759 1 XSRETURN(1);
760 }
761  
762 XS_INTERNAL(pb_remove_cb);
763 2 XS_INTERNAL(pb_remove_cb) {
764 2 dXSARGS;
765 2 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); 2 0 2
766 2 ▶ SV *id = items > 1 ? ST(1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *id = items > 1 ? ST(1) : NULL; 2 0 2
767 2 STRLEN l = 0;
768 4 ▶ const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 2 0 2
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 2 0 2
769 SV *r;
770 2 ▶ if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); }
Branch: 2 branches
BranchTrueFalseTotal
if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 2 0 2
if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 0 2 2
771 2 r = pcx_call_meth(aTHX_ store, "remove", &id, 1, 1);
772 2 ▶ ST(0) = sv_2mortal(newSViv((r && SvTRUE(r)) ? 1 : 0));
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = sv_2mortal(newSViv((r && SvTRUE(r)) ? 1 : 0)); 2 0 2
ST(0) = sv_2mortal(newSViv((r && SvTRUE(r)) ? 1 : 0)); 1 1 2
773 2 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 2 0 2
774 2 XSRETURN(1);
775 }
776  
777 XS_INTERNAL(pb_store_cb);
778 3 XS_INTERNAL(pb_store_cb) {
779 3 dXSARGS;
780 3 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); 3 0 3
781 3 ▶ ST(0) = store ? sv_2mortal(newSVsv(store)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = store ? sv_2mortal(newSVsv(store)) : &PL_sv_undef; 3 0 3
782 3 XSRETURN(1);
783 }
784  
785 XS_INTERNAL(pb_root_cb);
786 3 XS_INTERNAL(pb_root_cb) {
787 3 dXSARGS;
788 3 AV *cap = punk_clos_cap(aTHX_ cv);
789 3 ▶ SV **r = cap ? av_fetch(cap, 1, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **r = cap ? av_fetch(cap, 1, 0) : NULL; 3 0 3
790 PERL_UNUSED_VAR(items);
791 3 ▶ ST(0) = (r && *r) ? sv_2mortal(newSVsv(*r)) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = (r && *r) ? sv_2mortal(newSVsv(*r)) : &PL_sv_undef; 3 0 3
ST(0) = (r && *r) ? sv_2mortal(newSVsv(*r)) : &PL_sv_undef; 3 0 3
792 3 XSRETURN(1);
793 }
794  
795 XS_INTERNAL(pb_safe_type_cb);
796 1 XS_INTERNAL(pb_safe_type_cb) {
797 1 dXSARGS;
798 1 ▶ SV *claimed = items > 1 ? ST(1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *claimed = items > 1 ? ST(1) : NULL; 1 0 1
799 1 STRLEN l = 0;
800 2 ▶ const char *s = (claimed && SvOK(claimed)) ? SvPV_const(claimed, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (claimed && SvOK(claimed)) ? SvPV_const(claimed, l) : NULL; 1 0 1
const char *s = (claimed && SvOK(claimed)) ? SvPV_const(claimed, l) : NULL; 1 0 1
801 1 const char *ok = pb_inline_ok(s, l);
802 1 ▶ ST(0) = ok ? sv_2mortal(newSVpv(ok, 0)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = ok ? sv_2mortal(newSVpv(ok, 0)) : &PL_sv_undef; 0 1 1
803 1 XSRETURN(1);
804 }
805  
806 /* blob_send: the unfriendly defaults, and why.
807 *
808 * An uploader controls the bytes AND claims the type, so serving those bytes
809 * back as the claimed type is stored cross-site scripting with this
810 * application's origin on it - the commonest way a file-upload feature
811 * becomes a vulnerability.
812 *
813 * Content-Type: application/octet-stream
814 * Content-Disposition: attachment
815 * X-Content-Type-Options: nosniff
816 *
817 * nosniff matters as much as the type: without it a browser may sniff HTML
818 * out of a response labelled octet-stream and render it, which puts the whole
819 * defence back where it started. It is set always and this helper offers no
820 * way to turn it off. */
821 XS_INTERNAL(pb_send_cb);
822 11 XS_INTERNAL(pb_send_cb) {
823 11 dXSARGS;
824 11 ▶ SV *c = items > 0 ? ST(0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : NULL; 11 0 11
825 11 SV *store = pb_store_for(aTHX_ cv, c);
826 11 ▶ SV *id = items > 1 ? ST(1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *id = items > 1 ? ST(1) : NULL; 11 0 11
827 11 STRLEN l = 0;
828 22 ▶ const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 11 0 11
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 11 0 11
829 SV *path, **argv;
830 11 int n = 1, i, have_type = 0, have_cc = 0, have_fn = 0, inline_ok = 0;
831 11 int public_ = 0;
832  
833 11 ▶ if (!(store && c)) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!(store && c)) XSRETURN_EMPTY; 11 0 11
if (!(store && c)) XSRETURN_EMPTY; 0 11 11
834  
835 /* An id that is not an id is a 404, not an error: it came out of a URL,
836 * so something probing /blob/../.. should be told there is nothing there
837 * rather than fill the error log with 500s. */
838 11 ▶ if (!pb_id_ok(s, l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pb_id_ok(s, l)) { 1 10 11
839 1 ST(0) = sv_2mortal(pcx_call_meth(aTHX_ c, "not_found", NULL, 0, 1));
840 1 XSRETURN(1);
841 }
842  
843 /* Always, and before send_file, which folds set headers in. */
844 {
845 SV *hv[2];
846 SV *r;
847 10 hv[0] = sv_2mortal(newSVpvs("X-Content-Type-Options"));
848 10 hv[1] = sv_2mortal(newSVpvs("nosniff"));
849 10 r = pcx_call_meth(aTHX_ c, "header", hv, 2, 1);
850 10 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 10 0 10
851 }
852  
853 /* send_file's arguments: the path, every option the caller gave, and
854 * the ten at most that are added below. Sized from the call, because a
855 * fixed array is a stack overflow the caller's argument count chooses. */
856 10 Newx(argv, items + 12, SV *);
857 10 SAVEFREEPV(argv);
858  
859 13 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 3 10 13
860 3 const char *k = SvPV_nolen(ST(i));
861 3 ▶ if (strEQ(k, "type")) have_type = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "type")) have_type = 1; 1 2 3
862 2 ▶ else if (strEQ(k, "cache_control")) have_cc = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "cache_control")) have_cc = 1; 0 2 2
863 2 ▶ else if (strEQ(k, "filename")) have_fn = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "filename")) have_fn = 1; 0 2 2
864 2 ▶ else if (strEQ(k, "inline")) inline_ok = SvTRUE(ST(i + 1)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "inline")) inline_ok = SvTRUE(ST(i + 1)) ? 1 : 0; 1 1 2
865 1 ▶ else if (strEQ(k, "public")) { public_ = SvTRUE(ST(i + 1)) ? 1 : 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "public")) { public_ = SvTRUE(ST(i + 1)) ? 1 : 0; continue; } 1 0 1
866 2 argv[n++] = ST(i);
867 2 argv[n++] = ST(i + 1);
868 }
869  
870 10 ▶ if (!have_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have_type) { 9 1 10
871 /* nothing unknown is displayed inline */
872 9 argv[n++] = sv_2mortal(newSVpvs("type"));
873 9 argv[n++] = sv_2mortal(newSVpvs("application/octet-stream"));
874 9 ▶ if (inline_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (inline_ok) { 0 9 9
875 0 argv[n++] = sv_2mortal(newSVpvs("inline"));
876 0 argv[n++] = sv_2mortal(newSViv(0));
877 }
878 9 ▶ if (!have_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have_fn) { 9 0 9
879 9 argv[n++] = sv_2mortal(newSVpvs("filename"));
880 9 argv[n++] = sv_2mortal(newSVpvs("blob"));
881 9 have_fn = 1;
882 }
883 }
884 10 ▶ if (!have_cc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have_cc) { 10 0 10
885 /* A content-addressed URL cannot come to mean anything else, so a
886 * long lifetime is right - but `public` on a blob behind
887 * authorisation tells a shared cache it may hand somebody's document
888 * to the next person who asks. */
889 10 argv[n++] = sv_2mortal(newSVpvs("cache_control"));
890 10 ▶ argv[n++] = sv_2mortal(newSVpvf("%s, max-age=31536000, immutable",
Branch: 1 branch
BranchTrueFalseTotal
argv[n++] = sv_2mortal(newSVpvf("%s, max-age=31536000, immutable", 1 9 10
891 public_ ? "public" : "private"));
892 }
893 /* A well-formed id naming no blob is a reference the sweep collected. */
894 10 argv[n++] = sv_2mortal(newSVpvs("missing"));
895 10 argv[n++] = sv_2mortal(newSVpvs("not_found"));
896  
897 { /* the request path's one remaining Perl call was this snprintf */
898 char buf[PB_PATH_MAX];
899 10 STRLEN il = 0;
900 10 const char *is = SvPV_const(id, il);
901 10 int pn = pb_path(aTHX_ store, is, il, buf, sizeof buf);
902 10 path = sv_2mortal(newSVpvn(buf, pn));
903 }
904 10 argv[0] = path;
905 10 ST(0) = sv_2mortal(pcx_call_meth(aTHX_ c, "send_file", argv, n, 1));
906 10 XSRETURN(1);
907 }
908  
909 #endif /* PUNK_BLOB_H */
« include/punk/punk_afterres.h ↑ Summary include/punk/punk_scope.h »