Skip to content

Commit 842e862

Browse files
authored
fix(sdk): the TypeScript SDK's same-origin check refuses Origin: null (#552)
If an app's redirect URI has an opaque origin, such as `com.example.app:/callback`, the TypeScript SDK let a request with `Origin: null` and no `Sec-Fetch-Site` through its CSRF check on logout and `session()`. That's because `new URL(...).origin` is the string `"null"` for such a URI. The Rust SDK refuses this request. Now the TypeScript check refuses `Origin: null` too: a URI whose origin is `null` matches nothing, as in Rust (`BrowserRequest::is_same_origin` requires a tuple origin). Both SDKs now run the rule from a new `same-origin/` category in `tests/sdk-vectors`, which includes the opaque case. Without the fix, the TypeScript SDK fails `opaque-origin-null.json`. The SDK architecture page and the doc comment on `#sameOrigin` now describe the case. Closes #548 > [!NOTE] > Written by Claude Opus 5.5 in Claude Code (via T3 Code); reviewed by GPT 6.1 Sol 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> **TypeScript SDK (`packages/auth/src/login.ts`)** - `Login.#sameOrigin` now refuses an `Origin` comparison when the redirect URI origin is `"null"`. - Before this change, a request with `Origin: null` and no `Sec-Fetch-Site` passed for opaque redirect URIs such as `com.example.app:/callback`. - The code comment now states that `null` never matches, including for opaque redirect-URI origins. **TypeScript tests (`packages/auth/test/vectors.test.ts`)** - Adds a `same-origin vectors` test group. - For each case, the test builds a POST request with the case's `sec-fetch-site` and `origin` headers and checks whether `login.session` returns a session or a `Response`. **Shared vectors (`tests/sdk-vectors/same-origin/`, `tests/sdk-vectors/README.md`)** - Adds nine `same-origin` cases: `cross-site`, `fetch-site-only`, `no-headers`, `no-referrer-form`, `opaque-origin-null`, `origin-null`, `origin-only`, `same-site`, and `sibling-origin`. - `opaque-origin-null.json` covers the opaque redirect URI case and expects `ok: false`. - The README category table documents the new category and its inputs. **Rust tests (`crates/auth-sdk/src/tests.rs`)** - Adds a `same_origin_vectors` test. - The test loads the `same-origin` cases and compares `is_same_origin` with each expected result. **Rust vector generator (`crates/crypto/tests/sdk_vectors.rs`)** - Adds a `same_origin` generator that records expected results for the app origin. - `Vectors::finish` now includes the `same-origin` directory. **Documentation (`docs/dev/architecture/sdk.md`)** - The Sessions section states that `Origin` must equal the redirect URI origin. - It states that `null` never matches, including for opaque redirect-URI origins. **Public API** - No exported or public declarations changed. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent e24ecd3 commit 842e862

15 files changed

Lines changed: 249 additions & 6 deletions

File tree

‎crates/auth-sdk/src/tests.rs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
1111
use chrono::TimeDelta;
1212
use serde_json::Value;
1313
use sourceorigin_common::protocol::{
14-
challenge::BearerChallenge, claims::AccessTokenClaims, jwt::TokenType, metadata::ServerMetadata,
14+
challenge::BearerChallenge, claims::AccessTokenClaims, jwt::TokenType,
15+
metadata::ServerMetadata, same_origin::BrowserRequest,
1516
};
1617
use std::{
1718
fs,
@@ -311,6 +312,22 @@ fn callback_vectors() {
311312
}
312313
}
313314

315+
/// The CSRF check of logout and of reading a session, against the client's redirect
316+
/// URI's origin, as [`crate::axum`]'s login routes ask it.
317+
#[test]
318+
fn same_origin_vectors() {
319+
for case in Case::all("same-origin") {
320+
let site = Url::parse(case.text("redirect_uri"))
321+
.expect("redirect URI")
322+
.origin();
323+
let request = BrowserRequest {
324+
fetch_site: case.input("sec_fetch_site").as_str(),
325+
origin: case.input("origin").as_str(),
326+
};
327+
case.assert(Ok::<_, SdkError>(request.is_same_origin(&site)));
328+
}
329+
}
330+
314331
/// A local key endpoint whose responses tests switch between, counting requests.
315332
pub(crate) struct KeyServer {
316333
/// The JWKS URL.

‎crates/crypto/tests/sdk_vectors.rs‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ impl Vectors {
168168
"tokens",
169169
"challenges",
170170
"callbacks",
171+
"same-origin",
171172
"sessions",
172173
"cookie-keys",
173174
]
@@ -1302,6 +1303,102 @@ impl Vectors {
13021303
}
13031304
}
13041305

