interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / research / tests / authors.sh

236 lines · 19225 bytes Raw Blame History
1#!/bin/sh
2# Sourced by tests/run.sh after T/build is compiled and T, C, gen, ok and bad exist.
3# No new compiler or host-language implementation: fixtures use the actual builder/publisher.
4mkdir -p "$T/authors" "$T/auth-reg/show" "$T/auth-reg/text"
5cat > "$T/authors/alice.tsv" <<'DATA'
6O id alice
7O name Alice <script> "&'
8O orcid 0000-0000-0000-0001
9O added-by did:key:zAlice
10O via zenodo
11O checked 2026-11-11
12W foreign-a 2026-11-11 preprint Venue <b> 10.1/x}evil{\%&#_$ 10.1/quoted'"&<> 1 2oops 0 cc-by-4.0 Foreign <script>alert(1)</script> "&'
13S foreign-a Summary & <img src=x onerror=alert(1)>
14A foreign-a Abstract </p><script>alert(2)</script>
15C foreign-a Alice <b> & Bob
16B foreign-a Alice }evil{\%&#_$
17K foreign-a <script>keyword</script>
18L foreign-a references http://example.org/insecure-link
19L foreign-a references javascript:alert(1)
20L foreign-a references https://example.org/"'&<>
21F foreign-a File <b> "&' 12bytes http://example.org/insecure-file
22F foreign-a File <b> "&' 12bytes https://example.org/file?x="'&<>
23P foreign-a zenodo did:key:zAlice 2026-11-11 https://zenodo.org/records/1 1
24DATA
25cat > "$T/authors/bob.tsv" <<'DATA'
26O id bob
27O name Bob Separate
28O orcid
29O added-by did:key:zBob
30O via radicle
31O checked 2026-11-11
32W foreign-b 2025-01-01 preprint Radicle 1 1 0 custom Bob metadata work
33S foreign-b Metadata without text
34A foreign-b A metadata-only abstract
35C foreign-b Bob Separate
36P foreign-b radicle did:key:zBob 2026-11-11 rad:zRepo 0
37G foreign-b 0123456789abcdef0123456789abcdef01234567 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
38DATA
39for id in alice bob; do
40 gen merge '' "$T/authors/$id.tsv" > "$T/authors/$id.merged"
41 cp "$T/authors/$id.tsv" "$T/auth-reg/show/$id.tsv"
42done
43cat "$C" "$T/authors/alice.merged" "$T/authors/bob.merged" > "$T/authors/catalog"
44ac=$T/authors/catalog
45gen authors '' "$ac" > "$T/authors/ids"
46is "$(cat "$T/authors/ids")" "$(printf 'drakestapleton\nalice\nbob')" 'authors returns validated separate IDs'
47is "$(grep -c '^O' "$T/authors/alice.merged" || :)" 0 'merge removes foreign O lines'
48grep -Fq "$(printf 'J\tforeign-a\talice')" "$T/authors/alice.merged" && ok 'merge joins work to author' || bad 'missing merge join'
49for id in alice bob drakestapleton; do gen author "$id" "$ac" > "$T/authors/$id.html"; done
50grep -Fq "/research/foreign-a'" "$T/authors/alice.html" && ok 'Alice page lists her work' || bad 'Alice work absent'
51is "$(grep -c '/research/foreign-b' "$T/authors/alice.html" || :)" 0 'Alice page excludes Bob work'
52grep -Fq "/research/foreign-b'" "$T/authors/bob.html" && ok 'Bob page lists his work' || bad 'Bob work absent'
53is "$(grep -c '/research/foreign-a' "$T/authors/bob.html" || :)" 0 'Bob page excludes Alice work'
54grep -Fq 'Alice &lt;script&gt; &quot;&amp;&#39;' "$T/authors/alice.html" && ok 'author HTML escaped' || bad 'author name not escaped'
55gen index '' "$ac" > "$T/authors/index.html"
56gen json '' "$ac" > "$T/authors/index.json"
57for id in alice bob; do
58 grep -Fq "/research/authors/$id'" "$T/authors/index.html" && ok "hub links $id" || bad "hub missing $id"
59 grep -Fq "\"author_ids\":[\"$id\"]" "$T/authors/index.json" && ok "JSON joins $id" || bad "JSON missing $id"
60done
61for mode in work wjson wbib; do gen "$mode" foreign-a "$ac" > "$T/authors/$mode"; done
62is "$(grep -Eic '<script|<img|<b>' "$T/authors/work" || :)" 0 'foreign markup is data, not elements'
63grep -Fq '&lt;script&gt;alert(1)&lt;/script&gt;' "$T/authors/work" && ok 'foreign title escaped in HTML' || bad 'foreign title missing'
64grep -Fq '"title":"Foreign <script>alert(1)</script> \"' "$T/authors/wjson" && ok 'JSON quotes foreign title' || bad 'JSON title escaping'
65is "$(grep -Ec 'http://|javascript:' "$T/authors/work" "$T/authors/wjson" | cut -d: -f2 | sort -u)" 0 'foreign HTML and JSON omit non-https links'
66grep -Fq '"bytes":12' "$T/authors/wjson" && ok 'foreign file bytes numeric' || bad 'foreign file bytes invalid'
67grep -Fq '"versions":2,' "$T/authors/wjson" && ok 'foreign versions numeric' || bad 'foreign versions invalid'
68is "$(grep -c '}evil{' "$T/authors/wbib" || :)" 0 'foreign DOI and author cannot close BibTeX values'
69grep -Fq 'doi = {10.1/x evil \%\&\#\_\$}' "$T/authors/wbib" && ok 'foreign DOI BibTeX punctuation escaped' || bad 'foreign DOI escaping wrong'
70gen corpus '' "$ac" > "$T/authors/corpus-list"
71grep -q '^foreign-a' "$T/authors/corpus-list" && ok 'open P admits foreign text' || bad 'open text missing'
72is "$(grep -c '^foreign-b' "$T/authors/corpus-list" || :)" 0 'closed P excludes foreign text'
73sed '/^P[[:space:]]foreign-a/d' "$ac" > "$T/authors/no-p"
74gen corpus '' "$T/authors/no-p" > "$T/authors/no-p-list"
75is "$(grep -c '^foreign-a' "$T/authors/no-p-list" || :)" 0 'missing P fails closed for text'
76for mode in add doi; do
77 gen "$mode" '' "$ac" > "$T/authors/$mode.html"
78 is "$(grep -Eic '<script|style=|\b(every|everything|everyone|everybody|never|always|online)\b' "$T/authors/$mode.html" || :)" 0 "$mode has no scripts, inline style or banned copy"
79done
80# Requesters. A Zenodo record is public metadata that a Radicle key requested: the key gains no
81# ownership, is labelled Requester, and is never displayed as an author. Radicle keeps "Added through".
82zdisc='Zenodo records listed here are public metadata, requested by a key. Requesting a record gives that key no ownership of the record or the work, and the registry makes no claim that the key belongs to the person named on this page.'
83wdisc='This Zenodo record is public metadata, requested by a key. Requesting a record gives that key no ownership of the record or the work, and the registry makes no claim that the key belongs to a person named on this page.'
84# reqpage FILE DISCLAIMER KEY prints the first failed reason; a passing page prints nothing.
85reqpage() {
86 grep -Fq "$2" "$1" || { echo 'Zenodo public-metadata disclaimer missing'; return; }
87 grep -Fq "Requester: key $3" "$1" || { echo "requester $3 not labelled"; return; }
88 ! grep -Fq 'Added by' "$1" || { echo 'requester shown as Added by'; return; }
89 [ "$(grep -oF "$3" "$1" | wc -l)" = "$(grep -oF "Requester: key $3" "$1" | wc -l)" ] || { echo "requester $3 shown outside its label"; return; }
90}
91reqfix() {
92 printf 'O\tid\t%s\nO\tname\t%s\nO\torcid\t\nO\tadded-by\t%s\nO\tvia\tzenodo\nO\tchecked\t2026-11-11\n' "$1" "$2" "$3"
93 printf 'W\t%s\t\t2025-02-02\tpreprint\tZenodo\t\t\t1\t1\t0\tcustom\t%s metadata work\n' "$5" "$2"
94 printf 'S\t%s\tPublic metadata\nA\t%s\tA requested abstract\nC\t%s\t%s\n' "$5" "$5" "$5" "$2"
95 printf 'P\t%s\tzenodo\t%s\t2026-11-11\thttps://zenodo.org/records/%s\t0\n' "$5" "$3" "$4"
96}
97reqfix dana 'Dana Writer' did:key:zKeyOne 2 foreign-d > "$T/authors/dana.tsv"
98reqfix erin 'Erin Writer' did:key:zKeyTwo 3 foreign-e > "$T/authors/erin.tsv"
99for id in dana erin; do gen merge '' "$T/authors/$id.tsv" > "$T/authors/$id.merged"; done
100cat "$C" "$T/authors/dana.merged" "$T/authors/erin.merged" > "$T/authors/req-catalog"
101rq=$T/authors/req-catalog
102is "$(gen authors '' "$rq")" "$(printf 'drakestapleton\ndana\nerin')" 'two requester keys add no author IDs'
103gen json '' "$rq" > "$T/authors/req-index.json"
104gen index '' "$rq" > "$T/authors/req-index.html"
105reqcase() { # author-id author-name key other-key work-slug
106 gen author "$1" "$rq" > "$T/authors/req-$1.html"
107 gen work "$5" "$rq" > "$T/authors/req-$5.html"
108 gen wjson "$5" "$rq" > "$T/authors/req-$5.json"
109 is "$(reqpage "$T/authors/req-$1.html" "$zdisc" "$3")" '' "author page $1 says public metadata, labels requester $3, disclaims ownership"
110 is "$(reqpage "$T/authors/req-$5.html" "$wdisc" "$3")" '' "work page $5 says public metadata, labels requester $3, disclaims ownership"
111 grep -Fq "<p class='by'><a href='/research/authors/$1'>$2</a></p>" "$T/authors/req-$5.html" && ok "work $5 byline names author $1, not the requester" || bad "work $5 byline lost author $1"
112 is "$(cat "$T/authors/req-$1.html" "$T/authors/req-$5.html" | grep -cF "$4" || :)" 0 "pages for $1 omit the other requester $4"
113 grep -Fq "\"provenance\":{\"how\":\"zenodo\",\"added_by\":\"$3\",\"requester\":\"$3\",\"requester_role\":\"requested public Zenodo metadata; no ownership or identity claim\",\"checked\":\"2026-11-11\"," "$T/authors/req-$5.json" && ok "work JSON $5 keeps added_by and adds requester" || bad "work JSON $5 requester fields wrong"
114 grep -Fq "{\"id\":\"$1\",\"name\":\"$2\",\"orcid\":\"\",\"added_by\":\"$3\",\"via\":\"zenodo\",\"checked\":\"2026-11-11\",\"requester\":\"$3\",\"requester_role\":\"requested public Zenodo metadata; no ownership or identity claim\"}" "$T/authors/req-index.json" && ok "hub JSON $1 keeps legacy fields and adds requester" || bad "hub JSON $1 requester fields wrong"
115}
116reqcase dana 'Dana Writer' did:key:zKeyOne did:key:zKeyTwo foreign-d
117reqcase erin 'Erin Writer' did:key:zKeyTwo did:key:zKeyOne foreign-e
118is "$(grep -o '"author_ids":\[[^]]*\]' "$T/authors/req-index.json" | grep -c 'did:key' || :)" 0 'hub JSON lists no requester key as an author'
119is "$(grep -o '"name":"[^"]*"' "$T/authors/req-index.json" | grep -c 'did:key' || :)" 0 'hub JSON names no requester key'
120is "$(grep -c "/research/authors/did:" "$T/authors/req-index.html" || :)" 0 'hub HTML links no requester key as an author'
121python3 -I -c 'import json,sys; [json.load(open(f, encoding="utf-8")) for f in sys.argv[1:]]' "$T/authors/req-index.json" "$T/authors/req-foreign-d.json" "$T/authors/req-foreign-e.json" && ok 'requester JSON parses' || bad 'requester JSON invalid'
122is "$(reqpage "$T/authors/alice.html" "$zdisc" did:key:zAlice)" '' 'Alice Zenodo author page labels requester and disclaims ownership'
123is "$(reqpage "$T/authors/work" "$wdisc" did:key:zAlice)" '' 'Alice Zenodo work page labels requester and disclaims ownership'
124gen work foreign-b "$ac" > "$T/authors/req-radicle.html"
125grep -Fq 'Added through the shelf by key did:key:zBob' "$T/authors/req-radicle.html" && ok 'shelf work keeps its brought-by provenance, without the retired network name' || bad 'shelf provenance wording changed'
126is "$(cat "$T/authors/bob.html" "$T/authors/req-radicle.html" | grep -c 'Zenodo record' || :)" 0 'Radicle pages carry no Zenodo disclaimer'
127grep -Fq '"requester":"did:key:zBob","requester_role":"brought the repository to the shelf; not verified"}' "$T/authors/index.json" && ok 'hub JSON keeps Radicle meaning: brought, not verified' || bad 'Radicle requester role wrong'
128# Negative control: a builder whose two disclaimers are blanked must fail the same oracle for that reason,
129# and its pages must differ from the real ones only by the removed section.
130sed 's/^text \(mazdisc\|mazwork\) .*/text \1 "\\n"/' research/build.om > "$T/authors/nodisc.om"
131is "$(grep -cxF -e 'text mazdisc "\n"' -e 'text mazwork "\n"' "$T/authors/nodisc.om" || :)" 2 'control blanks both Zenodo disclaimers'
132if "$OMC" < "$T/authors/nodisc.om" > "$T/authors/nodisc" && chmod 700 "$T/authors/nodisc"; then
133 { printf 'M\tauthor\tdana\n'; cat "$rq"; } | "$T/authors/nodisc" > "$T/authors/nodisc-dana.html"
134 { printf 'M\twork\tforeign-d\n'; cat "$rq"; } | "$T/authors/nodisc" > "$T/authors/nodisc-foreign-d.html"
135 is "$(reqpage "$T/authors/nodisc-dana.html" "$zdisc" did:key:zKeyOne)" 'Zenodo public-metadata disclaimer missing' 'control author page fails for its missing disclaimer'
136 is "$(reqpage "$T/authors/nodisc-foreign-d.html" "$wdisc" did:key:zKeyOne)" 'Zenodo public-metadata disclaimer missing' 'control work page fails for its missing disclaimer'
137 is "$(python3 -I -c 'import sys
138r = open(sys.argv[1], encoding="utf-8").read(); m = open(sys.argv[2], encoding="utf-8").read()
139d = "<section class='"'"'card'"'"'><h2>" + sys.argv[4] + "</h2><p>" + sys.argv[3] + "</p></section>"
140print(int(r.count(d) == 1 and m == r.replace(d, "")))' "$T/authors/req-dana.html" "$T/authors/nodisc-dana.html" "$zdisc" 'Zenodo records')$(python3 -I -c 'import sys
141r = open(sys.argv[1], encoding="utf-8").read(); m = open(sys.argv[2], encoding="utf-8").read()
142d = "<section class='"'"'card'"'"'><h2>" + sys.argv[4] + "</h2><p>" + sys.argv[3] + "</p></section>"
143print(int(r.count(d) == 1 and m == r.replace(d, "")))' "$T/authors/req-foreign-d.html" "$T/authors/nodisc-foreign-d.html" "$wdisc" 'Zenodo record')" 11 'control pages differ from the real pages only by the disclaimer'
144else
145 bad 'disclaimer control builder does not compile'
146fi
147# Frozen baseline, including when foreign authors share the same catalog.
148mkdir -p "$T/authors/legacy"
149while IFS= read -r s; do
150 gen work "$s" "$ac" > "$T/authors/legacy/$s.html"
151 gen wjson "$s" "$ac" > "$T/authors/legacy/$s.json"
152 gen wbib "$s" "$ac" > "$T/authors/legacy/$s.bib"
153done < "$T/slugs"
154# The hub changes with the author registry; the immutable contract covers work documents.
155basefile=$T/authors/work-sha256.txt
156grep -Ev " research\.(html|json|bib|txt|css)$" research/tests/fixtures/base-sha256.txt > "$basefile"
157(cd "$T/authors/legacy" && sha256sum -c "$basefile") > "$T/authors/base-check" 2>&1 && ok '16 legacy HTML JSON Bib documents remain byte exact' || bad 'legacy document hash drift'
158# Source snapshot: G is shown for the foreign work that carries it, and refused elsewhere.
159gcommit=0123456789abcdef0123456789abcdef01234567
160gsha=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
161gen work foreign-b "$ac" > "$T/authors/g.html"
162gen wjson foreign-b "$ac" > "$T/authors/g.json"
163grep -Fq "Repository commit: <code>$gcommit</code>. Paper SHA-256: <code>$gsha</code>" "$T/authors/g.html" && ok "G commit and paper digest shown on foreign page" || bad "G snapshot missing from page"
164grep -Fq "\"open_text\":false,\"source_commit\":\"$gcommit\",\"paper_sha256\":\"$gsha\"}" "$T/authors/g.json" && ok "G commit and paper digest in provenance JSON" || bad "G snapshot missing from JSON"
165python3 -I -c "import json,sys; json.load(open(sys.argv[1]))" "$T/authors/g.json" && ok "G work JSON parses" || bad "G work JSON invalid"
166is "$(grep -c "source_commit\|Source snapshot" "$T/authors/work" "$T/authors/wjson" | cut -d: -f2 | sort -u)" 0 "work without G shows no snapshot"
167first=$(head -n 1 "$T/slugs")
168gline=$(printf "G\tforeign-b\t%s\t%s" "$gcommit" "$gsha")
169for gcase in upper short extra legacy duplicate; do
170 case "$gcase" in
171 upper) sed "s/^G\tforeign-b\t0123456789abcdef/G\tforeign-b\t0123456789ABCDEF/" "$ac" > "$T/authors/g-$gcase" ;;
172 short) sed "s/^G\tforeign-b\t$gcommit/G\tforeign-b\t0123456789abcdef0123456789abcdef0123456/" "$ac" > "$T/authors/g-$gcase" ;;
173 extra) sed "s/^G\tforeign-b\t.*/&\textra/" "$ac" > "$T/authors/g-$gcase" ;;
174 legacy) { cat "$ac"; printf "G\t%s\t%s\t%s\n" "$first" "$gcommit" "$gsha"; } > "$T/authors/g-$gcase" ;;
175 duplicate) { cat "$ac"; printf "%s\n" "$gline"; } > "$T/authors/g-$gcase" ;;
176 esac
177 rc=0; gen wjson foreign-b "$T/authors/g-$gcase" > "$T/authors/g-$gcase.out" || rc=$?
178 is "$rc" 4 "G $gcase refused"
179 is "$(wc -c < "$T/authors/g-$gcase.out" | tr -d " ")" 0 "G $gcase emits no document"
180done
181# Refusal cases: duplicate work and a path traversal author id produce no document.
182cat "$ac" "$T/authors/alice.merged" > "$T/authors/duplicate"
183rc=0; gen index '' "$T/authors/duplicate" > "$T/authors/refused" || rc=$?
184is "$rc" 4 'duplicate catalog work refused'
185is "$(wc -c < "$T/authors/refused" | tr -d ' ')" 0 'duplicate refusal emits no document'
186sed 's/^O\tid\talice$/O\tid\t..\/alice/' "$T/authors/alice.tsv" > "$T/authors/bad-id"
187rc=0; gen merge '' "$T/authors/bad-id" > "$T/authors/refused" || rc=$?
188is "$rc" 4 'path traversal author refused'
189# Publication consumes only gathered show/ files, quotes text and prunes its own outputs.
190cp "$C" "$T/auth-reg/catalog.tsv"
191cp research/registry/text/*.txt "$T/auth-reg/text/"
192printf 'First paragraph.\n@@\tforged-source\tFake title\nThird paragraph.\n' > "$T/auth-reg/text/foreign-a.txt"
193textsha=$(sha256sum "$T/auth-reg/text/foreign-a.txt" | cut -d' ' -f1)
194mv "$T/auth-reg/text/foreign-a.txt" "$T/auth-reg/text/foreign-a.$textsha.txt"
195printf 'T\tforeign-a\t%s\n' "$textsha" >> "$T/auth-reg/show/alice.tsv"
196printf 'This closed text must not enter the corpus.\n' > "$T/auth-reg/text/foreign-b.txt"
197mkdir -p "$T/auth-site/ask" "$T/auth-reg/a/not-gathered"
198printf 'keep answer\n' > "$T/auth-site/ask/keep.html"
199cp "$T/authors/alice.tsv" "$T/auth-reg/a/not-gathered/unfiltered.tsv"
200if sh research/publish.sh "$T/auth-site" "$T/auth-corpus" "$T/auth-reg" > "$T/authors/publish.log" 2>&1; then
201 ok 'publishes foreign metadata-only work without requiring its text'
202 [ -f "$T/auth-site/w/foreign-b.html" ] && ok 'metadata-only page exists' || bad 'metadata-only page missing'
203 is "$(grep -c '^@@' "$T/auth-corpus" || :)" 17 'corpus contains only registry-authoritative headers'
204 grep -Fq "$(printf '> @@\tforged-source\tFake title')" "$T/auth-corpus" && ok 'foreign planted corpus header quoted' || bad 'foreign corpus header unquoted'
205 is "$(grep -c '^@@.*foreign-b' "$T/auth-corpus" || :)" 0 'closed foreign text not published'
206else
207 bad "foreign publication failed: $(cat "$T/authors/publish.log")"
208fi
209# Simulate upstream takedown; raw a/ still contains the removed record.
210rm "$T/auth-reg/show/alice.tsv" "$T/auth-reg/show/bob.tsv"
211if sh research/publish.sh "$T/auth-site" "$T/auth-corpus" "$T/auth-reg" > "$T/authors/prune.log" 2>&1; then
212 ok 'republish after upstream takedown'
213 leftovers=$(find -L "$T/auth-site/w" "$T/auth-site/a" -type f \( -name 'foreign-*' -o -name alice.html -o -name bob.html \) | wc -l | tr -d ' ')
214 is "$leftovers" 0 'stale foreign docs and author pages removed'
215 is "$(grep -c 'foreign-a\|forged-source\|foreign-b' "$T/auth-corpus" || :)" 0 'stale foreign corpus removed'
216 is "$(cat "$T/auth-site/ask/keep.html")" 'keep answer' 'pruning preserves ask answers'
217 (cd "$T/auth-site/w" && sha256sum -c "$basefile") > "$T/authors/prune-base" 2>&1 && ok 'pruning preserves legacy document bytes' || bad 'pruning changed legacy documents'
218else
219 bad "prune publication failed: $(cat "$T/authors/prune.log")"
220fi
221# Refuse a full registry instead of emitting a document from a truncated prefix.
222{ printf 'M\tindex\n'; cat "$C"; dd if=/dev/zero bs=262144 count=1 2>/dev/null; } > "$T/authors/oversize"
223rc=0; "$T/build" < "$T/authors/oversize" > "$T/authors/oversize.out" || rc=$?
224is "$rc" 4 'oversize merged registry refused'
225is "$(wc -c < "$T/authors/oversize.out" | tr -d ' ')" 0 'oversize registry produces no document'
226# Failed publication must not replace the last accepted corpus or legacy docs.
227cp "$T/auth-corpus" "$T/authors/accepted-corpus"
228cp "$T/authors/alice.tsv" "$T/auth-reg/show/alice.tsv"
229dd if=/dev/zero bs=4194304 count=1 2>/dev/null | tr '\000' x > "$T/authors/oversize-text"
230textsha=$(sha256sum "$T/authors/oversize-text" | cut -d' ' -f1)
231cp "$T/authors/oversize-text" "$T/auth-reg/text/foreign-a.$textsha.txt"
232printf 'T\tforeign-a\t%s\n' "$textsha" >> "$T/auth-reg/show/alice.tsv"
233rc=0; sh research/publish.sh "$T/auth-site" "$T/auth-corpus" "$T/auth-reg" > "$T/authors/oversize-corpus.log" 2>&1 || rc=$?
234is "$rc" 1 'oversize corpus refused'
235grep -q 'corpus exceeds 4194303 bytes' "$T/authors/oversize-corpus.log" && ok 'oversize corpus fails for capacity, not cache identity' || bad 'oversize corpus wrong refusal'
236cmp -s "$T/authors/accepted-corpus" "$T/auth-corpus" && ok 'oversize corpus leaves accepted corpus intact' || bad 'failed publication replaced corpus'