Zoo

← back to the zoo

12. Your files

You can upload a file, see it listed, and download it again unchanged.

storage keeps each person's files (B2-backed) behind their sign-in; an app links to it and never holds storage's keys. The check here uploads random bytes, lists them, downloads them and compares.

This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit 55c71513c5 (live). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.

e12_your_files.odin sha256 ec5718925d8f · raw

1package main
2
3// Exhibit 12. A person uploads in storage's own page. The zoo proves the same path works: it asks
4// storage's /app/roundtrip to run the browser's upload (begin, presigned PUT to B2, commit, list), then
5// downloads what storage hands back and compares the bytes with what it sent.
6//     call_app("storage", "/app/roundtrip", true, "POST", bytes)      // rc.app_headers("storage", true)
7// The check does this as the robot, through our own /api/12/roundtrip.
8
9import "core:fmt"
10import "ratcloud:rc"
11
12Roundtrip :: struct {
13	size:    int `json:"size"`,
14	got:     int `json:"got"`,
15	matched: bool `json:"matched"`,
16	sha256:  string `json:"sha256"`,
17}
18
19roundtrip_once :: proc() -> (Roundtrip, string) {
20	bytes := random_bytes(4096 + random_int(0, 4096))
21	a := call_app("storage", "/app/roundtrip?path=zoo-roundtrip.bin", true, "POST", string(bytes), "application/octet-stream", 30_000)
22	r := parse(a.body)
23	if a.status != 200 || r == nil do return {}, fmt.tprintf("storage's roundtrip answered %d: %s%s", a.status, clip(a.body, 160), a.err)
24	if !jbool(r, "listed") do return {}, "uploaded, but the file was not in the listing"
25	// The download is a presigned B2 URL, fetched like a browser would: no assertion on it.
26	back := rc.fetch({url = jstr(r, "download"), follow = true, timeout_ms = 30_000})
27	if back.status != 200 do return {}, fmt.tprintf("the download answered %d", back.status)
28	sum := sha256(bytes)
29	return {len(bytes), len(back.body), eq_bytes(sum, sha256(back.body)), hex(sum)[:12]}, ""
30}
31
32e12 :: proc() -> Exhibit {
33	return {
34		n = 12, wing = "Storage",
35		title = "Your files",
36		promise = "You can upload a file, see it listed, and download it again unchanged.",
37		block = "storage keeps each person's files (B2-backed) behind their sign-in; an app links to it and never holds storage's keys. The check here uploads random bytes, lists them, downloads them and compares.",
38		src = {"e12_your_files.odin", #load("e12_your_files.odin", string)},
39		show = proc(me: ^rc.User) -> string {
40			d, s, found := stored_data(12, Roundtrip)
41			last := `<p class="muted">No matching roundtrip recorded yet.</p>`
42			if found && d.matched do last = fmt.tprintf(`<p>Last roundtrip, %s: <b>%d bytes</b> uploaded, listed and downloaded again: <span class="tag ok">matched</span></p>`, ago(s.at), d.size)
43			return cat(last, `<p><a href="`, h(rc.app_url("storage")), `/" target="_blank" rel="noopener">Open storage</a>, upload something, and download it again.</p>`)
44		},
45		api = proc(a: ^Api) -> bool {
46			if a.do_ != "roundtrip" || !a.post do return false
47			if a.me == nil {reply_err(a, 401, "sign in first"); return true}
48			r, err := roundtrip_once()
49			if err != "" {reply_err(a, 502, err); return true}
50			reply(a, r)
51			return true
52		},
53		// The robot asks the zoo to run the roundtrip as it (so the assertion names the robot).
54		check = proc() -> Check {
55			b, err := robot_at()
56			if err != "" do return fail(err)
57			b.timeout_ms = 40_000
58			r := post(b, self_url("/api/12/roundtrip"))
59			b.timeout_ms = 15_000
60			if r.status != 200 || r.j == nil do return fail(fmt.tprintf("the roundtrip failed (%d): %s", r.status, jstr(r.j, "error") if r.j != nil else clip(r.body, 120)))
61			j: Roundtrip
62			json_into(r.body, &j)
63			if !j.matched do return fail(fmt.tprintf("the downloaded copy differs from what was uploaded (%d bytes up, %d down)", j.size, j.got), j)
64			return pass(fmt.tprintf("uploaded %d random bytes, listed them, downloaded them again: matched (sha256 %s)", j.size, j.sha256), j)
65		},
66	}
67}