diff --git a/.claude/skills/coredesktop-security-checks/SKILL.md b/.claude/skills/coredesktop-security-checks/SKILL.md new file mode 100644 index 0000000..7d0225d --- /dev/null +++ b/.claude/skills/coredesktop-security-checks/SKILL.md @@ -0,0 +1,140 @@ +--- +name: coredesktop-security-checks +description: Plan and progress for CoreDesktop's security test suite. The report receiver and report's redaction are tested; the checks still to write run in the smoketest VM (the agent sandbox, network exposure, what root runs, the install path). Use when resuming, continuing, checking the status of, or changing CoreDesktop's security or penetration tests. +--- + +# CoreDesktop's security checks + +Written on 2026-11-16. Work out the current state before doing anything: + +``` +git -C ~/Projects/coredesktop fetch && git -C ~/Projects/coredesktop log --oneline origin/main -15 +ls ~/Projects/coredesktop/smoketest/scenarios/ ~/Projects/coredesktop/server/report-receiver/ +``` + +Tell the user which step is next and confirm before starting it. + +## Scope + +The suite covers what CoreDesktop itself adds. It doesn't test Arch or GNOME, +which upstream looks after. It goes after the places where a mistake would +leak data or give away control, in this order: + +1. The report receiver, the only part on the public internet. **Done.** +2. `report`'s redaction, which decides what leaves a user's computer. **Done.** +3. The agent sandbox: the one place that runs code CoreDesktop doesn't trust. +4. Network exposure from outside. +5. Files root runs, and secrets only root may read. +6. The install path (federated.sh, the `[federated]` repo). + +The pacman guard (`system/pacman-guard`) is +a safety net against mistakes, not a security control: anyone with sudo gets +past it by design. Don't test it as a boundary. + +## Done: steps 1 and 2 (the commit "Test the report receiver and redaction against attacks") + +- `server/report-receiver/test_security.py` sends the receiver what an + attacker could. It found, and the commit fixed: + - a negative Content-Length (read without limit) + - parts nested thousands deep (RecursionError, connection dropped) + - thousands of parts (8 s of CPU for 5.7 MB) + - a rate limit per IPv6 address instead of per /64 + - Markdown in report fields reaching the Gitea issue: @mentions, issue + references, links, tracking images + - no guard against filling the disk +- `tests/report.bats` (the "Redaction" section) found `report` sending: + - passwords in URLs + - tokens, API keys, Nextcloud app passwords and private keys + - IPv6 and MAC addresses + - the Core's domain and the user's name + - user and host names with regex characters, unredacted + + All fixed. A test also checks it doesn't over-redact (pacman's `::`, times, + epoch versions, `std::string`). +- Each test was checked to fail against the code before the fix. + +Still open from these steps: +- **Deploy** the new `receiver.py` on federated.sh (server/report-receiver/README.md, "To update it later"). +- **Slow uploads.** Caddy streams them to the receiver, one thread each. Try + `request_buffers 11MB` in Caddyfile.example's `reverse_proxy`, and check + that `caddy validate` accepts it. +- **Gitea attachments.** Check on the real server that Gitea serves attached + `.log` files as text or as downloads, never as HTML (a log can contain + `", + "kernel": b"`` @david `` ![x](https://evil.example/i.png)", + "siblings": b"connector 1.0\n@everyone 2.0\n| --- |", + } + self.assertEqual(self.post(report(**attacks))[0], 200) + issue = self.filed() + table = [line for line in issue["body"].split("\n") if line.startswith("| ")] + for line in table[2:]: + value = line.split(" | ", 1)[1].rsplit(" |", 1)[0] + if value == "–": + continue + # Each line of a value is one inline code span, and nothing else + for piece in value.split("
"): + self.assertRegex(piece, r"^(`+)( ?)[^`]*(?:`(?!\1)[^`]*)*\2\1$", line) + self.assertNotIn("\n@", issue["body"]) + self.assertNotIn(" @", issue["title"]) + self.assertNotIn("#", issue["title"]) + self.assertNotIn("<", issue["title"]) + self.assertNotIn("](", issue["title"]) + + def test_the_log_cant_break_out_of_its_code_block(self): + log = b"ok\n```\n@david [click](https://evil.example)\n````\n\n" + self.assertEqual(self.post(report(log=log))[0], 200) + body = self.filed()["body"] + fence = re.search(r"\n(`{3,})\n", body).group(1) + start = body.index(f"\n{fence}\n") + len(fence) + 2 + end = body.index(f"\n{fence}\n", start) + self.assertIn("@david", body[start:end]) + self.assertNotIn("@david", body[:start] + body[end:]) + + def test_control_characters_and_direction_overrides_dont_reach_the_title(self): + failed = "10-pacman.sh\x1b[31m‮gnp.exe​\r".encode() + self.assertEqual(self.post(report(failed=failed))[0], 200) + title = self.filed()["title"] + self.assertNotRegex(title, r"[\x00-\x1f\x7f​-‏‪-‮⁦-⁩]") + self.assertTrue(title.startswith("Update failed in 10-pacman.sh")) + + def test_an_unknown_kind_cant_change_the_attachments_name(self): + self.assertEqual(self.post(report(kind=b"../../etc/passwd"))[0], 200) + self.filed() + attachment = self.gitea.attachments[-1] + self.assertEqual(self.gitea.issues[-1]["title"].split()[0], "Other") + report_id = next(p.name for p in (self.tmp / "state").iterdir()) + self.assertEqual(attachment["path"], "/api/v1/repos/o/r/issues/1/assets") + self.assertTrue((self.tmp / "state" / report_id / "log.txt").exists()) + + def test_the_reply_is_only_an_id_and_never_the_gitea_token(self): + status, body = self.post(report()) + self.assertEqual(status, 200) + self.assertRegex(body, r"^CD-[2-9A-HJ-NP-Z]{6}\n$") + self.filed() + self.assertNotIn("secret", self.stderr.getvalue()) + + +class TextTest(unittest.TestCase): + def test_code_survives_backticks_at_either_end(self): + self.assertEqual(receiver.code("a"), "`a`") + self.assertEqual(receiver.code("a`b"), "``a`b``") + self.assertEqual(receiver.code("`a``"), "``` `a`` ```") + + def test_title_text_keeps_step_names_and_versions(self): + text = "71-siblings.sh, git pull, install.sh (2026.11.16 (5d7db2c)) 1.2+3:4" + self.assertEqual(receiver.title_text(text), text) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/report.bats b/tests/report.bats index ddb7fed..2b9d73b 100644 --- a/tests/report.bats +++ b/tests/report.bats @@ -159,3 +159,111 @@ field() { [ "$status" -eq 0 ] [[ "$output" == *"Your report's ID is CD-7Q2K;"* ]] } + +# --- Redaction: what mustn't leave this computer ------------------------------- + +# view LINE...: what report would send for a log of these lines +view() { + printf '%s\n' "$@" > "$LOG" + run bash "$REPO/report" --view "$LOG" + [ "$status" -eq 0 ] +} + +# sent_as LINE...: the output of the last view is exactly these lines +sent_as() { + [ "$output" = "$(printf '%s\n' "$@")" ] || { printf 'got:\n%s\n' "$output" >&2; return 1; } +} + +@test "redaction takes passwords out of URLs" { + view "Cloning https://bob:s3cr3t!pw@gitea.example.com/x.git" \ + "rclone: http://bob:hunter2@localhost:8080/dav/" \ + "fetch ssh://git@github.com/x.git and https://github.com/x.git" + sent_as "Cloning https://@gitea.example.com/x.git" \ + "rclone: http://@localhost:8080/dav/" \ + "fetch ssh://@github.com/x.git and https://github.com/x.git" +} + +@test "redaction takes out tokens, passwords and API keys" { + view "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.c2lnbmF0dXJl" \ + "curl -H 'Authorization: Basic Ym9iOmh1bnRlcjI='" \ + "password=hunter2 PASSWORD: hunter3 passphrase='correct horse' api_key: abc123" \ + "GET /callback?code=1&access_token=ya29.a0AfH6&state=2" \ + 'export ANTHROPIC_API_KEY="sk-ant-api03-AbCdEfGhIjKlMnOp-QrStUv"' \ + "keys sk-proj-abcdefghijklmnopqrstuvwx ghp_abcdefghijklmnopqrstuvwxyz0123456789" \ + "github_pat_11ABCDEFG0123456789_abcdefghijklmnop glpat-abcdefghijklmnopqrst" \ + "xoxb-1234567890-abcdefghij tskey-auth-kAbCdEf1CNTRL-xyz AKIAIOSFODNN7EXAMPLE hf_abcdefghijklmnopqrstuvwx" \ + "app password Ab3dE-fG4hI-jK5lM-nO6pQ-rS7tU in use" + sent_as "Authorization: Bearer " \ + "curl -H 'Authorization: Basic '" \ + "password= PASSWORD: passphrase=' horse' api_key: " \ + "GET /callback?code=1&access_token=&state=2" \ + 'export ANTHROPIC_API_KEY=""' \ + "keys " \ + " " \ + " " \ + "app password in use" +} + +@test "redaction takes out private keys whole" { + view "Adding key:" "-----BEGIN OPENSSH PRIVATE KEY-----" "b3BlbnNzaC1rZXktdjEAAAAA" \ + "AAAAC3NzaC1lZDI1NTE5AAAA" "-----END OPENSSH PRIVATE KEY-----" "done" \ + "-----BEGIN RSA PRIVATE KEY-----" "MIIEpAIBAAKCAQEA" "-----END RSA PRIVATE KEY-----" + sent_as "Adding key:" "" "done" "" +} + +@test "redaction takes out IPv6 and MAC addresses" { + view "inet6 2001:db8:85a3::8a2e:370:7334/64 scope global" \ + "tailscale fd7a:115c:a1e0::1 and fd7a:115c:a1e0:ab12:4843:cd96:6258:b240" \ + "link fe80::1ff:fe23:4567:890a%wlan0, loopback ::1" \ + "full 2001:0db8:0000:0000:0000:ff00:0042:8329." \ + "[2001:db8::7]:443 ether 3c:22:fb:12:34:56 brd ff:ff:ff:ff:ff:ff" + sent_as "inet6 scope global" \ + "tailscale and " \ + "link %wlan0, loopback " \ + "full ." \ + "[]:443 ether brd " +} + +@test "redaction keeps what only looks like an address or a secret" { + view ":: Synchronizing package databases..." \ + "[2026-11-08T11:03:07-0500] [ALPM] upgraded foo (1:2.3-1 -> 1:2.4-1)" \ + "std::string Foo::Bar::baz() at 12:34:56" \ + "[sudo] password for alice: " "Password: " \ + "sha256 e3b0c44298fc1c14 tokens: 5 secrets.toml" \ + "Bearer token missing" "sk-1 risk-assessment hf_x" + sent_as ":: Synchronizing package databases..." \ + "[2026-11-08T11:03:07-0500] [ALPM] upgraded foo (1:2.3-1 -> 1:2.4-1)" \ + "std::string Foo::Bar::baz() at 12:34:56" \ + "[sudo] password for : " "Password: " \ + "sha256 e3b0c44298fc1c14 tokens: 5 secrets.toml" \ + "Bearer token missing" "sk-1 risk-assessment hf_x" +} + +@test "redaction takes out your Cores' domains and your name, whatever their case" { + mkdir -p "$HOME/.config/coredesktop/cores" + printf 'CORE_DOMAIN=%q\nCORE_EMAIL=%q\nCORE_NAME=%q\n' acme.org alice@acme.org "Alice Smith (Acme)" \ + > "$HOME/.config/coredesktop/cores/acme.org.conf" + cp "$HOME/.config/coredesktop/cores/acme.org.conf" "$HOME/.config/coredesktop/core.conf" + printf 'CORE_DOMAIN=%q\nCORE_NAME=%q\n' my-core.example "Al O'Neil" > "$HOME/.config/coredesktop/cores/my-core.example.conf" + view "Mounting https://nextcloud.acme.org/remote.php for Alice Smith (Acme)" \ + "headscale.ACME.ORG and my-core.example, mail alice@acme.org, Al O'Neil" \ + "notacme.org.uk and acme.organic stay" + sent_as "Mounting https://nextcloud./remote.php for " \ + "headscale. and , mail , " \ + "notacme.org.uk and acme.organic stay" +} + +@test "a Core's settings can't run anything when report reads them" { + mkdir -p "$HOME/.config/coredesktop/cores" + printf 'CORE_DOMAIN=acme.org\ntouch "%s/ran"\n' "$BATS_TEST_TMPDIR" > "$HOME/.config/coredesktop/cores/acme.org.conf" + view "acme.org" + sent_as "" + [ ! -e "$BATS_TEST_TMPDIR/ran" ] +} + +@test "a user or host name with regular expression characters is still replaced" { + export USER='a.b+c' + stub uname '[ "$1" = -n ] && echo "lab(1)" || /usr/bin/uname "$@"' + view "a.b+c on lab(1); axbbc on lab1" + sent_as " on ; axbbc on lab1" +}