Repository navigation
fix(gcs): enforce the bucket CORS configuration on preflight and object reads - #101
Conversation
…ct reads CORS responses reflected any Origin and advertised a fixed method/header set unrelated to the bucket's configuration, so an origin the bucket forbids was granted access and a disallowed method passed preflight. Bucket CORS was also never applied to path-style (XML API) object reads, because bucket extraction only recognised the JSON API URL prefixes. Match a rule on both origin and method (honouring '*'), emit no CORS headers at all when nothing matches — as real GCS does, so the browser blocks the request — and resolve the bucket from path-style URLs too. Actual responses now carry only Allow-Origin/Vary/Expose-Headers, since Allow-Methods, Allow-Headers and Max-Age are preflight-only. Closes floci-io#99 Closes floci-io#98
|
| Filename | Overview |
|---|---|
| src/main/java/io/floci/gcp/services/gcs/GcsCorsFilter.java | Enforces configured origin-and-method matching and constrains path-style extraction to the matched XML resource, resolving the previously reported route-boundary leak. |
| src/test/java/io/floci/gcp/services/gcs/GcsCorsRestIntegrationTest.java | Adds comprehensive integration coverage for CORS authorization, response headers, API URL forms, route collisions, and buckets without CORS configuration. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Request with Origin] --> B{Preflight OPTIONS?}
B -->|Yes| C[Resolve bucket from known API prefix or XML route]
B -->|No| D[Resolve bucket and use actual method]
C --> E{Origin and requested method match CORS rule?}
D --> E
E -->|No| F[Emit no CORS headers]
E -->|Yes, preflight| G[Emit Allow-Origin, Methods, Headers, Max-Age, and Vary]
E -->|Yes, actual request| H[Emit Allow-Origin, Expose-Headers, and Vary]
Reviews (2): Last reviewed commit: "fix(gcs): only treat a leading path segm..." | Re-trigger Greptile
… routes The path-style fallback read the first path segment of every request, so an endpoint whose leading segment matched an existing bucket name (a bucket called 'batch' and the '/batch/storage/v1' route, for example) inherited that bucket's CORS configuration. Gate the fallback on the request having been routed to the path-style object controller, which JAX-RS has already resolved by the time the filter runs.
|
the route collision — it reproduced exactly as described: with a bucket named Fixed in 42f61d3 by gating the fallback on the request having actually been routed to the path-style object controller ( Also added the missing regression test ( Full suite: 445 tests, 0 failures. |
|
Thanks for chasing this down, and the reproduction in your comment is exactly the right level of detail. Gating the path-style bucket extraction on the request actually having been routed to the XML controller is a cleaner fix than I would have suggested, and it also settles the scoping question I raised last time: unrelated endpoints no longer resolve a bucket at all, rather than resolving one that happens to share a name. I checked the one thing that could have made this silently inert, which is whether the filter runs before matching. It does not, so No blockers from my side, this looks ready to me. |
|
🎉 This PR is included in version 0.7.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
# [0.7.0](floci-io/floci-gcp@0.6.0...0.7.0) (2026-08-18) ### Bug Fixes * **firestore:** enforce write preconditions and transaction conflicts ([floci-io#114](floci-io#114)) ([a8f4166](floci-io@a8f4166)) * **gcs:** correct resumable upload chunk handling ([floci-io#127](floci-io#127)) ([d8fe29e](floci-io@d8fe29e)) * **gcs:** emit x-goog-meta headers from the same generation as the downloaded bytes ([floci-io#123](floci-io#123)) ([6ea777d](floci-io@6ea777d)) * **gcs:** enforce object preconditions atomically ([floci-io#94](floci-io#94)) ([1384ebc](floci-io@1384ebc)) * **gcs:** enforce the bucket CORS configuration on preflight and object reads ([floci-io#101](floci-io#101)) ([6d16174](floci-io@6d16174)), closes [floci-io#99](floci-io#99) [floci-io#98](floci-io#98) * **gcs:** persist custom object metadata on upload paths ([floci-io#120](floci-io#120)) ([4c2c013](floci-io@4c2c013)), closes [floci-io#119](floci-io#119) ### Features * **release:** one-button release cut from main ([floci-io#118](floci-io#118)) ([aff208b](floci-io@aff208b)) * **sts:** add downscoped token exchange ([floci-io#115](floci-io#115)) ([6867225](floci-io@6867225))
Summary
Closes #99
Closes #98
GcsCorsFilternever actually enforced the bucket's CORS configuration:Access-Control-Allow-Originwas set unconditionally, even when no CORS rule matched the origin.GET, POST, PUT, DELETE, OPTIONS, HEAD/Content-Type, Authorization, X-Goog-*.Access-Control-Request-Methodwas ignored, so a preflight for a method the bucket forbids (e.g.DELETE) passed.extractBucketonly recognised the JSON API prefixes, soGET /{bucket}/{object}(the formblob.public_urlproduces) never resolved a bucket, which is [BUG] GCS: bucket CORS not applied to path-style object GET/HEAD (XML API), only to preflight #98.rule == null && bucket == nullguard still emittedAccess-Control-Allow-Originwhenever the bucket existed but permitted nothing.Net effect: cross-origin access the bucket configuration forbids was granted, so client code (and tests) exercising a CORS-denial path silently passed against the emulator.
Type of change
fix:)feat:)feat!:orfix!:)GCP Compatibility
Verified against the documented real-GCS behaviour (bucket CORS, CORS support) using the reproduction from #99/#98 — bucket CORS
origin: ["http://allowed.example"],method: ["GET"],responseHeader: ["Content-Type"],maxAgeSeconds: 3600:OPTIONSfromhttp://evil.example, ACRMDELETEACAO: http://evil.example+ hardcoded method listOPTIONSfrom allowed origin, ACRMDELETEOPTIONSfrom allowed origin, ACRMGETAllow-Methods: GET,Allow-Headers: Content-Type,Max-Age: 3600GET /{bucket}/{object}(XML API) from allowed originACAO+Expose-Headers: Content-Type+Vary: OriginGETfrom disallowed origin (either URL form)ACAOreflectedImplementation notes:
*in either list; the first matching rule wins.findBucket, so a non-GCS path simply resolves no bucket and no headers are added — mis-extraction is self-correcting.Allow-Origin,VaryandExpose-Headers;Allow-Methods,Allow-HeadersandMax-Ageare preflight-only per the CORS spec and are no longer emitted on object reads.Checklist
./mvnw testpasses locally (full suite: 444 tests, 0 failures)GcsCorsRestIntegrationTest(8 cases): allowed preflight advertises the configured method/header/max-age, disallowed origin and disallowed method each get no CORS headers, path-style and JSON API reads from an allowed origin carry the headers, both denial paths, and a bucket without CORS configuration gets nothing. There was no CORS coverage before.