smoke-auth.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env bash
  2. # End-to-end check of the auth gate against a local `wrangler dev`.
  3. #
  4. # Verifies the acceptance criteria for the gate: unauthenticated requests reach
  5. # nothing (pages, API, or static assets), a valid cookie reaches everything, and
  6. # a tampered cookie is rejected. Run it after touching src/auth.ts or the route
  7. # table in src/index.ts.
  8. #
  9. # ./scripts/smoke-auth.sh
  10. set -uo pipefail
  11. cd "$(dirname "$0")/.."
  12. # Deliberately NOT $PORT: that is commonly already set to some other local dev
  13. # server, and the whole suite would then silently test the wrong app.
  14. DASH_PORT="${DASH_PORT:-8788}"
  15. BASE="http://127.0.0.1:${DASH_PORT}"
  16. PASSWORD="$(grep '^ADMIN_PASSWORD=' .dev.vars | cut -d'"' -f2)"
  17. JAR="$(mktemp -t cg-dash-jar)"
  18. LOG="$(mktemp -t cg-dash-log)"
  19. DEV_VARS_BACKUP="$(mktemp -t cg-dash-vars)"
  20. PASS=0
  21. FAIL=0
  22. cleanup() {
  23. [[ -n "${DEV_PID:-}" ]] && kill "$DEV_PID" 2>/dev/null
  24. # The rotation phase rewrites .dev.vars; always put the original back.
  25. [[ -s "$DEV_VARS_BACKUP" ]] && cp "$DEV_VARS_BACKUP" .dev.vars
  26. rm -f "$JAR" "$LOG" "$DEV_VARS_BACKUP"
  27. }
  28. trap cleanup EXIT
  29. # `curl -o /dev/null -w '%{http_code}'` plus the headers we care about.
  30. status() { curl -s -o /dev/null -w '%{http_code}' "$@"; }
  31. body() { curl -s "$@"; }
  32. check() { # check <description> <expected> <actual>
  33. if [[ "$2" == "$3" ]]; then
  34. printf ' ok %s\n' "$1"
  35. PASS=$((PASS + 1))
  36. else
  37. printf ' FAIL %s (expected %s, got %s)\n' "$1" "$2" "$3"
  38. FAIL=$((FAIL + 1))
  39. fi
  40. }
  41. contains() { # contains <description> <needle> <haystack>
  42. if [[ "$3" == *"$2"* ]]; then
  43. printf ' ok %s\n' "$1"
  44. PASS=$((PASS + 1))
  45. else
  46. printf ' FAIL %s (missing %q in %.200q…)\n' "$1" "$2" "$3"
  47. FAIL=$((FAIL + 1))
  48. fi
  49. }
  50. lacks() { # lacks <description> <needle> <haystack>
  51. if [[ "$3" != *"$2"* ]]; then
  52. printf ' ok %s\n' "$1"
  53. PASS=$((PASS + 1))
  54. else
  55. printf ' FAIL %s (found %q)\n' "$1" "$2"
  56. FAIL=$((FAIL + 1))
  57. fi
  58. }
  59. echo "Seeding local D1 from the ingest worker's migration…"
  60. npx wrangler d1 execute codegraph-telemetry --local \
  61. --file=../telemetry-worker/migrations/0001_init.sql >/dev/null 2>&1
  62. echo "Starting wrangler dev on :${DASH_PORT}…"
  63. npx wrangler dev --port "$DASH_PORT" --ip 127.0.0.1 >"$LOG" 2>&1 &
  64. DEV_PID=$!
  65. READY=""
  66. for _ in $(seq 1 90); do
  67. if [[ "$(body "$BASE/robots.txt")" == "User-agent: *"* ]]; then READY=1; break; fi
  68. sleep 1
  69. done
  70. if [[ -z "$READY" ]]; then
  71. echo "wrangler dev never came up on :${DASH_PORT} — log follows"
  72. cat "$LOG"
  73. exit 1
  74. fi
  75. echo
  76. echo "Unauthenticated — nothing but the login page and robots.txt"
  77. check "GET / → 302 to login" 302 "$(status "$BASE/")"
  78. check "GET /index.html → 302 to login" 302 "$(status "$BASE/index.html")"
  79. check "GET /styles.css → 302 to login" 302 "$(status "$BASE/styles.css")"
  80. check "GET /app.js → 302 to login" 302 "$(status "$BASE/app.js")"
  81. check "GET /vendor/chart → 302 to login" 302 "$(status "$BASE/vendor/chart.umd.js")"
  82. check "GET /api/health → 401" 401 "$(status "$BASE/api/health")"
  83. check "GET /api/session → 401" 401 "$(status "$BASE/api/session")"
  84. check "GET /api/anything → 401" 401 "$(status "$BASE/api/whatever")"
  85. check "GET /login → 200" 200 "$(status "$BASE/login")"
  86. check "GET /robots.txt → 200" 200 "$(status "$BASE/robots.txt")"
  87. contains "no data leaks in the 401 body" '"unauthorized"' "$(body "$BASE/api/health")"
  88. echo
  89. echo "Login page"
  90. LOGIN_HTML="$(body "$BASE/login")"
  91. contains "sentence-case heading" "codegraph telemetry" "$LOGIN_HTML"
  92. contains "sentence-case label" ">Password<" "$LOGIN_HTML"
  93. contains "sentence-case button" ">Sign in<" "$LOGIN_HTML"
  94. lacks "no uppercased labels" "uppercase" "$LOGIN_HTML"
  95. lacks "no tracked-out labels" "letter-spacing" "$LOGIN_HTML"
  96. contains "label is normal size" "font-size: 16px" "$LOGIN_HTML"
  97. check "open redirect refused" "/" \
  98. "$(body "$BASE/login?next=%2F%2Fevil.example" | sed -n 's/.*name="next" value="\([^"]*\)".*/\1/p')"
  99. check "same-origin next kept" "/api/health" \
  100. "$(body "$BASE/login?next=%2Fapi%2Fhealth" | sed -n 's/.*name="next" value="\([^"]*\)".*/\1/p')"
  101. echo
  102. echo "Sign-in"
  103. check "wrong password → 401" 401 \
  104. "$(status -X POST "$BASE/login" -d "password=definitely-not-it" -d "next=/")"
  105. check "wrong password sets no cookie" "" \
  106. "$(curl -s -D - -o /dev/null -X POST "$BASE/login" -d "password=nope" | grep -ci 'set-cookie' | sed 's/^0$//')"
  107. check "empty password → 400" 400 "$(status -X POST "$BASE/login" -d "password=")"
  108. check "cross-origin post → 400" 400 \
  109. "$(status -X POST "$BASE/login" -H 'Origin: https://evil.example' -d "password=${PASSWORD}")"
  110. # One sign-in, then every cookie assertion reads the captured headers. Doing a
  111. # fresh POST per assertion would burn the login rate limit and 429 halfway down.
  112. SIGNIN="$(curl -s -D - -o /dev/null -c "$JAR" -X POST "$BASE/login" -d "password=${PASSWORD}" -d "next=/")"
  113. check "correct password → 302" "302" "$(printf '%s' "$SIGNIN" | head -1 | awk '{print $2}')"
  114. contains "cookie is HttpOnly" "HttpOnly" "$SIGNIN"
  115. contains "cookie is Secure" "Secure" "$SIGNIN"
  116. contains "cookie is SameSite=Lax" "SameSite=Lax" "$SIGNIN"
  117. contains "cookie is ~1 year" "Max-Age=31536000" "$SIGNIN"
  118. contains "cookie is site-wide" "Path=/" "$SIGNIN"
  119. COOKIE="$(grep cg_admin_session "$JAR" | awk '{print $NF}')"
  120. PAYLOAD="${COOKIE%%.*}"
  121. SIG="${COOKIE#*.}"
  122. # A persistent cookie carries a real expiry in the jar; a session cookie (gone
  123. # on browser restart) carries 0. This is the "survives a restart" criterion.
  124. JAR_EXPIRY="$(grep cg_admin_session "$JAR" | awk '{print $5}')"
  125. if [[ "$JAR_EXPIRY" -gt "$(( $(date +%s) + 300 * 86400 ))" ]]; then
  126. check "cookie persists across browser restarts" "persistent" "persistent"
  127. else
  128. check "cookie persists across browser restarts" "persistent" "session-only (expiry ${JAR_EXPIRY})"
  129. fi
  130. echo
  131. echo "Authenticated — the whole app"
  132. check "GET / → 200" 200 "$(status -b "$JAR" "$BASE/")"
  133. check "GET /styles.css → 200" 200 "$(status -b "$JAR" "$BASE/styles.css")"
  134. check "GET /app.js → 200" 200 "$(status -b "$JAR" "$BASE/app.js")"
  135. check "GET /vendor/chart→ 200" 200 "$(status -b "$JAR" "$BASE/vendor/chart.umd.js")"
  136. check "GET /api/session → 200" 200 "$(status -b "$JAR" "$BASE/api/session")"
  137. check "GET /api/health → 200" 200 "$(status -b "$JAR" "$BASE/api/health")"
  138. contains "health reads D1" '"ok":true' "$(body -b "$JAR" "$BASE/api/health")"
  139. check "GET /login while signed in → 302" 302 "$(status -b "$JAR" "$BASE/login")"
  140. check "unknown API route → 404" 404 "$(status -b "$JAR" "$BASE/api/nope")"
  141. check "POST to an API route → 405" 405 "$(status -b "$JAR" -X POST "$BASE/api/health")"
  142. echo
  143. echo "Tampering"
  144. # Mutate the FIRST signature character, not the last: base64url's final
  145. # character of a 32-byte tag carries only 4 significant bits, so flipping it is
  146. # sometimes a no-op on the decoded bytes and the test would pass vacuously.
  147. FLIPPED="${PAYLOAD}.$([[ "${SIG:0:1}" == 'A' ]] && echo B || echo A)${SIG:1}"
  148. check "flipped signature → 401" 401 "$(status -H "Cookie: cg_admin_session=${FLIPPED}" "$BASE/api/health")"
  149. check "truncated signature → 401" 401 "$(status -H "Cookie: cg_admin_session=${PAYLOAD}.${SIG:0:40}" "$BASE/api/health")"
  150. check "swapped payload → 401" 401 \
  151. "$(status -H "Cookie: cg_admin_session=$(printf '%s' '{"v":1,"iat":0,"exp":9999999999,"pw":"x"}' | base64 | tr -d '=' | tr '+/' '-_').${SIG}" "$BASE/api/health")"
  152. check "no signature → 401" 401 "$(status -H "Cookie: cg_admin_session=${PAYLOAD}" "$BASE/api/health")"
  153. check "garbage cookie → 401" 401 "$(status -H 'Cookie: cg_admin_session=not-a-token' "$BASE/api/health")"
  154. check "empty cookie → 401" 401 "$(status -H 'Cookie: cg_admin_session=' "$BASE/api/health")"
  155. check "tampered cookie on a page → 302 to login" 302 \
  156. "$(status -H "Cookie: cg_admin_session=${FLIPPED}" "$BASE/")"
  157. echo
  158. echo "Sign-out"
  159. check "POST /logout → 302" 302 "$(status -X POST "$BASE/logout")"
  160. contains "logout clears the cookie" "Max-Age=0" \
  161. "$(curl -s -D - -o /dev/null -X POST "$BASE/logout")"
  162. check "GET /logout → 405" 405 "$(status "$BASE/logout")"
  163. echo
  164. echo "Rate limiting (6 attempts in a minute; the 6th should be capped)"
  165. LAST=""
  166. for _ in 1 2 3 4 5 6 7; do
  167. LAST="$(status -X POST "$BASE/login" -d 'password=guess')"
  168. done
  169. check "brute force capped → 429" 429 "$LAST"
  170. echo
  171. echo "Password rotation (restarting with a different ADMIN_PASSWORD)"
  172. cp .dev.vars "$DEV_VARS_BACKUP"
  173. sed 's/^ADMIN_PASSWORD=.*/ADMIN_PASSWORD="rotated-password"/' "$DEV_VARS_BACKUP" >.dev.vars
  174. kill "$DEV_PID" 2>/dev/null
  175. wait "$DEV_PID" 2>/dev/null
  176. npx wrangler dev --port "$DASH_PORT" --ip 127.0.0.1 >"$LOG" 2>&1 &
  177. DEV_PID=$!
  178. for _ in $(seq 1 90); do
  179. [[ "$(body "$BASE/robots.txt")" == "User-agent: *"* ]] && break
  180. sleep 1
  181. done
  182. check "cookie from the old password → 401" 401 \
  183. "$(status -H "Cookie: cg_admin_session=${COOKIE}" "$BASE/api/health")"
  184. check "old password no longer signs in → 401" 401 \
  185. "$(status -X POST "$BASE/login" -d "password=${PASSWORD}")"
  186. check "new password signs in → 302" 302 \
  187. "$(status -X POST "$BASE/login" -d "password=rotated-password")"
  188. echo
  189. printf '%s\n' "-----"
  190. printf '%d passed, %d failed\n' "$PASS" "$FAIL"
  191. [[ "$FAIL" -eq 0 ]]