Skip to content

fix(worker): pass worker checks; fix CORS origin validation and module-ID validation - #20

Merged
marvelefe merged 1 commit into
mainfrom
fix/worker-tests-and-env
Oct 17, 2026
Merged

marvelefe merged 1 commit into
mainfrom
fix/worker-tests-and-env

Conversation

@marvelefe

Copy link
Copy Markdown
Owner

Summary

Makes the Cloudflare Worker sub-project's checks pass and fixes two genuine bugs found while doing so — including the CORS vulnerability in #4. This also lets the worker job be enabled in CI (#18).

🐛 Real bugs fixed

  1. CORS reflected any origin (worker/src/middleware/cors.ts) — Closes Worker CORS reflects any Origin with credentials enabled #4.
    getCorsHeaders(origin) echoed whatever Origin the caller sent and always set
    Access-Control-Allow-Credentials: true, so any website could make credentialed
    cross-origin requests
    to the auth API. Production FRONTEND_URL was never used.
    Now the origin is echoed only when it matches FRONTEND_URL, otherwise the
    configured origin is returned (never *), and the credentials header is gone
    (the API authenticates with Authorization: Bearer, not cookies).
  2. CORS threw on redirect responses. c.res.headers.set(...) throws
    TypeError: immutable on Hono's 302 redirects (the OAuth callback). It now
    rebuilds the response from a copied Headers, preserving status, Location, etc.
  3. Module-ID validation rejected every real sync payload. isValidModuleId
    required a domain/module pair, but the app sends bare IDs
    (loadQuiz('decision-trees', …), each module's config.json "id") — so every
    progress sync would have been rejected with a 400. It now accepts one or two
    kebab-case segments.
  4. Database errors crashed app.fetch(). worker/src/services/database.ts
    threw plain objects, which Hono does not forward to onError (only
    instanceof Error is). Throws now use the existing typed ApiError helpers, so
    DB failures return 500 { code: 'DATABASE_ERROR' } as intended.

🔧 Test fixes (no test deleted, skipped or weakened)

  • DB: → ml_platform_db: in mock envs (stale binding name; wrangler.toml and
    the Env interface use ml_platform_db).
  • mockenv → mockEnv typo.
  • Bookmark fixtures given the required fields, and the BookmarkWithParams[] type.
  • CORS expectations updated to the secure implementation's actual method/header
    lists — all origin-validation assertions kept unchanged.
  • validation.test.ts fixtures corrected to the real (bare) module-ID format, with
    a bare-ID case added to the validateProgressData valid fixture as regression
    coverage.

🧹 Also

✅ Verification

cd worker && npm run type-check   # exit 0
cd worker && npm run test         # 12 files, 223/223 passing
Check Before After
Worker npm run type-check ❌ ~24 errors ✅ 0
Worker npm run test ❌ 28 failed / 195 passed ✅ 223 passed

…e-ID validation

- CORS: echo the request origin only when it matches FRONTEND_URL instead of
  reflecting any origin, and drop Access-Control-Allow-Credentials (the API
  authenticates via Authorization: Bearer). Closes #4
- CORS: rebuild the response instead of mutating c.res.headers, which throws
  TypeError: immutable on Hono redirect responses
- validation: accept the app's bare kebab-case module IDs (e.g.
  "linear-regression"); the previous domain/module-only rule would have
  rejected every real progress sync
- database: throw typed ApiError subclasses so Hono forwards DB failures as
  500 JSON instead of rejecting app.fetch()
- tests: use the real ml_platform_db binding, fix the mockenv typo and
  bookmark fixtures; correct stale CORS/validation expectations. No test was
  deleted, skipped or weakened
- ci: enable the worker type-check + test job. Closes #18
- chore: replace the real GA measurement ID in .env.example. Closes #16
@marvelefe
marvelefe merged commit 6d47a80 into main Oct 17, 2026
3 of 4 checks passed
@marvelefe
marvelefe deleted the fix/worker-tests-and-env branch October 17, 2026 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant