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,678 @@
1
+ #!/usr/bin/env bash
2
+ # Checks a React + Vite app signed in with @passflow/react, and its Node API protected with @passflow/node.
3
+ #
4
+ # bash verify.sh static [--dir DIR] [--api-dir DIR]
5
+ # bash verify.sh run [--dir DIR] [--api-dir DIR] [--port N] [--api PATH] [--entry FILE] [--no-cors]
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='react-vite'
17
+ SPA_PACKAGE='@passflow/react'
18
+ API_PACKAGE='@passflow/node'
19
+ ENV_FILE_NAME='.env.local'
20
+ SPA_KEYS='VITE_PASSFLOW_URL VITE_PASSFLOW_APP_ID VITE_API_URL'
21
+ API_KEYS='PASSFLOW_ISSUER PASSFLOW_AUDIENCE APP_BASE_URL'
22
+ # Secrets of other Passflow stacks. None belongs in an SPA, so none may be in its bundle.
23
+ FOREIGN_SECRET_KEYS='PASSFLOW_CLIENT_SECRET PASSFLOW_SECRET'
24
+ NODE_MIN='22.12'
25
+ DEFAULT_PORT='8086'
26
+ DEFAULT_API='/api/me'
27
+ # Where `vite build` puts the files the browser loads.
28
+ CLIENT_BUNDLE_DIRS='dist'
29
+ # The API must already use one of these.
30
+ API_FRAMEWORKS='express fastify hono'
31
+ # React frameworks that render on a server; they list vite and react too, but are not single-page apps.
32
+ SSR_FRAMEWORKS='@tanstack/react-start @react-router/dev @remix-run/dev vike vite-plugin-ssr waku astro rakkasjs'
33
+
34
+ usage() {
35
+ cat >&2 <<'EOF'
36
+ Usage:
37
+ bash verify.sh static [--dir DIR] [--api-dir DIR]
38
+ Install with lifecycle scripts off, type-check the SPA and the API, build the SPA, scan dist/ for
39
+ secrets and server code, and scan the source for guardrail breaks. Needs no env file and no network
40
+ beyond the install. A VITE_ name that neither the environment nor .env, .env.local, .env.production
41
+ or .env.production.local sets is built with a placeholder; that build is removed after the scans.
42
+
43
+ bash verify.sh run [--dir DIR] [--api-dir DIR] [--port N] [--api PATH] [--entry FILE] [--no-cors]
44
+ Start the API and check the protected route's answers and its CORS. The API gets PASSFLOW_ISSUER,
45
+ PASSFLOW_AUDIENCE and APP_BASE_URL from the env file, and PORT. Refused when something already
46
+ listens on the port: only an API this script starts is tested.
47
+ --port port for the API, passed as PORT (default 8086)
48
+ --api protected route to call (default /api/me)
49
+ --entry the API's entry file, run with node. Default: the API's api script; in a separate API
50
+ folder its start or dev script; else server/index.ts (or .js, .mjs) or api/index.ts.
51
+ A separate API folder with a start script and a build script is built first.
52
+ --no-cors skip the CORS checks: the SPA calls the API on its own origin (a Vite proxy, for example).
53
+ They are also skipped when the API lacks its CORS package (cors, @fastify/cors).
54
+
55
+ DIR is the SPA's project root and defaults to the current directory.
56
+ --api-dir is the API's folder when it has its own package.json (for example DIR/api). By default it is
57
+ DIR when DIR's package.json lists express, fastify or hono, else DIR/api or DIR/server when that one does.
58
+ Without an API on one of the three, both modes refuse: add an API first.
59
+ Exit 0 pass, 1 a check failed, 2 refused or usage error.
60
+ EOF
61
+ }
62
+
63
+ MODE="${1:-}"
64
+ if [ "$MODE" != static ] && [ "$MODE" != run ]; then
65
+ usage
66
+ exit 2
67
+ fi
68
+ shift
69
+
70
+ DIR="$PWD"
71
+ API_DIR_ARG=''
72
+ PORT_ARG="$DEFAULT_PORT"
73
+ API_PATH="$DEFAULT_API"
74
+ ENTRY_ARG=''
75
+ NO_CORS=0
76
+
77
+ while [ "$#" -gt 0 ]; do
78
+ case "$1" in
79
+ --dir) DIR="${2:-}"; shift 2 || { usage; exit 2; } ;;
80
+ --api-dir) API_DIR_ARG="${2:-}"; shift 2 || { usage; exit 2; } ;;
81
+ --entry) ENTRY_ARG="${2:-}"; shift 2 || { usage; exit 2; } ;;
82
+ --no-cors) NO_CORS=1; shift ;;
83
+ --port) PORT_ARG="${2:-}"; shift 2 || { usage; exit 2; } ;;
84
+ --api) API_PATH="${2:-}"; shift 2 || { usage; exit 2; } ;;
85
+ -h | --help) usage; exit 2 ;;
86
+ *) printf 'Unknown argument: %s\n' "$1" >&2; usage; exit 2 ;;
87
+ esac
88
+ done
89
+
90
+ if [ -z "$DIR" ] || [ ! -f "$DIR/package.json" ]; then
91
+ printf 'REFUSED: no package.json in %s\n' "${DIR:-<empty>}" >&2
92
+ exit 2
93
+ fi
94
+ DIR="$(cd "$DIR" && pwd)"
95
+ case "$PORT_ARG" in
96
+ '' | *[!0-9]*) printf 'REFUSED: --port must be a number\n' >&2; exit 2 ;;
97
+ esac
98
+ case "$API_PATH" in
99
+ /*) ;;
100
+ *) printf 'REFUSED: --api must start with /\n' >&2; exit 2 ;;
101
+ esac
102
+
103
+ case "$ENTRY_ARG" in
104
+ '' | /*) ;;
105
+ *) ENTRY_ARG="$DIR/$ENTRY_ARG" ;;
106
+ esac
107
+
108
+ # 0 when DIR's package.json lists a server framework the skill supports.
109
+ has_api_framework() {
110
+ local fw
111
+ for fw in $API_FRAMEWORKS; do
112
+ if pf_has_dep "$1" "$fw"; then return 0; fi
113
+ done
114
+ return 1
115
+ }
116
+
117
+ pf_require_tool node || exit 2
118
+ if ! pf_node_at_least "$NODE_MIN"; then
119
+ printf 'REFUSED: Node.js %s or newer is required\n' "$NODE_MIN" >&2
120
+ exit 2
121
+ fi
122
+
123
+ STACK="$(pf_detect_stack "$DIR" "$(pf_index_file)")"
124
+ case "$STACK" in
125
+ "$STACK_ID") ;;
126
+ refused:*) printf 'REFUSED: %s\n' "$(pf_stack_refusal "$DIR" "$(pf_index_file)" "${STACK#refused:}")" >&2; exit 2 ;;
127
+ *) printf 'REFUSED: package.json does not describe a React + Vite app (detected: %s)\n' "$STACK" >&2; exit 2 ;;
128
+ esac
129
+
130
+ # Frameworks that render React on a server also list vite and react. This skill is for a single-page app.
131
+ # A TanStack Start app is detected as tanstack-start only when it also lists @tanstack/react-router.
132
+ if pf_has_dep "$DIR" @tanstack/react-start; then
133
+ printf 'REFUSED: package.json lists @tanstack/react-start but not @tanstack/react-router. The TanStack Start skill needs %s; this skill is for a React + Vite single-page app only.\n' \
134
+ "$(pf_stack_version_message tanstack-start)" >&2
135
+ exit 2
136
+ fi
137
+ for fw in $SSR_FRAMEWORKS; do
138
+ if pf_has_dep "$DIR" "$fw"; then
139
+ printf 'REFUSED: package.json lists %s, which renders on a server. This skill is for a React + Vite single-page app only.\n' "$fw" >&2
140
+ exit 2
141
+ fi
142
+ done
143
+
144
+ check_pm() {
145
+ local dir="$1" pm="$2"
146
+ if ! pf_pm_supported "$pm"; then
147
+ printf 'REFUSED: %s: %s\n' "$dir" "$(pf_pm_refusal "$pm")" >&2
148
+ exit 2
149
+ fi
150
+ pf_require_tool "$(pf_pm_tool "$pm")" || exit 2
151
+ }
152
+
153
+ PM="$(pf_detect_pm "$DIR")"
154
+ check_pm "$DIR" "$PM"
155
+
156
+ # The API's folder: --api-dir (relative to DIR or absolute); else DIR when its package.json lists Express,
157
+ # Fastify or Hono; else DIR/api or DIR/server when that folder's package.json does. The skill adds Passflow
158
+ # to an existing API and never creates one.
159
+ API_DIR=''
160
+ if [ -n "$API_DIR_ARG" ]; then
161
+ case "$API_DIR_ARG" in
162
+ /*) API_DIR="$API_DIR_ARG" ;;
163
+ *) API_DIR="$DIR/$API_DIR_ARG" ;;
164
+ esac
165
+ if [ ! -f "$API_DIR/package.json" ]; then
166
+ printf 'REFUSED: no package.json in --api-dir %s\n' "$API_DIR" >&2
167
+ exit 2
168
+ fi
169
+ API_DIR="$(cd "$API_DIR" && pwd)"
170
+ elif has_api_framework "$DIR"; then
171
+ API_DIR="$DIR"
172
+ else
173
+ for candidate in api server; do
174
+ if [ -f "$DIR/$candidate/package.json" ] && has_api_framework "$DIR/$candidate"; then
175
+ API_DIR="$DIR/$candidate"
176
+ break
177
+ fi
178
+ done
179
+ fi
180
+ if [ -z "$API_DIR" ] || ! has_api_framework "$API_DIR"; then
181
+ printf 'REFUSED: no API found: neither %s nor its api/ or server/ folder lists express, fastify or hono. This skill protects an existing API and never creates one: add an API first, then run again.\n' \
182
+ "${API_DIR:-$DIR}/package.json" >&2
183
+ exit 2
184
+ fi
185
+
186
+ API_PM="$PM"
187
+ if [ "$API_DIR" != "$DIR" ]; then
188
+ API_PM="$(pf_detect_pm "$API_DIR")"
189
+ # An API folder with no lockfile of its own installs with the root's manager.
190
+ [ "$API_PM" != none ] || API_PM="$PM"
191
+ check_pm "$API_DIR" "$API_PM"
192
+ fi
193
+
194
+ ENV_FILE="$DIR/$ENV_FILE_NAME"
195
+
196
+ # ---------------------------------------------------------------- static
197
+
198
+ # Env names that look secret, from the env file. Names only.
199
+ secret_looking_names() {
200
+ [ -f "$ENV_FILE" ] || return 0
201
+ sed -n 's/^[[:space:]]*\(export[[:space:]]\{1,\}\)\{0,1\}\([A-Za-z_][A-Za-z0-9_]*\)[[:space:]]*=.*/\2/p' "$ENV_FILE" |
202
+ grep -E 'SECRET|PASSWORD|PRIVATE' | LC_ALL=C sort -u || true
203
+ }
204
+
205
+ # The env files `vite build` reads (production mode), in Vite's order.
206
+ VITE_BUILD_ENV_FILES='.env .env.local .env.production .env.production.local'
207
+
208
+ # vite_env_sets NAME: 0 when one of the env files a production build reads gives NAME a non-empty value.
209
+ vite_env_sets() {
210
+ local f
211
+ for f in $VITE_BUILD_ENV_FILES; do
212
+ [ -f "$DIR/$f" ] || continue
213
+ if [ -z "$(pf_env_missing_keys "$DIR/$f" "$1")" ]; then return 0; fi
214
+ done
215
+ return 1
216
+ }
217
+
218
+ # The folders that hold the API's code: the API folder when it is separate, else DIR/server and DIR/api.
219
+ api_code_dirs() {
220
+ local sub
221
+ if [ "$API_DIR" != "$DIR" ]; then
222
+ printf '%s\n' "$API_DIR"
223
+ return 0
224
+ fi
225
+ for sub in server api; do
226
+ if [ -d "$DIR/$sub" ]; then printf '%s\n' "$DIR/$sub"; fi
227
+ done
228
+ }
229
+
230
+ # resolve_import FILE SPEC: the absolute path a relative import SPEC in FILE points at (without checking that
231
+ # a file is there), or nothing when its folder does not exist.
232
+ resolve_import() {
233
+ local base parent leaf
234
+ base="$(dirname "$1")/$2"
235
+ parent="$(dirname "$base")"
236
+ leaf="$(basename "$base")"
237
+ parent="$(cd "$parent" 2>/dev/null && pwd -P)" || return 0
238
+ printf '%s/%s\n' "$parent" "$leaf"
239
+ }
240
+
241
+ # One line per import in src/ that reaches server code: "FAIL<TAB><file>:<line>" for @passflow/node, a server
242
+ # framework, or a relative import that resolves into the API's code; "NOTE<TAB><file>:<line>" for an alias
243
+ # such as @/server/ or ~/api/, whose target this script cannot resolve. A relative import into src/api/ (a
244
+ # browser client) is not a hit.
245
+ server_imports_in_src() {
246
+ local api_dirs tab f line n spec target d
247
+ tab="$(printf '\t')"
248
+ api_dirs="$(api_code_dirs | while IFS= read -r d; do (cd "$d" && pwd -P); done)"
249
+ find "$DIR/src" -type f \( -name '*.ts' -o -name '*.tsx' -o -name '*.js' -o -name '*.jsx' -o -name '*.mjs' \) -print 2>/dev/null |
250
+ while IFS= read -r f; do
251
+ { grep -noE -- "(from|import|require\\()[[:space:]]*['\"][^'\"]+['\"]" "$f" 2>/dev/null || true; } |
252
+ while IFS= read -r line; do
253
+ n="${line%%:*}"
254
+ spec="$(printf '%s' "${line#*:}" | sed -E "s/^.*['\"]([^'\"]+)['\"]\$/\\1/")"
255
+ case "$spec" in
256
+ @passflow/node | @passflow/node/* | express | fastify | fastify/* | hono | hono/* | @hono/node-server | @hono/node-server/*)
257
+ printf 'FAIL%s%s:%s\n' "$tab" "${f#"$DIR"/}" "$n"
258
+ ;;
259
+ ./* | ../*)
260
+ target="$(resolve_import "$f" "$spec")"
261
+ [ -n "$target" ] || continue
262
+ printf '%s\n' "$api_dirs" | while IFS= read -r d; do
263
+ [ -n "$d" ] || continue
264
+ case "$target/" in
265
+ "$d"/*) printf 'FAIL%s%s:%s\n' "$tab" "${f#"$DIR"/}" "$n" ;;
266
+ esac
267
+ done
268
+ ;;
269
+ @/server/* | @/api/* | \~/server/* | \~/api/* | '#server/'* | '#api/'*)
270
+ printf 'NOTE%s%s:%s\n' "$tab" "${f#"$DIR"/}" "$n"
271
+ ;;
272
+ esac
273
+ done
274
+ done
275
+ }
276
+
277
+ # The API's own type-check: its package's when it has one, else server/tsconfig.json or api/tsconfig.json.
278
+ typecheck_api() {
279
+ local sub
280
+ if [ "$API_DIR" != "$DIR" ]; then
281
+ pf_typecheck "$API_DIR" "$API_PM"
282
+ return $?
283
+ fi
284
+ for sub in server api; do
285
+ if [ -f "$DIR/$sub/tsconfig.json" ]; then
286
+ pf_exec "$DIR" tsc -p "$sub/tsconfig.json" --noEmit
287
+ return $?
288
+ fi
289
+ done
290
+ return 3
291
+ }
292
+
293
+ run_static() {
294
+ local rc names hits notes name placeholders='' bundle any_bundle=0 secret_names build_failed=0
295
+
296
+ if pf_has_dep "$DIR" "$SPA_PACKAGE"; then
297
+ pf_pass "dependency $SPA_PACKAGE"
298
+ else
299
+ pf_fail "dependency $SPA_PACKAGE" "not in dependencies or devDependencies of $DIR/package.json"
300
+ fi
301
+ if pf_has_dep "$API_DIR" "$API_PACKAGE"; then
302
+ pf_pass "dependency $API_PACKAGE ($API_DIR)"
303
+ else
304
+ pf_fail "dependency $API_PACKAGE" "not in dependencies or devDependencies of $API_DIR/package.json"
305
+ fi
306
+
307
+ if [ -f "$ENV_FILE" ]; then
308
+ names="$(secret_looking_names | grep '^VITE_' || true)"
309
+ if [ -z "$names" ]; then
310
+ pf_pass "no secret-looking VITE_ name in $ENV_FILE_NAME"
311
+ else
312
+ pf_fail "no secret-looking VITE_ name in $ENV_FILE_NAME" "$(printf '%s' "$names" | tr '\n' ' ')"
313
+ fi
314
+ else
315
+ pf_skip "VITE_ names in $ENV_FILE_NAME" "no $ENV_FILE_NAME"
316
+ fi
317
+
318
+ rc=0
319
+ pf_install "$DIR" >"$WORK/install.log" 2>&1 || rc=$?
320
+ if [ "$rc" -eq 0 ]; then
321
+ pf_pass "install ($PM)"
322
+ else
323
+ pf_fail "install ($PM)" "exit $rc; log: $WORK/install.log"
324
+ return 0
325
+ fi
326
+ if [ "$API_DIR" != "$DIR" ]; then
327
+ rc=0
328
+ pf_install "$API_DIR" "$API_PM" >"$WORK/install-api.log" 2>&1 || rc=$?
329
+ if [ "$rc" -eq 0 ]; then
330
+ pf_pass "install API ($API_PM)"
331
+ else
332
+ pf_fail "install API ($API_PM)" "exit $rc; log: $WORK/install-api.log"
333
+ return 0
334
+ fi
335
+ fi
336
+
337
+ rc=0
338
+ pf_typecheck "$DIR" >"$WORK/typecheck.log" 2>&1 || rc=$?
339
+ case "$rc" in
340
+ 0) pf_pass "typecheck (SPA)" ;;
341
+ 3) pf_skip "typecheck (SPA)" "no typecheck script and no tsconfig.json" ;;
342
+ *) pf_fail "typecheck (SPA)" "exit $rc; log: $WORK/typecheck.log" ;;
343
+ esac
344
+
345
+ rc=0
346
+ typecheck_api >"$WORK/typecheck-api.log" 2>&1 || rc=$?
347
+ case "$rc" in
348
+ 0) pf_pass "typecheck (API)" ;;
349
+ 3) pf_skip "typecheck (API)" "no tsconfig.json for the API (server/tsconfig.json from the skill's assets)" ;;
350
+ *) pf_fail "typecheck (API)" "exit $rc; log: $WORK/typecheck-api.log" ;;
351
+ esac
352
+
353
+ if pf_has_script "$DIR" build; then
354
+ # A VITE_ name with no value makes the app throw at load, and the bundler then drops the rest of it.
355
+ # Build every missing one with a placeholder, so the bundle holds the whole app for the scans below.
356
+ # A name counts as set when the environment or any env file Vite reads for a production build sets it;
357
+ # a placeholder never overrides a real value.
358
+ for name in $SPA_KEYS; do
359
+ if [ -z "${!name:-}" ] && ! vite_env_sets "$name"; then
360
+ placeholders="$placeholders $name"
361
+ fi
362
+ done
363
+ rc=0
364
+ (
365
+ for name in $placeholders; do
366
+ case "$name" in
367
+ VITE_PASSFLOW_URL) export VITE_PASSFLOW_URL='https://passflow.invalid' ;;
368
+ VITE_PASSFLOW_APP_ID) export VITE_PASSFLOW_APP_ID='placeholder-app-id' ;;
369
+ VITE_API_URL) export VITE_API_URL='https://api.invalid' ;;
370
+ esac
371
+ done
372
+ pf_run_script "$DIR" build
373
+ ) >"$WORK/build.log" 2>&1 || rc=$?
374
+ if [ "$rc" -eq 0 ]; then
375
+ pf_pass build
376
+ if [ -n "$placeholders" ]; then
377
+ pf_info "built with placeholder values for:$placeholders (no env file nor the environment sets them); the build is scanned, then removed."
378
+ fi
379
+ else
380
+ build_failed=1
381
+ pf_fail build "exit $rc; log: $WORK/build.log"
382
+ fi
383
+ else
384
+ pf_skip build "no build script"
385
+ fi
386
+
387
+ # shellcheck disable=SC2086
388
+ secret_names="$({
389
+ printf '%s\n' $FOREIGN_SECRET_KEYS
390
+ pf_stack_get "$(pf_index_file)" "$STACK_ID" secret_env_keys
391
+ secret_looking_names
392
+ } | LC_ALL=C sort -u)"
393
+ for bundle in $CLIENT_BUNDLE_DIRS; do
394
+ # After a failed build the folder holds an older build: scanning it would prove nothing.
395
+ [ "$build_failed" -eq 0 ] || break
396
+ [ -d "$DIR/$bundle" ] || continue
397
+ any_bundle=1
398
+ rc=0
399
+ # shellcheck disable=SC2086
400
+ pf_scan_bundle "$DIR/$bundle" "$ENV_FILE" $secret_names >"$WORK/bundle-secrets.log" 2>&1 || rc=$?
401
+ if [ "$rc" -eq 0 ]; then
402
+ pf_pass "no secret in $bundle/"
403
+ else
404
+ pf_fail "no secret in $bundle/" "see below"
405
+ cat "$WORK/bundle-secrets.log"
406
+ fi
407
+ # The API's env names appear in a bundle only when server code was imported into the SPA. Names only:
408
+ # their values are public or equal to VITE_ values.
409
+ rc=0
410
+ # shellcheck disable=SC2086
411
+ pf_scan_bundle "$DIR/$bundle" '' $API_KEYS >"$WORK/bundle-server.log" 2>&1 || rc=$?
412
+ if [ "$rc" -eq 0 ]; then
413
+ pf_pass "no server code in $bundle/"
414
+ else
415
+ pf_fail "no server code in $bundle/" "the SPA imports API code; see below"
416
+ cat "$WORK/bundle-server.log"
417
+ fi
418
+ done
419
+ if [ "$build_failed" -eq 1 ]; then
420
+ pf_skip "bundle scan" "the build failed"
421
+ elif [ "$any_bundle" -eq 0 ]; then
422
+ pf_skip "bundle scan" "no build output in $CLIENT_BUNDLE_DIRS"
423
+ fi
424
+
425
+ # The placeholder build must never be deployed: remove it now that it is scanned.
426
+ if [ -n "$placeholders" ] && [ "$build_failed" -eq 0 ]; then
427
+ for bundle in $CLIENT_BUNDLE_DIRS; do
428
+ if [ -d "$DIR/$bundle" ]; then rm -rf "${DIR:?}/$bundle"; fi
429
+ done
430
+ pf_info "removed $CLIENT_BUNDLE_DIRS/: it was built with placeholder values. Build again with real values to deploy."
431
+ fi
432
+
433
+ if [ -d "$DIR/src" ]; then
434
+ names="$(server_imports_in_src || true)"
435
+ hits="$(printf '%s\n' "$names" | sed -n 's/^FAIL //p')"
436
+ notes="$(printf '%s\n' "$names" | sed -n 's/^NOTE //p')"
437
+ if [ -z "$hits" ]; then
438
+ pf_pass "no server import in src/"
439
+ else
440
+ pf_fail "no server import in src/" "src/ imports @passflow/node, a server framework or the API's code:"
441
+ printf '%s\n' "$hits" | while IFS= read -r h; do printf ' %s\n' "$h"; done
442
+ fi
443
+ if [ -n "$notes" ]; then
444
+ pf_skip "server imports through an alias" "cannot tell where these point; check by hand that they are not API code:"
445
+ printf '%s\n' "$notes" | while IFS= read -r h; do printf ' %s\n' "$h"; done
446
+ fi
447
+ fi
448
+
449
+ rc=0
450
+ pf_guardrail_scan "$DIR" spa >"$WORK/guardrails.log" 2>&1 || rc=$?
451
+ if [ "$rc" -eq 0 ]; then
452
+ pf_pass guardrails
453
+ else
454
+ pf_fail guardrails "see below"
455
+ cat "$WORK/guardrails.log"
456
+ fi
457
+ }
458
+
459
+ # ---------------------------------------------------------------- run
460
+
461
+ stop_server() {
462
+ if [ -n "$PF_BG_PID" ]; then
463
+ pf_kill_tree "$PF_BG_PID"
464
+ PF_BG_PID=''
465
+ fi
466
+ }
467
+
468
+ run_live() {
469
+ local missing script entry candidate how base www cc app_origin rc
470
+ if [ ! -f "$ENV_FILE" ]; then
471
+ printf 'REFUSED: %s not found; run mode needs it\n' "$ENV_FILE" >&2
472
+ exit 2
473
+ fi
474
+ # shellcheck disable=SC2086
475
+ missing="$(pf_env_missing_keys "$ENV_FILE" $API_KEYS)"
476
+ if [ -n "$missing" ]; then
477
+ printf 'REFUSED: %s has no value for: %s\n' "$ENV_FILE_NAME" "$(printf '%s' "$missing" | tr '\n' ' ')" >&2
478
+ exit 2
479
+ fi
480
+ pf_require_tool curl || exit 2
481
+
482
+ # How the API starts: --entry, else a script, else a usual entry file. verify.sh never adds a script.
483
+ script=''
484
+ entry="$ENTRY_ARG"
485
+ if [ -n "$entry" ]; then
486
+ if [ ! -f "$entry" ]; then
487
+ printf 'REFUSED: --entry %s not found\n' "$entry" >&2
488
+ exit 2
489
+ fi
490
+ elif pf_has_script "$API_DIR" api; then
491
+ script=api
492
+ elif [ "$API_DIR" != "$DIR" ] && pf_has_script "$API_DIR" start; then
493
+ script=start
494
+ elif [ "$API_DIR" != "$DIR" ] && pf_has_script "$API_DIR" dev; then
495
+ script=dev
496
+ else
497
+ for candidate in server/index.ts server/index.js server/index.mjs api/index.ts api/index.js src/index.ts src/server.ts; do
498
+ if [ -f "$API_DIR/$candidate" ]; then
499
+ entry="$API_DIR/$candidate"
500
+ break
501
+ fi
502
+ done
503
+ if [ -z "$entry" ]; then
504
+ printf 'REFUSED: cannot tell how to start the API in %s: no api script and no server/index.ts. Pass its entry file with --entry.\n' "$API_DIR" >&2
505
+ exit 2
506
+ fi
507
+ fi
508
+
509
+ if ! pf_port_free "$PORT_ARG"; then
510
+ printf 'REFUSED: %s\n' "$(pf_port_busy_refusal "$PORT_ARG")" >&2
511
+ exit 2
512
+ fi
513
+
514
+ # shellcheck disable=SC2086
515
+ pf_load_env "$ENV_FILE" $API_KEYS
516
+
517
+ # A separate API's start script usually runs its build output: build first, so the run tests the current
518
+ # code.
519
+ if [ "$script" = start ] && pf_has_script "$API_DIR" build; then
520
+ rc=0
521
+ pf_run_script "$API_DIR" build "$API_PM" >"$WORK/build-api.log" 2>&1 || rc=$?
522
+ if [ "$rc" -eq 0 ]; then
523
+ pf_pass "build (API)"
524
+ else
525
+ pf_fail "build (API)" "exit $rc; log: $WORK/build-api.log"
526
+ return 0
527
+ fi
528
+ fi
529
+
530
+ # APP_BASE_URL reduced to its origin, as the API's CORS allows it.
531
+ app_origin="$(printf '%s' "$APP_BASE_URL" | sed -E 's#^([A-Za-z][A-Za-z0-9+.-]*://[^/?#]+).*#\1#')"
532
+ export PORT="$PORT_ARG"
533
+ pf_traps stop_server
534
+ if [ -n "$script" ]; then
535
+ how="$script script"
536
+ case "$API_PM" in
537
+ npm) pf_start_bg "$API_DIR" "$WORK/server.log" npm run "$script" ;;
538
+ pnpm) pf_start_bg "$API_DIR" "$WORK/server.log" pnpm run "$script" ;;
539
+ yarn-classic) pf_start_bg "$API_DIR" "$WORK/server.log" yarn run "$script" ;;
540
+ esac
541
+ else
542
+ how="node ${entry#"$DIR"/}"
543
+ case "$entry" in
544
+ *.ts | *.mts) pf_start_bg "$API_DIR" "$WORK/server.log" node --experimental-strip-types "$entry" ;;
545
+ *) pf_start_bg "$API_DIR" "$WORK/server.log" node "$entry" ;;
546
+ esac
547
+ fi
548
+
549
+ # localhost, not 127.0.0.1: an API bound to localhost may listen on ::1 only.
550
+ base="http://localhost:$PORT_ARG"
551
+ if pf_wait_http "$base$API_PATH" 60 "$PF_BG_PID"; then
552
+ pf_pass "API starts on port $PORT_ARG ($how)"
553
+ elif pf_alive "$PF_BG_PID"; then
554
+ pf_fail "API starts on port $PORT_ARG ($how)" "no answer within 60 s; log: $WORK/server.log"
555
+ return 0
556
+ else
557
+ pf_fail "API starts on port $PORT_ARG ($how)" "the process exited; log: $WORK/server.log"
558
+ return 0
559
+ fi
560
+ pf_check_listener "$PORT_ARG" "$PF_BG_PID"
561
+
562
+ pf_http "$WORK/no-token" GET "$base$API_PATH" || true
563
+ www="$(pf_header "$WORK/no-token" WWW-Authenticate)"
564
+ cc="$(pf_header "$WORK/no-token" Cache-Control)"
565
+ if [ "$(cat "$WORK/no-token/status")" = 401 ] && [ "$www" = Bearer ]; then
566
+ pf_pass "GET $API_PATH without a token: 401, WWW-Authenticate: Bearer"
567
+ else
568
+ pf_fail "GET $API_PATH without a token" "status $(cat "$WORK/no-token/status"), WWW-Authenticate '${www}'; want 401 and 'Bearer' (status 000: no answer)"
569
+ fi
570
+ if pf_has_token "$cc" no-store; then
571
+ pf_pass "GET $API_PATH without a token: Cache-Control no-store"
572
+ else
573
+ pf_fail "GET $API_PATH without a token" "Cache-Control '${cc}' has no no-store"
574
+ fi
575
+
576
+ pf_http "$WORK/bogus-token" GET "$base$API_PATH" -H 'Authorization: Bearer not.a.token' || true
577
+ www="$(pf_header "$WORK/bogus-token" WWW-Authenticate)"
578
+ case "$(cat "$WORK/bogus-token/status") $www" in
579
+ '401 Bearer error="invalid_token"'*) pf_pass "GET $API_PATH with an invalid token: 401 invalid_token" ;;
580
+ *) pf_fail "GET $API_PATH with an invalid token" "status $(cat "$WORK/bogus-token/status"), WWW-Authenticate '${www}'; want 401 and error=\"invalid_token\"" ;;
581
+ esac
582
+
583
+ if [ "$NO_CORS" -eq 1 ]; then
584
+ pf_skip CORS "--no-cors: the SPA calls the API on its own origin"
585
+ elif ! api_has_cors_package; then
586
+ pf_skip CORS "the API has no CORS package ($(api_cors_package)); the skill leaves CORS to the developer. Pass --no-cors for a same-origin setup"
587
+ else
588
+ check_cors "$base" "$app_origin"
589
+ fi
590
+
591
+ if ! pf_alive "$PF_BG_PID"; then
592
+ pf_fail "API still running after the checks" "the process exited; log: $WORK/server.log"
593
+ fi
594
+ stop_server
595
+ }
596
+
597
+ # The CORS package the API's framework needs: cors (Express), @fastify/cors (Fastify), none for Hono.
598
+ api_cors_package() {
599
+ if pf_has_dep "$API_DIR" express; then
600
+ printf 'cors\n'
601
+ elif pf_has_dep "$API_DIR" fastify; then
602
+ printf '@fastify/cors\n'
603
+ else
604
+ printf 'hono/cors\n'
605
+ fi
606
+ }
607
+
608
+ # 0 when the API can mount CORS: Hono always (hono/cors ships with it), Express with cors, Fastify with
609
+ # @fastify/cors.
610
+ api_has_cors_package() {
611
+ local pkg
612
+ pkg="$(api_cors_package)"
613
+ [ "$pkg" = hono/cors ] && return 0
614
+ pf_has_dep "$API_DIR" "$pkg"
615
+ }
616
+
617
+ # check_cors BASE APP_ORIGIN: the 401 to the SPA's origin allows it and exposes WWW-Authenticate, the preflight
618
+ # allows Authorization, and another origin is not allowed. Each answer must be an HTTP answer (status 000,
619
+ # no answer, is a FAIL), and header lists are matched by whole items.
620
+ check_cors() {
621
+ local base="$1" app_origin="$2" status allow expose allow_headers
622
+ pf_http "$WORK/origin" GET "$base$API_PATH" -H "Origin: $app_origin" || true
623
+ status="$(cat "$WORK/origin/status")"
624
+ allow="$(pf_header "$WORK/origin" Access-Control-Allow-Origin)"
625
+ expose="$(pf_header "$WORK/origin" Access-Control-Expose-Headers)"
626
+ if [ "$status" = 401 ] && [ "$allow" = "$app_origin" ] && pf_has_token "$expose" WWW-Authenticate; then
627
+ pf_pass "CORS: the 401 to APP_BASE_URL's origin ($app_origin) exposes WWW-Authenticate"
628
+ else
629
+ pf_fail "CORS: the answer to $app_origin" "status $status, Access-Control-Allow-Origin '${allow}', Access-Control-Expose-Headers '${expose}'; want 401, the origin and WWW-Authenticate"
630
+ fi
631
+
632
+ pf_http "$WORK/preflight" OPTIONS "$base$API_PATH" -H "Origin: $app_origin" \
633
+ -H 'Access-Control-Request-Method: GET' -H 'Access-Control-Request-Headers: authorization' || true
634
+ status="$(cat "$WORK/preflight/status")"
635
+ allow="$(pf_header "$WORK/preflight" Access-Control-Allow-Origin)"
636
+ allow_headers="$(pf_header "$WORK/preflight" Access-Control-Allow-Headers)"
637
+ case "$status" in
638
+ 2??)
639
+ if [ "$allow" = "$app_origin" ] && pf_has_token "$allow_headers" authorization; then
640
+ pf_pass "CORS: the preflight from $app_origin allows the Authorization header"
641
+ else
642
+ pf_fail "CORS: the preflight from $app_origin" "Access-Control-Allow-Origin '${allow}', Access-Control-Allow-Headers '${allow_headers}'; want the origin and authorization"
643
+ fi
644
+ ;;
645
+ *) pf_fail "CORS: the preflight from $app_origin" "status $status; want 2xx (is CORS mounted before the routes?)" ;;
646
+ esac
647
+
648
+ pf_http "$WORK/other-origin" GET "$base$API_PATH" -H 'Origin: https://other-origin.invalid' || true
649
+ status="$(cat "$WORK/other-origin/status")"
650
+ allow="$(pf_header "$WORK/other-origin" Access-Control-Allow-Origin)"
651
+ if [ "$status" != 401 ]; then
652
+ pf_fail "CORS: another origin is refused" "status $status; want the signed-out 401 without its origin allowed"
653
+ else
654
+ case "$allow" in
655
+ '*' | 'https://other-origin.invalid') pf_fail "CORS: another origin is refused" "Access-Control-Allow-Origin is '${allow}'; want only $app_origin" ;;
656
+ *) pf_pass "CORS: another origin is refused" ;;
657
+ esac
658
+ fi
659
+ }
660
+
661
+
662
+ WORK="$(mktemp -d "$PF_TMP/passflow-verify.XXXXXX")"
663
+ printf 'verify.sh %s: %s (%s, %s)\n' "$MODE" "$DIR" "$STACK_ID" "$PM"
664
+ if [ "$API_DIR" != "$DIR" ]; then
665
+ printf 'API: %s (%s)\n' "$API_DIR" "$API_PM"
666
+ fi
667
+ printf 'logs: %s\n' "$WORK"
668
+
669
+ if [ "$MODE" = static ]; then
670
+ run_static
671
+ else
672
+ run_live
673
+ fi
674
+
675
+ if pf_result; then
676
+ exit 0
677
+ fi
678
+ exit 1