Skip to content

[Server] Protect cookie login and writes from CSRF - #1994

Merged
yili-db merged 5 commits into
unitycatalog:mainfrom
timothyw553:fix/cookie-csrf-protection
Oct 28, 2026
Merged

yili-db merged 5 commits into
unitycatalog:mainfrom
timothyw553:fix/cookie-csrf-protection

Conversation

@timothyw553

@timothyw553 timothyw553 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description of changes

Cookie login and logout accepted requests without an AJAX header. Require X-Requested-With: XMLHttpRequest for cookie login and cookie-authenticated writes when authorization is enabled. Browsers need CORS approval to send this header across origins. The UI sends it automatically. Cookie-based scripts must add it on writes or use Bearer tokens.

Validation: 39 focused server tests, API generation, Java/UI formatting, and git diff --check passed. UI type checking has the same dependency syntax errors on main.

@yili-db

yili-db commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

/review

Comment thread ui/src/context/client.ts
baseURL: `${UC_API_PREFIX}`,
headers: {
'Content-type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @newfront
Please check the ui-new/ for this requirement.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked ui-new/; it only has a README for now. cookie requests from the new client will need this header too.

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] The approach looks right to me: the check covers every cookie-authenticated write through AuthDecorator plus cookie issuance on /auth/tokens, and Bearer clients are unaffected. A few comments inline.

}

/** Requires a header that browsers cannot send across origins without CORS preflight approval. */
static void validateCsrfHeader(RequestHeaders headers) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] The new tests only send POST. Nothing shows that PUT/PATCH/DELETE without the header are rejected, or that HEAD/OPTIONS with a cookie get through. The preflight case sends no cookie, so it doesn't exercise SAFE_METHODS. Can we add a small test that calls validateCsrfHeader once per method, so widening SAFE_METHODS later makes a test fail?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added direct tests for safe and unsafe methods, including the error code and message for rejected requests.

* @param request The HTTP request, checked for CSRF protection when issuing a cookie.
* @return The token exchange response
*/
@Post("/tokens")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] /auth/tokens with ext=cookie and cookie-authenticated /auth/logout now return 403 without the header. Should api/control.yaml describe the header and the 403, with the checked-in docs regenerated?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the header and 403 response to control.yaml and regenerated the UI types.

HttpRequest request) {
LOGGER.debug("Got token: {}", form);

if (ext.orElse(null) == TokenEndpointExtensionType.COOKIE) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] nit: this repeats the ext.ifPresent(e -> e.equals(COOKIE)) check further down. Can we compute an isCookie boolean once and use it in both places?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, both places now use isCookie.

Comment thread docs/server/auth.md Outdated
Comment on lines +73 to +77
Cookie-authenticated requests other than GET, HEAD, and OPTIONS require
`X-Requested-With: XMLHttpRequest`. Requesting a cookie with `POST /auth/tokens?ext=cookie`
also requires this header. The UI sends it automatically. Browser requests with this header
require CORS preflight approval across origins; the server does not enable cross-origin access.
Clients that use `Authorization: Bearer <token>` do not need this header.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] nit: can this say that a missing header returns 403 PERMISSION_DENIED, and that the check only applies when authorization is enabled?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the 403 PERMISSION_DENIED response and clarified what happens when authorization is disabled.

@timothyw553
timothyw553 requested a review from yili-db October 20, 2026 18:13
@timothyw553

Copy link
Copy Markdown
Collaborator Author

/review

@timothyw553
timothyw553 force-pushed the fix/cookie-csrf-protection branch from 206de90 to fdb5048 Compare October 20, 2026 19:22
@timothyw553

Copy link
Copy Markdown
Collaborator Author

/review

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] Thanks for addressing the earlier comments; the per-method tests, spec update, and isCookie cleanup look good. One question about the new logout check, plus two doc nits.

.findFirst()
.map(
authorizationCookie -> {
if (!request.headers().get(HttpHeaderNames.AUTHORIZATION, "").startsWith("Bearer ")) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] Why do we need this? With authorization disabled, grantToken throws "Authorization is disabled", so that server never issues the cookie, and nothing reads UC_TOKEN there.

It also costs us two things:

  • testCookieWritesRequireAjaxHeader sends its rejected writes only to /auth/logout, and logout now rejects them by itself. So the test still passes if the check in AuthDecorator is removed.
  • The Bearer-precedence rule now lives in two places (a literal "Bearer " here vs BEARER_PREFIX in the decorator), and the throwing check inside Optional.map is easy to miss.

Can we drop it, along with the UnityCatalogServerTest case and the "even when authorization is disabled" wording in control.yaml and auth.md? Then aim the rejection cases in testCookieWritesRequireAjaxHeader at a decorated route such as POST /scim2/Users. If you want to keep it, please share one helper with the decorator; the integration test still needs a non-logout route either way.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the extra logout check, its test, and the wording about authorization being disabled. logout still uses the shared check when authorization is enabled. the rejection cases now use POST /scim2/Users.

Comment thread docs/server/auth.md Outdated
Comment on lines +72 to +83
The authentication cookie uses `Secure`, `HttpOnly`, and `SameSite=Strict`.
Requesting a cookie with `POST /auth/tokens?ext=cookie` always requires
`X-Requested-With: XMLHttpRequest`. Clearing a cookie with `POST /auth/logout` also requires
this header, even when authorization is disabled, unless a Bearer token is provided.
When authorization is enabled, all other cookie-authenticated requests except GET, HEAD, and OPTIONS
require this header. A missing or incorrect header returns HTTP 403 `PERMISSION_DENIED`
before the cookie token is validated, even if that token is invalid or expired.
The UI sends the header automatically. Cookie-based scripts must add it to writes or use Bearer tokens.
Browser requests with this header require CORS preflight approval across origins;
the server does not enable cross-origin access.
Clients that use `Authorization: Bearer <token>` for authentication do not need this header.
Token exchange is unavailable when authorization is disabled.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] nit: this paragraph has roughly doubled. "Before the cookie token is validated, even if that token is invalid or expired" describes how the code is ordered, which can change without anyone updating this doc. Can we cut it back to which requests need the header, that a missing header returns 403 PERMISSION_DENIED, and that Bearer clients are exempt?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the implementation details. kept the header requirements, the 403 response, and the bearer exception.

