passflow 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -2
  3. package/dist/cli.mjs +1759 -0
  4. package/package.json +42 -3
  5. package/skills/REVISION +1 -0
  6. package/skills/index.json +405 -0
  7. package/skills/passflow-nextjs/SKILL.md +205 -0
  8. package/skills/passflow-nextjs/assets/.env.example +5 -0
  9. package/skills/passflow-nextjs/assets/app/account/page.tsx +29 -0
  10. package/skills/passflow-nextjs/assets/app/actions.example.ts +21 -0
  11. package/skills/passflow-nextjs/assets/app/api/me/route.ts +22 -0
  12. package/skills/passflow-nextjs/assets/components/auth-links.tsx +11 -0
  13. package/skills/passflow-nextjs/assets/lib/call-api.example.ts +15 -0
  14. package/skills/passflow-nextjs/assets/lib/passflow.ts +4 -0
  15. package/skills/passflow-nextjs/assets/proxy.ts +10 -0
  16. package/skills/passflow-nextjs/references/guardrails.md +87 -0
  17. package/skills/passflow-nextjs/scripts/index.json +405 -0
  18. package/skills/passflow-nextjs/scripts/lib.sh +1069 -0
  19. package/skills/passflow-nextjs/scripts/verify.sh +467 -0
  20. package/skills/passflow-node-api/SKILL.md +189 -0
  21. package/skills/passflow-node-api/assets/examples/explain-rejection.ts +22 -0
  22. package/skills/passflow-node-api/assets/examples/express-routes.ts +22 -0
  23. package/skills/passflow-node-api/assets/examples/fastify-routes.ts +28 -0
  24. package/skills/passflow-node-api/assets/examples/hono-routes.ts +19 -0
  25. package/skills/passflow-node-api/assets/src/express-cors.ts +11 -0
  26. package/skills/passflow-node-api/assets/src/express.ts +13 -0
  27. package/skills/passflow-node-api/assets/src/fastify-cors.ts +11 -0
  28. package/skills/passflow-node-api/assets/src/fastify.ts +14 -0
  29. package/skills/passflow-node-api/assets/src/fetch-handler.ts +13 -0
  30. package/skills/passflow-node-api/assets/src/hono-cors.ts +11 -0
  31. package/skills/passflow-node-api/assets/src/hono.ts +16 -0
  32. package/skills/passflow-node-api/assets/src/passflow.ts +21 -0
  33. package/skills/passflow-node-api/references/guardrails.md +66 -0
  34. package/skills/passflow-node-api/scripts/index.json +405 -0
  35. package/skills/passflow-node-api/scripts/lib.sh +1069 -0
  36. package/skills/passflow-node-api/scripts/verify.sh +283 -0
  37. package/skills/passflow-react-vite/SKILL.md +225 -0
  38. package/skills/passflow-react-vite/assets/server/express-cors.ts +9 -0
  39. package/skills/passflow-react-vite/assets/server/express.ts +14 -0
  40. package/skills/passflow-react-vite/assets/server/fastify-cors.ts +11 -0
  41. package/skills/passflow-react-vite/assets/server/fastify.ts +15 -0
  42. package/skills/passflow-react-vite/assets/server/hono-cors.ts +8 -0
  43. package/skills/passflow-react-vite/assets/server/hono.ts +17 -0
  44. package/skills/passflow-react-vite/assets/server/passflow.ts +30 -0
  45. package/skills/passflow-react-vite/assets/server/tsconfig.json +14 -0
  46. package/skills/passflow-react-vite/assets/src/passflow.tsx +28 -0
  47. package/skills/passflow-react-vite/assets/src/require-sign-in.tsx +35 -0
  48. package/skills/passflow-react-vite/assets/src/use-api.ts +23 -0
  49. package/skills/passflow-react-vite/references/guardrails.md +82 -0
  50. package/skills/passflow-react-vite/scripts/index.json +405 -0
  51. package/skills/passflow-react-vite/scripts/lib.sh +1069 -0
  52. package/skills/passflow-react-vite/scripts/verify.sh +678 -0
  53. package/skills/passflow-tanstack-start/SKILL.md +182 -0
  54. package/skills/passflow-tanstack-start/assets/.env.example +5 -0
  55. package/skills/passflow-tanstack-start/assets/src/components/auth-links.tsx +10 -0
  56. package/skills/passflow-tanstack-start/assets/src/lib/call-api.example.ts +14 -0
  57. package/skills/passflow-tanstack-start/assets/src/lib/passflow.ts +14 -0
  58. package/skills/passflow-tanstack-start/assets/src/lib/server-functions.example.ts +20 -0
  59. package/skills/passflow-tanstack-start/assets/src/routes/account.tsx +25 -0
  60. package/skills/passflow-tanstack-start/assets/src/routes/api/me.ts +25 -0
  61. package/skills/passflow-tanstack-start/assets/src/start.ts +14 -0
  62. package/skills/passflow-tanstack-start/references/guardrails.md +73 -0
  63. package/skills/passflow-tanstack-start/scripts/index.json +405 -0
  64. package/skills/passflow-tanstack-start/scripts/lib.sh +1069 -0
  65. package/skills/passflow-tanstack-start/scripts/verify.sh +470 -0
