Skip to content

Commit c004672

Browse files
committed
Let the consent form post without an Origin header
Staging found what the test could not: pressing "İzin ver ve devam et" answered origin_forbidden. Browsers do not send an Origin header on a same-origin top-level form submission, and requireAllowedOrigin rejects a missing header. My end-to-end test set the header by hand, so it verified that the check passes when the header is present and never measured what the browser actually sends. Green for the wrong reason, in the exact shape this repo has been bitten by before. The consent endpoint now uses its own check: if Origin is present it must match, and if Sec-Fetch-Site is present it must not be cross-site. A missing Origin is accepted, which is safe because the dangerous case always carries one — a form submitted from another site is cross-origin, and browsers have sent Origin for that for years. The real gate is unchanged and is not this: it is the CSRF value bound to the session's digest. The test helper now posts the way a browser does, with no Origin, and the cases that want the header pass it explicitly. Reverting to requireAllowedOrigin fails six tests including the new one, which is the proof the old shape was untested rather than merely unlucky.
1 parent 11f16e2 commit c004672

2 files changed

Lines changed: 74 additions & 8 deletions

File tree

‎scripts/orbit-site-signin-e2e-tests.ts‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,40 @@ function authorizeQuery(overrides: Record<string, string> = {}): Record<string,
143143
};
144144
}
145145

146+
/* Onay gönderimi, GERÇEK tarayıcı gibi: `Origin` başlığı YOK.
147+
*
148+
* Bu, staging'de öğrenilmiş bir ders ve testin biçimi o dersin kaydı.
149+
* Tarayıcı aynı kökene giden üst düzey form gönderiminde `Origin` göndermiyor;
150+
* eski hâlinde bu yardımcı başlığı elle koyuyordu ve test, gerçekte
151+
* çalışmayan bir yolu yeşil gösteriyordu. Başlığı isteyen durumlar artık
152+
* `origin` seçeneğiyle AÇIKÇA belirtiliyor. */
146153
async function consent(
147154
ticket: string,
148155
decision: string,
149-
options: { cookie?: string; csrf?: string; now?: number } = {},
156+
options: {
157+
cookie?: string;
158+
csrf?: string;
159+
now?: number;
160+
origin?: string;
161+
secFetchSite?: string;
162+
} = {},
150163
): Promise<Response> {
151164
const body = new URLSearchParams({
152165
ticket,
153166
csrf: options.csrf ?? csrfToken,
154167
decision,
155168
});
169+
const headers: Record<string, string> = {
170+
'content-type': 'application/x-www-form-urlencoded',
171+
cookie: options.cookie ?? sessionCookie,
172+
'x-test-now': String(options.now ?? NOW),
173+
};
174+
if (options.origin !== undefined) headers.origin = options.origin;
175+
if (options.secFetchSite !== undefined) headers['sec-fetch-site'] = options.secFetchSite;
156176
return await fetch(`${baseUrl}/v1/oauth/consent`, {
157177
method: 'POST',
158178
redirect: 'manual',
159-
headers: {
160-
'content-type': 'application/x-www-form-urlencoded',
161-
origin: ORIGIN,
162-
cookie: options.cookie ?? sessionCookie,
163-
'x-test-now': String(options.now ?? NOW),
164-
},
179+
headers,
165180
body: body.toString(),
166181
});
167182
}
@@ -469,6 +484,29 @@ describe('Signing in to another Equinox site with Orbit', { concurrency: false }
469484
assert.equal(forged.status, 403);
470485
});
471486

