Title: server: inject CORS headers on Fastify-internal errors (#376) by melvincarvalho · Pull Request #421 · JavaScriptSolidServer/JavaScriptSolidServer · GitHub
Open Graph Title: server: inject CORS headers on Fastify-internal errors (#376) by melvincarvalho · Pull Request #421 · JavaScriptSolidServer/JavaScriptSolidServer
X Title: server: inject CORS headers on Fastify-internal errors (#376) by melvincarvalho · Pull Request #421 · JavaScriptSolidServer/JavaScriptSolidServer
Description: Closes #376. Before $ curl -i -H 'Origin: https://example.com' 'https://example.test/foo%g1' HTTP/2 400 content-type: application/json content-length: 116 # (no access-control-* headers) {"error":"Bad Request","code":"FST_ERR_BAD_URL", ...} Browser-side: surfaces as a generic CORS / network error instead of the real 400, masking the actual failure mode (malformed percent-encoding). After $ curl -i -H 'Origin: https://example.com' 'https://example.test/foo%g1' HTTP/2 400 access-control-allow-origin: https://example.com access-control-allow-methods: GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS access-control-allow-headers: Accept, Authorization, Content-Type, DPoP, ... access-control-expose-headers: Accept-Patch, Accept-Post, ... access-control-allow-credentials: true access-control-max-age: 86400 content-type: application/json; charset=utf-8 {"error":"Bad Request","code":"FST_ERR_BAD_URL","message":"'/foo%g1' is not a valid url component","statusCode":400} Browser sees the real 400 and the actual FST_ERR_BAD_URL reason. The error field uses the HTTP status text ("Bad Request") via STATUS_CODES[statusCode], matching Fastify's default body shape so any pre-fix client that was parsing error keeps working. For non-browser requests (no Origin header), CORS headers are still set with Access-Control-Allow-Origin: * — consistent with the rest of the server, where the global onRequest hook sets CORS unconditionally. Root cause FST_ERR_BAD_URL is thrown by Fastify's URL parser BEFORE the request enters the normal handler chain. Direct probe confirmed neither setErrorHandler nor onSend hooks fire for this code path. Fastify writes the 400 directly via res.writeHead() / res.end() in onBadUrl() (node_modules/fastify/fastify.js:752–771) — bypassing every JSS CORS-injecting handler. Fix Configure Fastify's frameworkErrors option in createServer. This is the explicit hook Fastify provides for framework-level errors (FST_ERR_BAD_URL, FST_ERR_ASYNC_CONSTRAINT). When set, Fastify routes through this function instead of the direct-write path, giving us a Reply object to attach headers to. The handler: Always calls getCorsHeaders(request.headers.origin) and applies the full JSS CORS set — Allow-Origin mirrors the request Origin if present, falls back to * if absent Sends a JSON body with error: STATUS_CODES[statusCode] (HTTP status text, e.g. "Bad Request"), code (the Fastify error code, e.g. "FST_ERR_BAD_URL"), message, and statusCode — same shape as Fastify's default error body Test plan Three new integration tests in test/error-handler.test.js against a live JSS server: %g1 with Origin → 400, ACAO mirrors Origin, full CORS set, body error: "Bad Request" %g1 without Origin → 400, ACAO=*, full CORS set, body error: "Bad Request" Normal 404 path (no FST_ERR) → CORS still injected by the existing wildcard handler — confirms the new hook didn't displace existing behavior Full suite: 777 → 780 pass Refs #371 — Git-Protocol CORS header #374 — GIT_CORS_HEADERS const + 4xx return paths (the PR that surfaced this issue) Fastify frameworkErrors option
Open Graph Description: Closes #376. Before $ curl -i -H 'Origin: https://example.com' 'https://example.test/foo%g1' HTTP/2 400 content-type: application/json content-length: 116 # (no access-control-* hea...
X Description: Closes #376. Before $ curl -i -H 'Origin: https://example.com' 'https://example.test/foo%g1' HTTP/2 400 content-type: application/json content-length: 116 # (no acce...
Opengraph URL: https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/pull/421
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:b47ccb68-d2e9-c42a-cd97-840623c0e73d |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C584:2CA452:1439093:19B940D:6B039F21 |
| html-safe-nonce | 9ef7be28bc8ff39a62bd4072e47b86082d71ad487218c500724560c787f4264e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTg0OjJDQTQ1MjoxNDM5MDkzOjE5Qjk0MEQ6NkIwMzlGMjEiLCJ2aXNpdG9yX2lkIjoiMjI2MzM4MTc0NDU1MDEyNTM0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5e34bd18774006b0a22e0c61fae2c382fdd63acbe95b1b90a594fc78c09ec0ff |
| hovercard-subject-tag | pull_request:3664297243 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/pull/421/files |
| twitter:image | https://avatars.githubusercontent.com/u/65864?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/65864?s=400&v=4 |
| og:image:alt | Closes #376. Before $ curl -i -H 'Origin: https://example.com' 'https://example.test/foo%g1' HTTP/2 400 content-type: application/json content-length: 116 # (no access-control-* hea... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/JavaScriptSolidServer/JavaScriptSolidServer git https://github.com/JavaScriptSolidServer/JavaScriptSolidServer.git |
| octolytics-dimension-user_id | 205442424 |
| octolytics-dimension-user_login | JavaScriptSolidServer |
| octolytics-dimension-repository_id | 958025407 |
| octolytics-dimension-repository_nwo | JavaScriptSolidServer/JavaScriptSolidServer |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 958025407 |
| octolytics-dimension-repository_network_root_nwo | JavaScriptSolidServer/JavaScriptSolidServer |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width