1306+
/// Same-origin cases: whether a browser request with these `Sec-Fetch-Site` and
1307+
/// `Origin` headers comes from the pages of an app whose redirect URI is
1308+
/// `redirect_uri`, the CSRF check of logout and of reading a session.
1309+
fn same_origin(&mut self) {
1310+
let app = "https://client.example.com/callback";
1311+
let opaque = "com.example.notes:/callback";
1312+
for (name, description, redirect_uri, fetch_site, origin, own) in [
1313+
(
1314+
"no-referrer-form",
1315+
"a form on a no-referrer page sends Origin: null, and Sec-Fetch-Site decides",
1316+
app,
1317+
Some("same-origin"),
1318+
Some("null"),
1319+
true,
1320+
),
1321+
(
1322+
"fetch-site-only",
1323+
"Sec-Fetch-Site: same-origin is enough without Origin",
1324+
app,
1325+
Some("same-origin"),
1326+
None,
1327+
true,
1328+
),
1329+
(
1330+
"origin-only",
1331+
"without Sec-Fetch-Site, an Origin equal to the app's is accepted",
1332+
app,
1333+
None,
1334+
Some("https://client.example.com"),
1335+
true,
1336+
),
1337+
(
1338+
"same-site",
1339+
"Sec-Fetch-Site: same-site is refused, whatever Origin says",
1340+
app,
1341+
Some("same-site"),
1342+
Some("https://client.example.com"),
1343+
false,
1344+
),
1345+
(
1346+
"cross-site",
1347+
"Sec-Fetch-Site: cross-site is refused, whatever Origin says",
1348+
app,
1349+
Some("cross-site"),
1350+
Some("https://client.example.com"),
1351+
false,
1352+
),
1353+
(
1354+
"origin-null",
1355+
"without Sec-Fetch-Site, Origin: null is refused",
1356+
app,
1357+
None,
1358+
Some("null"),
1359+
false,
1360+
),
1361+
(
1362+
"no-headers",
1363+
"a request with neither header is refused",
1364+
app,
1365+
None,
1366+
None,
1367+
false,
1368+
),
1369+
(
1370+
"sibling-origin",
1371+
"a sibling under the same domain is another origin",
1372+
app,
1373+
None,
1374+
Some("https://evil.client.example.com"),
1375+
false,
1376+
),
1377+
(
1378+
"opaque-origin-null",
1379+
"an opaque redirect URI's origin serializes as null but matches nothing",
1380+
opaque,
1381+
None,
1382+
Some("null"),
1383+
false,
1384+
),
1385+
] {
1386+
self.case(
1387+
&format!("same-origin/{name}.json"),
1388+
&Case {
1389+
description,
1390+
now: None,
1391+
input: json!({
1392+
"redirect_uri": redirect_uri,
1393+
"sec_fetch_site": fetch_site,
1394+
"origin": origin,
1395+
}),
1396+
expected: Expected::ok(&own),
1397+
},
1398+
);
1399+
}
1400+
}
1401+
13051402
/// Session cookie cases: the session a `Cookie` header holds for the fixture
13061403
/// client, sealed with [`COOKIE_KEY`]. They are sealed here with OpenSSL, apart
13071404
/// from any SDK's own code, and a fixed nonce, so they are reproducible.
@@ -1589,6 +1686,7 @@ fn sdk_vectors_are_current() {
15891686
vectors.tokens();
15901687
vectors.challenges();
15911688
vectors.callbacks();
1689+
vectors.same_origin();
15921690
vectors.sessions();
15931691
vectors.cookie_keys();
15941692
vectors.finish();

‎docs/dev/architecture/sdk.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ sibling site's under the same domain: any `*.sourceorigin.dev` page could post t
392392
its signed-in user. So logout, and every request that is not `GET`, `HEAD`, `OPTIONS`
393393
or `TRACE` to a handler reading the session, are accepted only from the app's own
394394
pages: `Sec-Fetch-Site: same-origin` when the browser sends it, and otherwise an
395-
`Origin` equal to the app's, which `null` never is. Anything else gets 403 before
395+
`Origin` equal to the app's redirect URI's. `null` never matches, not even when the
396+
redirect URI has an opaque origin, such as `com.example.app:/callback`, which
397+
serializes as `null` too. Anything else gets 403 before
396398
the session is read, with or without a cookie, so handlers need no CSRF token. A form
397399
on a page with `Referrer-Policy: no-referrer` sends `Origin: null` with
398400
`Sec-Fetch-Site: same-origin`, so it is accepted. Safe methods must not change

‎packages/auth/src/login.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,13 +747,15 @@ export class Login {
747747
/**
748748
* Whether a browser request comes from this app's own pages, whose origin is the
749749
* redirect URI's: `Sec-Fetch-Site: same-origin` when the browser sends it, and
750-
* otherwise an `Origin` equal to the app's, which `null` never is. Logout and
751-
* {@link Login.session} both ask it.
750+
* otherwise an `Origin` equal to the app's. `null` never matches, not even the
751+
* opaque origin of a redirect URI such as `com.example.app:/callback`, which
752+
* serializes as `null` too. Logout and {@link Login.session} both ask it.
752753
*/
753754
#sameOrigin(request: Request): boolean {
754755
const site = request.headers.get('sec-fetch-site');
755756
if (site !== null) return site === 'same-origin';
756-
return request.headers.get('origin') === new URL(this.client.config.redirectUri).origin;
757+
const origin = new URL(this.client.config.redirectUri).origin;
758+
return origin !== 'null' && request.headers.get('origin') === origin;
757759
}
758760

759761
/**

‎packages/auth/test/vectors.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
REDIRECT_URI,
2424
TOKEN_URL,
2525
cases,
26-
outcome
26+
outcome,
27+
vector
2728
} from './vectors.ts';
2829

2930
describe('discovery vectors', () => {
@@ -166,6 +167,29 @@ describe('callback vectors', () => {
166167
}
167168
});
168169

170+
describe('same-origin vectors', () => {
171+
const cookieKey = CookieKey.parse(String(vector('sessions', 'valid').input.cookie_key));
172+
for (const vector of cases('same-origin')) {
173+
test(vector.name, async () => {
174+
const issuer = await Issuer.discover(ISSUER, { fetch: FakeFetch.issuer().fetch });
175+
const client = issuer.client({
176+
clientId: 'notes-web',
177+
clientSecret: 'secret',
178+
redirectUri: String(vector.input.redirect_uri)
179+
});
180+
const login = new Login(client, { cookieKey });
181+
const { sec_fetch_site: site, origin } = vector.input;
182+
const headers = new Headers();
183+
if (typeof site === 'string') headers.set('sec-fetch-site', site);
184+
if (typeof origin === 'string') headers.set('origin', origin);
185+
// Without a cookie, the app's own pages read no session; any other page is refused.
186+
const request = new Request('https://client.example.com/notes', { method: 'POST', headers });
187+
const session = await login.session(request);
188+
assert.deepEqual({ ok: !(session instanceof Response) }, vector.expected);
189+
});
190+
}
191+
});
192+
169193
/** Why `CookieKey.parse` refuses `text`, in the vectors' shape, read from its message. */
170194
function refusal(text: string): unknown {
171195
try {

‎tests/sdk-vectors/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Each file is one case:
4646
| `tokens/` | `issuer`, `jwks`, `kind` (`access`, `id` or `logout`), `audience` (the resource, or the client for ID and logout tokens), `token`; ID tokens add `nonce` and `max_age` (`null` when not sent) | the token's claims |
4747
| `challenges/` | `challenge` (format cases) or `header` (parse cases) | `status` and `header`, or `challenge` |
4848
| `callbacks/` | `issuer`, `authorization_response_iss_parameter_supported`, the expected `state`, and the callback's query `parameters` | `code` |
49+
| `same-origin/` | `redirect_uri`, the client's, and the request's `sec_fetch_site` and `origin` headers (`null` when not sent) | whether the request comes from the app's own pages |
4950
| `sessions/` | `cookie_key` (standard base64), `client_id`, and the `cookie` header the browser sends | the session the cookie holds, in the cookie's JSON, or `null` for none |
5051
| `cookie-keys/` | `cookie_key`, the text the app configures | the key's 32 bytes in hex |
5152

‎tests/sdk-vectors/same-origin/cross-site.json‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/sdk-vectors/same-origin/fetch-site-only.json‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/sdk-vectors/same-origin/no-headers.json‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/sdk-vectors/same-origin/no-referrer-form.json‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)