c1d80cc / test/webui.sh
6700 bytes
#!/bin/sh
set -eu
cli="$(git rev-parse --show-toplevel)/anbot.mjs"
scratch=$(mktemp -d)
port=${ANBOT_TEST_WEBUI_PORT:-18790}
server=
cleanup() {
if [ -n "$server" ]; then
kill "$server" 2>/dev/null || :
wait "$server" 2>/dev/null || :
fi
rm -r "$scratch"
}
trap cleanup EXIT
trap 'exit 1' INT TERM
git init -q -b main --object-format=sha1 "$scratch/source"
printf '<b>bold</b> & "quotes"\n' >"$scratch/source/README.md"
mkdir "$scratch/source/sub"
printf 'nested file\n' >"$scratch/source/sub/file.txt"
printf 'a\000b\000c' >"$scratch/source/binary.bin"
git -C "$scratch/source" add .
git -C "$scratch/source" -c user.name=Test -c user.email=test@example.invalid commit -q -m 'Test <script>alert(1)</script> escaping'
git -C "$scratch/source" -c user.name=Test -c user.email=test@example.invalid commit -q --allow-empty -m 'Second commit'
head=$(git -C "$scratch/source" rev-parse HEAD)
parent=$(git -C "$scratch/source" rev-parse HEAD~1)
node "$cli" publish --data "$scratch/node" --source "$scratch/source" --branch main --name webui --port "$port" >/dev/null
node "$cli" serve --data "$scratch/node" --port "$port" >"$scratch/url" &
server=$!
for attempt in 1 2 3 4 5; do
if test -s "$scratch/url"; then break; fi
sleep 1
done
url=$(cat "$scratch/url")
base=${url%%/git/*}
node --input-type=module - "$base" <<'JS'
import assert from 'node:assert/strict'
const response = await fetch(process.argv[2] + '/feed')
assert.equal(response.status, 200)
assert.match(await response.text(), /No posts yet/)
JS
author=$(node "$cli" identity --data "$scratch/node")
other=$(node "$cli" identity --data "$scratch/peer")
post=$(node "$cli" post --data "$scratch/node" --text 'Hello <script>alert(1)</script>
Second line' |
node -pe 'JSON.parse(require("node:fs").readFileSync(0)).id')
node "$cli" post --data "$scratch/peer" --reply-to "$post" --text 'Reply from another author' >/dev/null
cp "$scratch/peer/posts/"* "$scratch/node/posts/"
if node "$cli" post --data "$scratch/node" --text ' ' >/dev/null 2>&1; then exit 1; fi
if node "$cli" post --data "$scratch/node" --text 'Bad reply' --reply-to 'invalid' >/dev/null 2>&1; then exit 1; fi
node --input-type=module - "$base" "$head" "$parent" "$author" "$other" "$post" <<'JS'
import assert from 'node:assert/strict'
import http from 'node:http'
const [, , base, head, parent, author, other, post] = process.argv
async function get(path) {
const response = await fetch(base + path)
return { status: response.status, type: response.headers.get('content-type'), text: await response.text() }
}
function getRaw(path) {
const { hostname, port } = new URL(base)
return new Promise((resolve, reject) => {
const request = http.get({ hostname, port, path, method: 'GET' }, response => {
let text = ''
response.on('data', chunk => { text += chunk })
response.on('end', () => resolve({ status: response.statusCode, text }))
})
request.on('error', reject)
})
}
function checkHtml(page, live = false) {
assert.equal(page.status, 200)
assert.match(page.type, /^text\/html/)
const scripts = page.text.match(/<script\b[^>]*>/g) ?? []
assert.deepEqual(scripts, live ? ['<script src="./feed-live.js" defer>'] : [])
}
const index = await get('/')
checkHtml(index)
assert.match(index.text, /<h1>webui<\/h1>/)
assert.match(index.text, /Clone URL/)
assert.match(index.text, /href="\.\/feed"/)
assert.match(index.text, /<script>/)
assert.match(index.text, /\/blob\/README\.md/)
assert.match(index.text, /\/tree\/sub/)
assert.match(index.text, new RegExp(`/commit/${head}`))
assert.match(index.text, new RegExp(`/commit/${parent}`))
assert.doesNotMatch(index.text, /\/commit\/\n/)
assert.match(index.text, /<td>23<\/td>/)
const tree = await get('/tree')
checkHtml(tree)
assert.match(tree.text, /README\.md/)
assert.match(tree.text, /sub\//)
const subtree = await get('/tree/sub')
checkHtml(subtree)
assert.match(subtree.text, /file\.txt/)
const blob = await get('/blob/README.md')
checkHtml(blob)
assert.match(blob.text, /<b>/)
assert.doesNotMatch(blob.text, /<b>bold<\/b>/)
const binary = await get('/blob/binary.bin')
checkHtml(binary)
assert.match(binary.text, /Binary file/)
assert.match(binary.text, /5 bytes/)
const commit = await get(`/commit/${head}`)
checkHtml(commit)
assert.match(commit.text, new RegExp(head))
assert.match(commit.text, /Second commit/)
assert.equal((await get('/blob/nope.txt')).status, 404)
assert.equal((await get('/tree/nope')).status, 404)
assert.equal((await get('/commit/0000000000000000000000000000000000000000')).status, 404)
assert.equal((await getRaw('/tree/%2e%2e/')).status, 404)
assert.equal((await getRaw('/blob/%2e%2e%2fREADME.md')).status, 404)
const feed = await get('/feed')
checkHtml(feed, true)
assert.match(feed.text, /id="feed-entries"/)
const liveScript = await get('/feed-live.js')
assert.equal(liveScript.status, 200)
assert.match(liveScript.type, /^text\/javascript/)
assert.match(feed.text, /<h1>ANBot feed<\/h1>/)
assert.doesNotMatch(feed.text, /<style|\sstyle=|<form|ANBot repository|<table/)
assert.match(feed.text, /Hello <script>alert\(1\)<\/script><br>\nSecond line/)
assert.ok(feed.text.includes(`?author=${encodeURIComponent(author)}`))
assert.ok(feed.text.includes(`?post=${encodeURIComponent(post)}`))
assert.ok(feed.text.indexOf('Reply from another author') < feed.text.indexOf('Hello'))
const ownFeed = await get(`/feed?author=${encodeURIComponent(author)}`)
checkHtml(ownFeed, true)
assert.match(ownFeed.text, /Hello/)
assert.doesNotMatch(ownFeed.text, /Reply from another author/)
const peerFeed = await get(`/feed?author=${encodeURIComponent(other)}`)
checkHtml(peerFeed, true)
assert.match(peerFeed.text, /Reply from another author/)
assert.doesNotMatch(peerFeed.text, /Hello/)
const thread = await get(`/feed?post=${encodeURIComponent(post)}`)
checkHtml(thread, true)
assert.match(thread.text, /Hello[\s\S]*<h2>Replies<\/h2>[\s\S]*Reply from another author/)
assert.equal((await get('/feed?post=invalid')).status, 404)
assert.equal((await get(`/feed?post=${encodeURIComponent(author)}`)).status, 404)
JS
for record in "$scratch/node/posts/"*; do
if sed 's/Reply from another author/Tampered reply/' "$record" >"$scratch/tampered" &&
! cmp -s "$record" "$scratch/tampered"; then
mv "$scratch/tampered" "$record"
fi
done
node --input-type=module - "$base" <<'JS'
import assert from 'node:assert/strict'
const response = await fetch(process.argv[2] + '/feed')
assert.equal(response.status, 503)
JS
git clone -q "$url" "$scratch/clone"
test "$(git -C "$scratch/clone" rev-parse HEAD)" = "$head"
printf 'PASS: web UI and signed social feed served %s and Git clone still works.\n' "$head"
Git ยท Social feed