@@ -0,0 +1,470 @@
1
+ #!/usr/bin/env bash
2
+ # Checks a TanStack Start app that signs users in with @passflow/tanstack-react-start.
3
+ #
4
+ # bash verify.sh static [--dir DIR]
5
+ # bash verify.sh run [--dir DIR] [--port N] [--page PATH] [--api PATH]
6
+ #
7
+ # Exit 0: every check passed. 1: a check failed. 2: refused or usage error.
8
+ # Prints one "PASS <check>", "FAIL <check>: <reason>" or "SKIP <check>: <reason>" line per check,
9
+ # then "RESULT pass|fail".
10
+ set -euo pipefail
11
+
12
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13
+ # shellcheck source=SCRIPTDIR/lib.sh
14
+ . "$SCRIPT_DIR/lib.sh"
15
+
16
+ STACK_ID='tanstack-start'
17
+ PACKAGE='@passflow/tanstack-react-start'
18
+ ENV_FILE_NAME='.env.local'
19
+ ENV_KEYS='PASSFLOW_ISSUER PASSFLOW_CLIENT_ID PASSFLOW_CLIENT_SECRET PASSFLOW_SECRET APP_BASE_URL'
20
+ SECRET_KEYS='PASSFLOW_CLIENT_SECRET PASSFLOW_SECRET'
21
+ NODE_MIN='22.12'
22
+ DEFAULT_PORT='3101'
23
+ DEFAULT_PAGE='/account'
24
+ DEFAULT_API='/api/me'
25
+ # Where the build puts the files the browser loads: Nitro output, then the default Start output.
26
+ CLIENT_BUNDLE_DIRS='.output/public dist/client'
27
+
28
+ usage() {
29
+ cat >&2 <<'EOF'
30
+ Usage:
31
+ bash verify.sh static [--dir DIR]
32
+ Install with lifecycle scripts off, type-check, build, search the client bundle for the
33
+ secrets' names and values and for server-only code, and scan the source for guardrail breaks.
34
+ Needs no env values and no network beyond the install.
35
+
36
+ bash verify.sh run [--dir DIR] [--port N] [--page PATH] [--api PATH]
37
+ Build, start the app with .env.local, and check its answers while signed out.
38
+ --port port for the built app (default 3101)
39
+ --page a protected page (default /account)
40
+ --api a protected server route (default /api/me)
41
+
42
+ DIR defaults to the current directory.
43
+ Exit 0 pass, 1 a check failed, 2 refused or usage error.
44
+ EOF
45
+ }
46
+
47
+ MODE="${1:-}"
48
+ if [ "$MODE" != static ] && [ "$MODE" != run ]; then
49
+ usage
50
+ exit 2
51
+ fi
52
+ shift
53
+
54
+ INDEX="$(pf_index_file)"
55
+ DIR="$PWD"
56
+ PORT_ARG="$DEFAULT_PORT"
57
+ PAGE_PATH="$DEFAULT_PAGE"
58
+ API_PATH="$DEFAULT_API"
59
+
60
+ while [ "$#" -gt 0 ]; do
61
+ case "$1" in
62
+ --dir) DIR="${2:-}"; shift 2 || { usage; exit 2; } ;;
63
+ --port) PORT_ARG="${2:-}"; shift 2 || { usage; exit 2; } ;;
64
+ --page) PAGE_PATH="${2:-}"; shift 2 || { usage; exit 2; } ;;
65
+ --api) API_PATH="${2:-}"; shift 2 || { usage; exit 2; } ;;
66
+ -h | --help) usage; exit 2 ;;
67
+ *) printf 'Unknown argument: %s\n' "$1" >&2; usage; exit 2 ;;
68
+ esac
69
+ done
70
+
71
+ if [ -z "$DIR" ] || [ ! -f "$DIR/package.json" ]; then
72
+ printf 'REFUSED: no package.json in %s\n' "${DIR:-<empty>}" >&2
73
+ exit 2
74
+ fi
75
+ DIR="$(cd "$DIR" && pwd)"
76
+ case "$PORT_ARG" in
77
+ '' | *[!0-9]*) printf 'REFUSED: --port must be a number\n' >&2; exit 2 ;;
78
+ esac
79
+ for p in "$PAGE_PATH" "$API_PATH"; do
80
+ case "$p" in
81
+ /*) ;;
82
+ *) printf 'REFUSED: --page and --api must start with /\n' >&2; exit 2 ;;
83
+ esac
84
+ done
85
+
86
+ pf_require_tool node || exit 2
87
+ if ! pf_node_at_least "$NODE_MIN"; then
88
+ printf 'REFUSED: Node.js %s or newer is required\n' "$NODE_MIN" >&2
89
+ exit 2
90
+ fi
91
+
92
+ STACK="$(pf_detect_stack "$DIR" "$INDEX")"
93
+ case "$STACK" in
94
+ "$STACK_ID") ;;
95
+ refused:*) printf 'REFUSED: %s\n' "$(pf_stack_refusal "$DIR" "$INDEX" "${STACK#refused:}")" >&2; exit 2 ;;
96
+ *) printf 'REFUSED: package.json does not describe a TanStack Start app (detected: %s)\n' "$STACK" >&2; exit 2 ;;
97
+ esac
98
+ # The directory that holds routes/ and router.tsx: src/ by default, app/ in older layouts.
99
+ if [ -d "$DIR/src/routes" ]; then
100
+ ROOT_NAME='src'
101
+ elif [ -d "$DIR/app/routes" ]; then
102
+ ROOT_NAME='app'
103
+ else
104
+ printf 'REFUSED: no src/routes/ or app/routes/ directory\n' >&2
105
+ exit 2
106
+ fi
107
+ ROOT="$DIR/$ROOT_NAME"
108
+
109
+ PM="$(pf_detect_pm "$DIR")"
110
+ if ! pf_pm_supported "$PM"; then
111
+ printf 'REFUSED: %s\n' "$(pf_pm_refusal "$PM")" >&2
112
+ exit 2
113
+ fi
114
+ pf_require_tool "$(pf_pm_tool "$PM")" || exit 2
115
+
116
+ # ---------------------------------------------------------------- checks used by both modes
117
+
118
+ check_start_file() {
119
+ local f found=''
120
+ for f in start.ts start.tsx start.js; do
121
+ if [ -f "$ROOT/$f" ] && grep -q 'requestMiddleware' "$ROOT/$f" && grep -q 'passflow\.middleware()' "$ROOT/$f"; then
122
+ found="$f"
123
+ fi
124
+ done
125
+ if [ -z "$found" ]; then
126
+ pf_fail "request middleware" "no $ROOT_NAME/start.ts lists passflow.middleware() in requestMiddleware"
127
+ elif ! grep -q 'createCsrfMiddleware(' "$ROOT/$found"; then
128
+ # Setting requestMiddleware turns off Start's own check on server-function calls; this line restores it.
129
+ pf_fail "request middleware" "$ROOT_NAME/$found has no createCsrfMiddleware({ filter: (ctx) => ctx.handlerType === 'serverFn' }); server functions are unprotected without it"
130
+ elif ! grep -qE "handlerType[[:space:]]*===[[:space:]]*['\"]serverFn['\"]" "$ROOT/$found"; then
131
+ # Unfiltered, Start's check also refuses the issuer's redirect back to /auth/callback.
132
+ pf_fail "request middleware" "$ROOT_NAME/$found calls createCsrfMiddleware without the filter (ctx) => ctx.handlerType === 'serverFn'"
133
+ else
134
+ pf_pass "request middleware ($ROOT_NAME/$found registers passflow.middleware())"
135
+ fi
136
+ }
137
+
138
+ # getSession, requireSession and getAccessToken read the server's request: each call must sit inside a server
139
+ # function or a server route. A call is flagged when the nearest line above it (or its own line) that opens
140
+ # code opens browser-side code (beforeLoad, loader, component, a component function) rather than a server
141
+ # function or server route (createServerFn, createServerOnlyFn, .handler(, server: {). A helper module with
142
+ # neither, called from server code, is not flagged.
143
+ server_only_scan() {
144
+ local files f found hits=''
145
+ files="$(_pf_source_files "$ROOT")"
146
+ while IFS= read -r f; do
147
+ [ -n "$f" ] || continue
148
+ grep -qE 'passflow\.(getSession|requireSession|getAccessToken)\(' "$f" 2>/dev/null || continue
149
+ found="$(awk -v file="$f" '
150
+ /createServerFn|createServerOnlyFn|\.handler\(|server:[[:space:]]*\{/ { ctx = "server" }
151
+ /beforeLoad|loader[[:space:]]*:|component[[:space:]]*:|function[[:space:]]+[A-Z]|=[[:space:]]*\([^)]*\)[[:space:]]*=>[[:space:]]*[(<]/ {
152
+ if ($0 !~ /createServerFn|createServerOnlyFn|\.handler\(|server:[[:space:]]*\{/) ctx = "client"
153
+ }
154
+ /passflow\.(getSession|requireSession|getAccessToken)\(/ {
155
+ if (ctx == "client") printf " getSession, requireSession or getAccessToken outside a server function or server route: %s:%d\n", file, NR
156
+ }' "$f" 2>/dev/null || true)"
157
+ if [ -n "$found" ]; then
158
+ hits="$hits$found
159
+ "
160
+ fi
161
+ done <<EOF
162
+ $files
163
+ EOF
164
+ if [ -n "$hits" ]; then
165
+ printf '%s' "$hits"
166
+ return 1
167
+ fi
168
+ return 0
169
+ }
170
+
171
+ # ---------------------------------------------------------------- static
172
+
173
+ run_static() {
174
+ local rc d scanned=0 found=0 bundle_dirs server_code='' build_failed=0
175
+
176
+ if pf_has_dep "$DIR" "$PACKAGE"; then
177
+ pf_pass "dependency $PACKAGE"
178
+ else
179
+ pf_fail "dependency $PACKAGE" "not in dependencies or devDependencies of package.json"
180
+ fi
181
+
182
+ check_start_file
183
+
184
+ rc=0
185
+ pf_install "$DIR" >"$WORK/install.log" 2>&1 || rc=$?
186
+ if [ "$rc" -eq 0 ]; then
187
+ pf_pass "install ($PM)"
188
+ else
189
+ pf_fail "install ($PM)" "exit $rc; log: $WORK/install.log"
190
+ return 0
191
+ fi
192
+
193
+ # Start writes routeTree.gen.ts during the build, so a new route type-checks only after it.
194
+ if pf_has_script "$DIR" build; then
195
+ rc=0
196
+ pf_run_script "$DIR" build >"$WORK/build.log" 2>&1 || rc=$?
197
+ if [ "$rc" -eq 0 ]; then
198
+ pf_pass build
199
+ else
200
+ build_failed=1
201
+ pf_fail build "exit $rc; log: $WORK/build.log"
202
+ fi
203
+ else
204
+ pf_skip build "no build script"
205
+ fi
206
+
207
+ rc=0
208
+ pf_typecheck "$DIR" >"$WORK/typecheck.log" 2>&1 || rc=$?
209
+ case "$rc" in
210
+ 0) pf_pass typecheck ;;
211
+ 3) pf_skip typecheck "no typecheck script and no tsconfig.json" ;;
212
+ *) pf_fail typecheck "exit $rc; log: $WORK/typecheck.log" ;;
213
+ esac
214
+
215
+ if [ "$build_failed" -eq 1 ]; then
216
+ # The folders hold an older build: scanning them would prove nothing.
217
+ pf_skip "client bundle" "the build failed"
218
+ elif pf_has_script "$DIR" build; then
219
+ bundle_dirs="$CLIENT_BUNDLE_DIRS"
220
+ : >"$WORK/bundle.log"
221
+ for d in $bundle_dirs; do
222
+ rc=0
223
+ # shellcheck disable=SC2086
224
+ pf_scan_bundle "$DIR/$d" "$DIR/$ENV_FILE_NAME" $SECRET_KEYS >>"$WORK/bundle.log" 2>&1 || rc=$?
225
+ case "$rc" in
226
+ 0) scanned=1 ;;
227
+ 1) scanned=1; found=1 ;;
228
+ esac
229
+ if [ -d "$DIR/$d" ]; then
230
+ server_code="$server_code$(grep -rlF 'node:async_hooks' "$DIR/$d" 2>/dev/null || true)"
231
+ fi
232
+ done
233
+ if [ "$scanned" -eq 0 ]; then
234
+ pf_fail "client bundle" "no client bundle in: $(printf '%s' "$bundle_dirs" | tr '\n' ' ')"
235
+ elif [ "$found" -eq 1 ]; then
236
+ pf_fail "client bundle" "a secret's name or value is in the browser's files"
237
+ cat "$WORK/bundle.log"
238
+ else
239
+ pf_pass "client bundle has no secret names or values"
240
+ fi
241
+ if [ "$scanned" -eq 1 ]; then
242
+ if [ -n "$server_code" ]; then
243
+ pf_fail "client bundle" "server-only code (node:async_hooks) is in the browser's files"
244
+ printf '%s\n' "$server_code" | while IFS= read -r f; do printf ' %s\n' "$f"; done
245
+ else
246
+ pf_pass "client bundle has no server-only code"
247
+ fi
248
+ fi
249
+ else
250
+ pf_skip "client bundle" "nothing was built"
251
+ fi
252
+
253
+ rc=0
254
+ pf_guardrail_scan "$DIR" server >"$WORK/guardrails.log" 2>&1 || rc=$?
255
+ server_only_scan >>"$WORK/guardrails.log" 2>&1 || rc=1
256
+ if [ "$rc" -eq 0 ]; then
257
+ pf_pass guardrails
258
+ else
259
+ pf_fail guardrails "see below"
260
+ cat "$WORK/guardrails.log"
261
+ fi
262
+ }
263
+
264
+ # ---------------------------------------------------------------- run
265
+
266
+ stop_server() {
267
+ if [ -n "$PF_BG_PID" ]; then
268
+ pf_kill_tree "$PF_BG_PID"
269
+ PF_BG_PID=''
270
+ fi
271
+ }
272
+
273
+ # Starts the built app: the start script, else the Nitro output, else the preview script.
274
+ start_server() {
275
+ if pf_has_script "$DIR" start; then
276
+ SERVER_HOW='start script'
277
+ case "$PM" in
278
+ npm) pf_start_bg "$DIR" "$WORK/server.log" npm run start ;;
279
+ pnpm) pf_start_bg "$DIR" "$WORK/server.log" pnpm run start ;;
280
+ yarn-classic) pf_start_bg "$DIR" "$WORK/server.log" yarn run start ;;
281
+ esac
282
+ elif [ -f "$DIR/.output/server/index.mjs" ]; then
283
+ SERVER_HOW='.output/server/index.mjs'
284
+ pf_start_bg "$DIR" "$WORK/server.log" node .output/server/index.mjs
285
+ elif pf_has_script "$DIR" preview; then
286
+ SERVER_HOW='preview script'
287
+ case "$PM" in
288
+ npm) pf_start_bg "$DIR" "$WORK/server.log" npm run preview -- --port "$PORT_ARG" --strictPort ;;
289
+ pnpm) pf_start_bg "$DIR" "$WORK/server.log" pnpm run preview --port "$PORT_ARG" --strictPort ;;
290
+ yarn-classic) pf_start_bg "$DIR" "$WORK/server.log" yarn run preview --port "$PORT_ARG" --strictPort ;;
291
+ esac
292
+ else
293
+ return 2
294
+ fi
295
+ }
296
+
297
+ run_live() {
298
+ local env_file="$DIR/$ENV_FILE_NAME" missing base status location body_has issuer rc app_origin
299
+ if [ ! -f "$env_file" ]; then
300
+ printf 'REFUSED: %s not found; run mode needs it\n' "$env_file" >&2
301
+ exit 2
302
+ fi
303
+ # shellcheck disable=SC2086
304
+ missing="$(pf_env_missing_keys "$env_file" $ENV_KEYS)"
305
+ if [ -n "$missing" ]; then
306
+ printf 'REFUSED: %s has no value for: %s\n' "$ENV_FILE_NAME" "$(printf '%s' "$missing" | tr '\n' ' ')" >&2
307
+ exit 2
308
+ fi
309
+ pf_require_tool curl || exit 2
310
+ if ! pf_has_script "$DIR" build; then
311
+ printf 'REFUSED: package.json has no build script\n' >&2
312
+ exit 2
313
+ fi
314
+ # localhost, not 127.0.0.1: `vite preview` listens on localhost only, which can be IPv6.
315
+ base="http://localhost:$PORT_ARG"
316
+ if ! pf_port_free "$PORT_ARG"; then
317
+ printf 'REFUSED: %s\n' "$(pf_port_busy_refusal "$PORT_ARG")" >&2
318
+ exit 2
319
+ fi
320
+
321
+ # Only the stack's own names; the app reads the rest of .env.local itself.
322
+ # shellcheck disable=SC2086
323
+ pf_load_env "$env_file" $ENV_KEYS
324
+
325
+ # The build comes first: with Nitro it writes .output/server/index.mjs, one of the ways to start.
326
+ rc=0
327
+ pf_run_script "$DIR" build >"$WORK/build.log" 2>&1 || rc=$?
328
+ if [ "$rc" -eq 0 ]; then
329
+ pf_pass build
330
+ else
331
+ pf_fail build "exit $rc; log: $WORK/build.log"
332
+ return 0
333
+ fi
334
+
335
+ export PORT="$PORT_ARG"
336
+ pf_traps stop_server
337
+ SERVER_HOW=''
338
+ if ! start_server; then
339
+ printf 'REFUSED: nothing to start: package.json has no start or preview script and the build wrote no .output/server/index.mjs\n' >&2
340
+ exit 2
341
+ fi
342
+
343
+ if pf_wait_http "$base/" 90 "$PF_BG_PID"; then
344
+ pf_pass "server starts on port $PORT_ARG ($SERVER_HOW)"
345
+ elif pf_alive "$PF_BG_PID"; then
346
+ pf_fail "server starts on port $PORT_ARG ($SERVER_HOW)" "no answer within 90 s; log: $WORK/server.log"
347
+ return 0
348
+ else
349
+ pf_fail "server starts on port $PORT_ARG ($SERVER_HOW)" "the process exited; log: $WORK/server.log"
350
+ return 0
351
+ fi
352
+ pf_check_listener "$PORT_ARG" "$PF_BG_PID"
353
+
354
+ # 1. A protected page, signed out: no protected content, and the way out is sign-in.
355
+ pf_http "$WORK/page" GET "$base$PAGE_PATH" || true
356
+ status="$(cat "$WORK/page/status")"
357
+ location="$(pf_header "$WORK/page" Location)"
358
+ body_has=0
359
+ if grep -q 'data-passflow-protected' "$WORK/page/body"; then body_has=1; fi
360
+ if [ "$body_has" -eq 1 ]; then
361
+ pf_fail "GET $PAGE_PATH signed out" "status $status; the body has data-passflow-protected content"
362
+ else
363
+ # Only a real redirect counts. A sign-in link in the page's navigation also names /auth/login, on an
364
+ # unprotected page or a 404 as well, so the body's links prove nothing.
365
+ case "$status $location" in
366
+ 30[12378]\ */auth/login*) pf_pass "GET $PAGE_PATH signed out: no protected content, $status redirect to /auth/login" ;;
367
+ 404\ *) pf_fail "GET $PAGE_PATH signed out" "status 404; --page must name a protected route that exists" ;;
368
+ *)
369
+ if grep -qE 'http-equiv="?refresh"?[^>]*/auth/login' "$WORK/page/body"; then
370
+ pf_pass "GET $PAGE_PATH signed out: no protected content, a meta refresh to /auth/login ($status)"
371
+ else
372
+ pf_fail "GET $PAGE_PATH signed out" "status $status, Location '${location}'; want a redirect to /auth/login (is beforeLoad: requireUser on the route?)"
373
+ fi
374
+ ;;
375
+ esac
376
+ fi
377
+
378
+ # 2. A protected server route, signed out: 401 with no-store, never a redirect.
379
+ pf_http "$WORK/api" GET "$base$API_PATH" || true
380
+ status="$(cat "$WORK/api/status")"
381
+ if [ "$status" = 401 ] && pf_has_token "$(pf_header "$WORK/api" Cache-Control)" no-store; then
382
+ pf_pass "GET $API_PATH signed out: 401, Cache-Control no-store"
383
+ else
384
+ pf_fail "GET $API_PATH signed out" "status $status, Cache-Control '$(pf_header "$WORK/api" Cache-Control)'; want 401 and no-store (status 000: no answer)"
385
+ fi
386
+
387
+ # 3. Sign-in starts at the issuer with PKCE and sets the transaction cookie.
388
+ pf_http "$WORK/login" GET "$base/auth/login" || true
389
+ status="$(cat "$WORK/login/status")"
390
+ location="$(pf_header "$WORK/login" Location)"
391
+ issuer="${PASSFLOW_ISSUER%/}"
392
+ case "$status" in
393
+ 30[1278])
394
+ if [ "${location#"$issuer"/oidc/authorize}" = "$location" ]; then
395
+ pf_fail "GET /auth/login" "Location is not PASSFLOW_ISSUER/oidc/authorize"
396
+ elif ! printf '%s' "$location" | grep -q 'code_challenge_method=S256'; then
397
+ pf_fail "GET /auth/login" "Location has no code_challenge_method=S256"
398
+ else
399
+ pf_pass "GET /auth/login: $status to the issuer's /oidc/authorize with S256"
400
+ fi
401
+ ;;
402
+ *) pf_fail "GET /auth/login" "status $status; want a redirect" ;;
403
+ esac
404
+ if tr -d '\r' <"$WORK/login/headers" | grep -iE '^set-cookie:[[:space:]]*(__Host-)?passflow_txn=' | grep -qi 'httponly'; then
405
+ pf_pass "GET /auth/login sets an HttpOnly transaction cookie (passflow_txn, or __Host-passflow_txn on https)"
406
+ else
407
+ pf_fail "GET /auth/login" "no HttpOnly passflow_txn or __Host-passflow_txn cookie"
408
+ fi
409
+ location=''
410
+ issuer=''
411
+
412
+ # 4. The middleware refuses a POST from another site that carries any cookie, a session made of chunks
413
+ # only included. A POST with no cookie (a webhook) and the app's own POST reach the route, which answers
414
+ # its signed-out 401.
415
+ pf_http "$WORK/cross-chunk" POST "$base$API_PATH" -H 'Cookie: passflow_session.0=x' -H 'Origin: https://evil.example' || true
416
+ status="$(cat "$WORK/cross-chunk/status")"
417
+ if [ "$status" = 403 ]; then
418
+ pf_pass "cross-origin POST $API_PATH with only passflow_session.0: 403"
419
+ else
420
+ pf_fail "cross-origin POST $API_PATH with only passflow_session.0" "status $status; want 403"
421
+ fi
422
+ pf_http "$WORK/cross-cookie" POST "$base$API_PATH" -H 'Cookie: theme=dark' -H 'Origin: https://evil.example' || true
423
+ status="$(cat "$WORK/cross-cookie/status")"
424
+ if [ "$status" = 403 ]; then
425
+ pf_pass "cross-origin POST $API_PATH with an ordinary cookie: 403"
426
+ else
427
+ pf_fail "cross-origin POST $API_PATH with an ordinary cookie" "status $status; want 403"
428
+ fi
429
+ pf_http "$WORK/cross-none" POST "$base$API_PATH" -H 'Origin: https://evil.example' || true
430
+ status="$(cat "$WORK/cross-none/status")"
431
+ if [ "$status" = 405 ]; then
432
+ # The middleware let it through and the route has no POST handler: the route-level checks cannot run.
433
+ pf_skip "POST $API_PATH reaching the route" "the route has no POST handler (405); the cross-origin cookie checks above still apply"
434
+ else
435
+ if [ "$status" = 401 ]; then
436
+ pf_pass "cross-origin POST $API_PATH with no cookie reaches the route: 401"
437
+ else
438
+ pf_fail "cross-origin POST $API_PATH with no cookie" "status $status; want the route's signed-out 401"
439
+ fi
440
+ app_origin="$(printf '%s' "$APP_BASE_URL" | sed -E 's#^([A-Za-z][A-Za-z0-9+.-]*://[^/?#]+).*#\1#')"
441
+ pf_http "$WORK/same" POST "$base$API_PATH" -H 'Cookie: passflow_session.0=x' -H "Origin: $app_origin" || true
442
+ status="$(cat "$WORK/same/status")"
443
+ app_origin=''
444
+ if [ "$status" = 401 ]; then
445
+ pf_pass "same-origin POST $API_PATH passes the Origin check: 401 signed out"
446
+ else
447
+ pf_fail "same-origin POST $API_PATH" "status $status; want the route's signed-out 401 (the app's own origin is APP_BASE_URL's)"
448
+ fi
449
+ fi
450
+
451
+ if ! pf_alive "$PF_BG_PID"; then
452
+ pf_fail "server still running after the checks" "the process exited; log: $WORK/server.log"
453
+ fi
454
+ stop_server
455
+ }
456
+
457
+ WORK="$(mktemp -d "$PF_TMP/passflow-verify.XXXXXX")"
458
+ printf 'verify.sh %s: %s (%s, %s)\n' "$MODE" "$DIR" "$STACK_ID" "$PM"
459
+ printf 'logs: %s\n' "$WORK"
460
+
461
+ if [ "$MODE" = static ]; then
462
+ run_static
463
+ else
464
+ run_live
465
+ fi
466
+
467
+ if pf_result; then
468
+ exit 0
469
+ fi
470
+ exit 1