Repository navigation
[Server] Protect cookie login and writes from CSRF - #1994
Conversation
|
/review |
| baseURL: `${UC_API_PREFIX}`, | ||
| headers: { | ||
| 'Content-type': 'application/json', | ||
| 'X-Requested-With': 'XMLHttpRequest', |
There was a problem hiding this comment.
cc @newfront
Please check the ui-new/ for this requirement.
There was a problem hiding this comment.
checked ui-new/; it only has a README for now. cookie requests from the new client will need this header too.
yili-db
left a comment
There was a problem hiding this comment.
[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) { |
There was a problem hiding this comment.
[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?
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
[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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
[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?
There was a problem hiding this comment.
done, both places now use isCookie.
| 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. |
There was a problem hiding this comment.
[AI Assisted] nit: can this say that a missing header returns 403 PERMISSION_DENIED, and that the check only applies when authorization is enabled?
There was a problem hiding this comment.
added the 403 PERMISSION_DENIED response and clarified what happens when authorization is disabled.
|
/review |
206de90 to
fdb5048
Compare
|
/review |
yili-db
left a comment
There was a problem hiding this comment.
[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 ")) { |
There was a problem hiding this comment.
[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:
testCookieWritesRequireAjaxHeadersends its rejected writes only to/auth/logout, and logout now rejects them by itself. So the test still passes if the check inAuthDecoratoris removed.- The Bearer-precedence rule now lives in two places (a literal
"Bearer "here vsBEARER_PREFIXin the decorator), and the throwing check insideOptional.mapis 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.
There was a problem hiding this comment.
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.
| 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. |
There was a problem hiding this comment.
[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?
There was a problem hiding this comment.
removed the implementation details. kept the header requirements, the 403 response, and the bearer exception.
| With authorization enabled, all cookie-authenticated requests other than GET, HEAD, and OPTIONS | ||
| require X-Requested-With: XMLHttpRequest. |
There was a problem hiding this comment.
[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?
There was a problem hiding this comment.
logout now describes just logout. added the ext=cookie header requirement to getToken and regenerated the UI types.
Signed-off-by: Timothy Wang <[email protected]>
Signed-off-by: Timothy Wang <[email protected]>
Signed-off-by: Timothy Wang <[email protected]>
Test rejected cookie writes on the users route and shorten the auth docs. Signed-off-by: Timothy Wang <[email protected]>
5bb67a7 to
4c52b47
Compare
|
/review |
|
Waiting for @tdas approval on |
**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]>
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]>
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]>
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]>
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]>
PR Checklist
docsis updatedDescription of changes
Cookie login and logout accepted requests without an AJAX header. Require
X-Requested-With: XMLHttpRequestfor 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 --checkpassed. UI type checking has the same dependency syntax errors on main.