487+
test('the consent form works without an Origin header but not from another site', async () => {
488+
const screen = await authorize(authorizeQuery({
489+
scope: 'openid profile email orbit.graph.read',
490+
state: 'origin-state',
491+
}));
492+
assert.equal(screen.status, 200);
493+
const ticket = ticketFromConsentPage(await screen.text());
494+
495+
/* Başka bir siteden gönderilen form: `Origin` orada VAR ve eşleşmiyor. */
496+
const foreign = await consent(ticket, 'allow', { origin: 'https://saldirgan.example' });
497+
assert.equal(foreign.status, 403);
498+
499+
/* Modern tarayıcının çapraz köken sinyali. */
500+
const crossSite = await consent(ticket, 'allow', { secFetchSite: 'cross-site' });
501+
assert.equal(crossSite.status, 403);
502+
503+
/* Ve gerçek hâl: aynı kökenden gelen form, `Origin` başlığı olmadan. Bu
504+
* satır staging'de kırılan yolun kendisi. */
505+
const real = await consent(ticket, 'allow', { secFetchSite: 'same-origin' });
506+
assert.equal(real.status, 302, 'the browser sends no Origin on a same-origin form post');
507+
assert.ok(locationOf(real).searchParams.get('code'));
508+
});
509+
472510
test('a code cannot be exchanged twice, and the replay burns the tokens', async () => {
473511
const granted = await authorize(authorizeQuery({ state: 'replay-state' }));
474512
const code = locationOf(granted).searchParams.get('code') ?? '';

‎src/server/http/api.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4006,6 +4006,34 @@ function siteIssuer(env: OrbitBindings): string {
40064006
return env.ORBIT_ALLOWED_ORIGIN;
40074007
}
40084008

4009+
/* Tarayıcı formundan gelen POST'un köken kontrolü.
4010+
*
4011+
* `requireAllowedOrigin` burada KULLANILAMIYOR ve bunu staging'de öğrendim:
4012+
* tarayıcı, aynı kökene giden üst düzey bir form gönderiminde `Origin`
4013+
* başlığını hiç göndermiyor. Onay ekranındaki "İzin ver" düğmesi bu yüzden
4014+
* `origin_forbidden` alıyordu. Uçtan uca testim bunu yakalamamıştı, çünkü
4015+
* başlığı elle koyuyordu — yani kontrolün başlık VARKEN çalıştığını
4016+
* doğrulamış, gerçek tarayıcının onu göndermediğini hiç ölçmemiştim.
4017+
*
4018+
* Kural: başlık varsa eşleşmek zorunda. Yoksa geçiyor, çünkü tehlikeli olan
4019+
* durum her zaman başlığı taşıyor — başka bir siteden gelen bir form
4020+
* gönderimi çapraz kökenlidir ve tarayıcılar o durumda `Origin`'i yıllardır
4021+
* gönderiyor. `Sec-Fetch-Site` varsa o da ikinci bir kapı.
4022+
*
4023+
* Asıl koruma yine de bu değil: oturumun digest'ine bağlı CSRF değeri. Köken
4024+
* kontrolü onun üstüne binen ikinci katman; tek katman olsaydı bu boşluk bir
4025+
* açık olurdu, öyle değil. */
4026+
function requireBrowserFormOrigin(request: Request, env: OrbitBindings): void {
4027+
const origin = request.headers.get('origin');
4028+
if (origin !== null && origin !== env.ORBIT_ALLOWED_ORIGIN) {
4029+
throw new ApiError(403, 'origin_forbidden', 'Request origin is not allowed.');
4030+
}
4031+
const site = request.headers.get('sec-fetch-site');
4032+
if (site !== null && site !== 'same-origin' && site !== 'none') {
4033+
throw new ApiError(403, 'origin_forbidden', 'Request origin is not allowed.');
4034+
}
4035+
}
4036+
40094037
async function readFormBody(request: Request): Promise<URLSearchParams> {
40104038
const contentType = request.headers.get('content-type') ?? '';
40114039
if (!contentType.includes('application/x-www-form-urlencoded')) {
@@ -4441,7 +4469,7 @@ async function handleSiteConsent(
44414469
now: number,
44424470
requestId: string,
44434471
): Promise<Response> {
4444-
requireAllowedOrigin(request, env);
4472+
requireBrowserFormOrigin(request, env);
44454473
const body = await readFormBody(request);
44464474
const auth = await authenticateHuman(request, env, repository, now, false);
44474475

0 commit comments

Comments
 (0)