Skip to content

fix(gcs): enforce the bucket CORS configuration on preflight and object reads - #101

Merged
hectorvent merged 2 commits into
floci-io:mainfrom
ankit1324:fix/gcs-enforce-bucket-cors
Aug 12, 2026
Merged

hectorvent merged 2 commits into
floci-io:mainfrom
ankit1324:fix/gcs-enforce-bucket-cors

Conversation

@ankit1324

Copy link
Copy Markdown
Contributor

Summary

Closes #99
Closes #98

GcsCorsFilter never actually enforced the bucket's CORS configuration:

  1. Any origin was granted access — Access-Control-Allow-Origin was set unconditionally, even when no CORS rule matched the origin.
  2. Advertised lists were unrelated to the config — with no matching rule the filter fell back to a hardcoded GET, POST, PUT, DELETE, OPTIONS, HEAD / Content-Type, Authorization, X-Goog-*.
  3. The requested method was never checked — Access-Control-Request-Method was ignored, so a preflight for a method the bucket forbids (e.g. DELETE) passed.
  4. Path-style (XML API) reads got no bucket CORS — extractBucket only recognised the JSON API prefixes, so GET /{bucket}/{object} (the form blob.public_url produces) never resolved a bucket, which is [BUG] GCS: bucket CORS not applied to path-style object GET/HEAD (XML API), only to preflight #98.
  5. The response filter's rule == null && bucket == null guard still emitted Access-Control-Allow-Origin whenever 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

  • Bug fix (fix:)
  • New feature (feat:)
  • Breaking change (feat!: or fix!:)
  • Docs / chore

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:

Request Before After (real GCS)
OPTIONS from http://evil.example, ACRM DELETE 200 + ACAO: http://evil.example + hardcoded method list 200, no CORS headers
OPTIONS from allowed origin, ACRM DELETE allowed no CORS headers (method not permitted)
OPTIONS from allowed origin, ACRM GET fixed method/header lists Allow-Methods: GET, Allow-Headers: Content-Type, Max-Age: 3600
GET /{bucket}/{object} (XML API) from allowed origin no CORS headers ACAO + Expose-Headers: Content-Type + Vary: Origin
GET from disallowed origin (either URL form) ACAO reflected no CORS headers

Implementation notes:

  • Rule matching now requires both origin and method to be permitted, honouring * in either list; the first matching rule wins.
  • No matching rule ⇒ no CORS headers at all, which is what makes the browser block the request rather than the emulator silently allowing it.
  • Path-style bucket extraction takes the first path segment and still goes through findBucket, so a non-GCS path simply resolves no bucket and no headers are added — mis-extraction is self-correcting.
  • Actual (non-preflight) responses now carry only Allow-Origin, Vary and Expose-Headers; Allow-Methods, Allow-Headers and Max-Age are preflight-only per the CORS spec and are no longer emitted on object reads.

Checklist

  • ./mvnw test passes locally (full suite: 444 tests, 0 failures)
  • New or updated integration test added — 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.
  • Commit messages follow Conventional Commits

…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
@greptile-apps

greptile-apps Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

Greptile Summary

The PR restricts GCS CORS responses to configured origins and methods while safely supporting path-style object routes.

  • Matches preflight and object requests against bucket CORS rules.
  • Emits preflight-only and actual-response headers appropriately.
  • Limits path-style bucket extraction to the XML download controller.
  • Adds integration coverage for allowed, denied, path-style, JSON API, route-collision, and unconfigured-bucket cases.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

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]
Loading

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.
@ankit1324

ankit1324 commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

the route collision — it reproduced exactly as described: with a bucket named batch, an OPTIONS /batch/storage/v1 request picked up that bucket's CORS configuration, because the path-style fallback read the leading segment of every request.

Fixed in 42f61d3 by gating the fallback on the request having actually been routed to the path-style object controller (GcsXmlDownloadController), which JAX-RS has already resolved by the time the filter runs — so route ownership decides it rather than a name guess. The JSON API prefixes keep their existing explicit extraction. Preflights still work on path-style URLs because that controller declares its own @OPTIONS methods.

Also added the missing regression test (bucketNamedLikeAnotherRouteDoesNotGovernThatRoute): a permissive */* CORS config on a bucket named batch must not put CORS headers on /batch/storage/v1. It fails on the previous commit and passes now.

Full suite: 445 tests, 0 failures.

@hectorvent hectorvent added bug Something isn't working storage Cloud Storage (GCS) waiting-contributor Deprecated: use waiting-author labels Aug 1, 2026
@hectorvent hectorvent added waiting-maintainer Contributor has replied; waiting on a maintainer response and removed waiting-contributor Deprecated: use waiting-author labels Aug 12, 2026
@hectorvent

Copy link
Copy Markdown
Member

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 ResourceInfo is populated for both the request and the response filter and the gate works as intended.

No blockers from my side, this looks ready to me.

@hectorvent hectorvent removed the waiting-maintainer Contributor has replied; waiting on a maintainer response label Aug 12, 2026
@hectorvent
hectorvent merged commit 6d16174 into floci-io:main Aug 12, 2026
11 checks passed
@hectorvent

Copy link
Copy Markdown
Member

🎉 This PR is included in version 0.7.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

snazy pushed a commit to snazy/floci-gcp that referenced this pull request Aug 18, 2026
# [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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working released storage Cloud Storage (GCS)

Projects

None yet

2 participants