Comment thread api/control.yaml Outdated
Comment on lines +229 to +230
With authorization enabled, all cookie-authenticated requests other than GET, HEAD, and OPTIONS
require X-Requested-With: XMLHttpRequest.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] nit: this states the rule for all cookie writes, but it only shows up in the logout docs and control.gen.ts. Can the logout entry describe just logout, and the getToken description mention that ext=cookie needs the header?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logout now describes just logout. added the ext=cookie header requirement to getToken and regenerated the UI types.

@timothyw553
timothyw553 requested a review from yili-db October 21, 2026 22:46
Test rejected cookie writes on the users route and shorten the auth docs.

Signed-off-by: Timothy Wang <[email protected]>
@timothyw553
timothyw553 force-pushed the fix/cookie-csrf-protection branch from 5bb67a7 to 4c52b47 Compare October 21, 2026 22:52
@timothyw553 timothyw553 reopened this Oct 23, 2026
@timothyw553

Copy link
Copy Markdown
Collaborator Author

/review

@yili-db

yili-db commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Waiting for @tdas approval on api/control.yaml

@tdas tdas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on yaml

@yili-db
yili-db merged commit 45320ad into unitycatalog:main Oct 28, 2026
33 of 34 checks passed
yili-db pushed a commit that referenced this pull request Oct 28, 2026
**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [ ] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

`main` fails
`AuthServiceTest.testScimCreateUserDatabaseFailureIsAServerError`, and
so does every open PR's "All tests" job, because CI tests each PR merged
with `main`.

#1994 requires `X-Requested-With: XMLHttpRequest` on writes
authenticated with the `UC_TOKEN` cookie. This test, added by #1999
before #1994 merged, sends a SCIM create with that cookie and no header,
so it gets `403 Forbidden` instead of the `500` it checks. It now sends
the header, like the other cookie tests #1994 updated.

<details>
<summary>📐 Design notes & details</summary>

- On the commit just before #1994 (`6ee532c`), which already has #1999's
test, `AuthServiceTest` passes (8/8). On `main` (`45320ad`) it fails
with `expected: 500 Internal Server Error but was: 403 Forbidden`. With
this change it passes on `main` (10/10).
- The test still covers what #1999 added it for: a database failure
inside the SCIM create is a 500, not a client error.

</details>

This pull request and its description were written by Isaac.

Signed-off-by: Shubham Bansal <[email protected]>
Co-authored-by: Isaac <[email protected]>
shba24 added a commit to shba24/unitycatalog that referenced this pull request Nov 12, 2026
UC requires X-Requested-With on the ext=cookie token exchange (CSRF guard,
unitycatalog#1994) and answers 403 without it. The ui-new bridge dropped the header, so
the Google and internal-mode Okta sign-in failed against main. The SPA now
sends it on every call (as the current ui does), the bridge forwards it to UC,
and the bridge's CORS allow-list accepts it for cross-origin deployments.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
shba24 added a commit to shba24/unitycatalog that referenced this pull request Nov 12, 2026
UC requires X-Requested-With on the ext=cookie token exchange (CSRF guard,
unitycatalog#1994) and answers 403 without it. The ui-new bridge dropped the header, so
the Google and internal-mode Okta sign-in failed against main. The SPA now
sends it on every call (as the current ui does), the bridge forwards it to UC,
and the bridge's CORS allow-list accepts it for cross-origin deployments.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
shba24 added a commit to shba24/unitycatalog that referenced this pull request Nov 12, 2026
UC requires X-Requested-With on the ext=cookie token exchange (CSRF guard,
unitycatalog#1994) and answers 403 without it. The ui-new bridge dropped the header, so
the Google and internal-mode Okta sign-in failed against main. The SPA now
sends it on every call (as the current ui does), the bridge forwards it to UC,
and the bridge's CORS allow-list accepts it for cross-origin deployments.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
shba24 added a commit to shba24/unitycatalog that referenced this pull request Nov 12, 2026
UC requires X-Requested-With on the ext=cookie token exchange (CSRF guard,
unitycatalog#1994) and answers 403 without it. The ui-new bridge dropped the header, so
the Google and internal-mode Okta sign-in failed against main. The SPA now
sends it on every call (as the current ui does), the bridge forwards it to UC,
and the bridge's CORS allow-list accepts it for cross-origin deployments.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants