Skip to content

Commit 3a6ec8d

Browse files
committed
Write the consent screen and the ticket that binds it
Third step of Plan 008: the screen a person actually sees, and the signature that makes it mean something. The ticket exists because a consent form is a form. Carrying the scopes, the redirect URI and the PKCE challenge as hidden inputs would let whoever submits it change them — the screen could read "your email" while the recorded grant also covered posts. The ticket is minted server-side in /authorize and re-verified in /consent, so what was shown and what is stored are the same thing. Same pattern as the MCP authorization ticket. The screen is an inventory, not a pitch. It lists what the site will see, and then lists what it will not: the following feed, messages, drafts and settings. A consent screen's most useful sentence is the one about what is withheld. The email row says plainly that the address is copied into the site's own database and that revoking the grant later does not delete that copy — the cost of that decision belongs on the screen where the decision is made, not in a document nobody opens. Self-contained CSS for the same reason as the OAuth error page: needing a second request to be readable means there is a moment where the choice cannot be read, and the person approves without seeing what they give. No script at all, and a CSP that keeps it that way plus frame-ancestors none, so the screen cannot be wrapped in an iframe and presented as something else. The client label is operator-entered and therefore trusted today; it is escaped anyway, with a test that tries to inject markup through it. The "trusted input" assumption goes quietly wrong the day some other path starts writing that column. Endpoints are not wired yet — /authorize and /consent land next, with the end-to-end test through the real worker. 22 tests pass.
1 parent d415e71 commit 3a6ec8d

3 files changed

Lines changed: 436 additions & 0 deletions

File tree

‎scripts/orbit-site-authorization-tests.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import {
2424
siteJwks,
2525
} from '../src/server/identity/site-id-token';
2626
import { SITE_AUTHORIZATION_SCOPES } from '../src/server/identity/site-authorization-scopes';
27+
import {
28+
createSiteAuthorizationRequestTicket,
29+
verifySiteAuthorizationRequestTicket,
30+
} from '../src/server/identity/site-authorization-request';
31+
import { siteConsentPage } from '../src/server/http/site-consent-page';
2732

2833
const ROOT = process.cwd();
2934
const WRANGLER = path.join(ROOT, 'node_modules', 'wrangler', 'bin', 'wrangler.js');
@@ -877,6 +882,108 @@ describe('Orbit as a sign-in door for other sites', { concurrency: false }, () =
877882
assert.equal(document.scopes_supported.some((scope) => scope.endsWith('.write')), false);
878883
});
879884

885+
test('the consent ticket binds what the screen showed', async () => {
886+
const pepper = 'test-oauth-pepper-at-least-32-bytes-long';
887+
const issuedAt = 1_760_003_000_000;
888+
const request = {
889+
clientRowId: CLIENT.id,
890+
clientId: CLIENT.clientId,
891+
redirectUri: CLIENT.redirectUris[0],
892+
scopes: ['openid', 'email'] as const,
893+
state: 'supabase-state-value',
894+
codeChallenge: 'e'.repeat(43),
895+
nonce: 'supabase-nonce',
896+
issuedAt,
897+
expiresAt: issuedAt + 10 * 60 * 1000,
898+
};
899+
900+
const ticket = await createSiteAuthorizationRequestTicket({ ...request, scopes: [...request.scopes] }, pepper);
901+
const verified = await verifySiteAuthorizationRequestTicket(ticket, pepper, issuedAt + 1_000);
902+
assert.deepEqual(verified?.scopes, ['openid', 'email']);
903+
assert.equal(verified?.redirectUri, CLIENT.redirectUris[0]);
904+
assert.equal(verified?.state, 'supabase-state-value');
905+
906+
/* Asıl mesele bu: gövdedeki kapsamı büyütmek. Ekranda "e-postan" yazarken
907+
* kaydedilen iznin gönderilerini de kapsaması, imza olmadan mümkün olurdu. */
908+
const [prefix, payload, signature] = ticket.split('.');
909+
const widened = Buffer.from(JSON.stringify({
910+
...request,
911+
scopes: ['openid', 'email', 'orbit.posts.read'],
912+
})).toString('base64url');
913+
assert.equal(
914+
await verifySiteAuthorizationRequestTicket(
915+
`${prefix}.${widened}.${signature}`,
916+
pepper,
917+
issuedAt + 1_000,
918+
),
919+
null,
920+
'a widened scope list must not survive the signature',
921+
);
922+
923+
assert.equal(
924+
await verifySiteAuthorizationRequestTicket(ticket, 'another-pepper-at-least-32-bytes-long', issuedAt),
925+
null,
926+
);
927+
assert.equal(
928+
await verifySiteAuthorizationRequestTicket(ticket, pepper, issuedAt + 11 * 60 * 1000),
929+
null,
930+
'an expired ticket must not be accepted',
931+
);
932+
assert.equal(await verifySiteAuthorizationRequestTicket(`${prefix}.${payload}`, pepper, issuedAt), null);
933+
});
934+
935+
test('the consent screen names what is shared and what is not', async () => {
936+
const response = siteConsentPage({
937+
clientLabel: 'Anime sitesi',
938+
clientSiteUrl: 'https://anime.sametbasbug.dev',
939+
scopes: ['openid', 'profile', 'email'],
940+
accountHandle: 'samet',
941+
accountDisplayName: 'Samet Başbuğ',
942+
ticket: 'orb_site_req_v1.payload.signature',
943+
csrfToken: 'csrf-value',
944+
cancelUrl: 'https://anime.sametbasbug.dev/auth/v1/callback?error=access_denied',
945+
});
946+
const html = await response.text();
947+
948+
assert.equal(response.headers.get('content-type'), 'text/html; charset=utf-8');
949+
assert.equal(response.headers.get('cache-control'), 'no-store');
950+
/* Sayfada script yok ve CSP bunu kalıcı kılıyor; iframe'e alınamıyor. */
951+
assert.match(response.headers.get('content-security-policy') ?? '', /frame-ancestors 'none'/u);
952+
assert.equal(/<script/iu.test(html), false);
953+
954+
assert.match(html, /E-posta adresin/u);
955+
/* E-postanın geri alınamazlığı ekranda yazılı: kararın bedeli onay
956+
* anında görünmeli, sonradan değil. */
957+
assert.match(html, /o kopya sitede kalır/u);
958+
/* Verilmeyenler listesi — takip akışı burada, çünkü kapsam listesinde
959+
* bilerek yok. */
960+
assert.match(html, /takip akışın/u);
961+
assert.match(html, /Mesajların/u);
962+
/* İstenmeyen kapsamın metni sayfaya hiç girmiyor. */
963+
assert.equal(html.includes('Herkese açık gönderilerin'), false);
964+
assert.match(html, /@samet/u);
965+
});
966+
967+
test('a hostile client label cannot inject markup into the consent screen', async () => {
968+
const response = siteConsentPage({
969+
clientLabel: '<img src=x onerror="alert(1)">Anime',
970+
clientSiteUrl: 'https://anime.example/"><script>alert(2)</script>',
971+
scopes: ['openid'],
972+
accountHandle: 'samet',
973+
accountDisplayName: 'Samet',
974+
ticket: 'orb_site_req_v1.a.b',
975+
csrfToken: 'csrf',
976+
cancelUrl: 'https://anime.example/cb',
977+
});
978+
const html = await response.text();
979+
/* İstemci adı operatör eliyle giriliyor, yani bugün güvenilir. Kaçış yine
980+
* var: "bize güvenilir veri" varsayımı, o veriyi başka bir yerden alan bir
981+
* yol yazıldığı gün sessizce yanlış olur. */
982+
assert.equal(html.includes('<img src=x'), false);
983+
assert.equal(html.includes('<script>alert(2)'), false);
984+
assert.match(html, /&lt;img src=x/u);
985+
});
986+
880987
test('expired codes are removed by retention', async () => {
881988
const now = 1_760_001_200_000;
882989
const accountId = 'door-twelve';
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/* "Orbit ile devam et" onay ekranı.
2+
*
3+
* Bu sayfa tarayıcıda görünen ilk ve tek Orbit yüzeyi olabilir: Anime
4+
* sitesinden gelen biri Orbit'i belki hiç görmemiştir. O yüzden metin
5+
* pazarlama değil, envanter — ne verildiği tek tek yazıyor ve verilmeyenler
6+
* de yazıyor.
7+
*
8+
* Neden kendi içinde tam: hata sayfasındaki gerekçenin aynısı. Site CSS'i bir
9+
* varlık isteği demek; onay ekranının ikinci bir isteğe bağlı olması, kararın
10+
* okunamadığı bir an doğurur ve kullanıcı ne verdiğini görmeden onaylar.
11+
*
12+
* Dinamik değerler (istemci adı, adres) operatör eliyle giriliyor ama yine
13+
* kaçırılıyor: "bize güvenilir veri" varsayımı, bir gün o veriyi başka bir
14+
* yerden alan bir yol yazıldığında sessizce yanlış olur. */
15+
16+
import type { SiteAuthorizationScope } from '../identity/site-authorization-scopes';
17+
18+
interface ScopeText {
19+
title: string;
20+
detail: string;
21+
}
22+
23+
/* Kapsam metinleri. "Kimliğin" gibi soyut sözcük yok: kullanıcı ne verdiğini
24+
* somut görmeli. */
25+
const SCOPE_TEXTS: Record<SiteAuthorizationScope, ScopeText> = {
26+
openid: {
27+
title: 'Bu sitede kim olduğun',
28+
detail: 'Yalnız bu siteye özel bir kimlik numarası. Orbit hesabının kendi numarası verilmiyor ve başka siteler aynı numarayı görmüyor.',
29+
},
30+
profile: {
31+
title: 'Görünen adın, kullanıcı adın ve profil resmin',
32+
detail: 'Orbit profilinde herkese açık olan bilgiler.',
33+
},
34+
email: {
35+
title: 'E-posta adresin',
36+
detail: 'Site bu adresi kendi veritabanına kaydeder. Buradan verdiğin izni sonra geri alsan da o kopya sitede kalır — adresini geri çağırmanın bir yolu yok.',
37+
},
38+
'orbit.graph.read': {
39+
title: 'Kimi takip ettiğin',
40+
detail: 'Takip listen ve takipçi sayıların. Bunlar Orbit’te zaten herkese açık.',
41+
},
42+
'orbit.posts.read': {
43+
title: 'Herkese açık gönderilerin',
44+
detail: 'Orbit’te giriş yapmamış bir ziyaretçinin de görebildiği kayıtlar.',
45+
},
46+
};
47+
48+
/* Hiçbir kapsamın açmadığı şeyler. Ekranda yazıyor, çünkü bir onay ekranının
49+
* en çok işe yarayan cümlesi neyin verilmediğini söyleyen cümledir. */
50+
const WITHHELD = [
51+
'Kimleri okuduğun — yani takip akışın',
52+
'Mesajların',
53+
'Taslakların ve yayımlamadığın kayıtların',
54+
'Hesap ayarların',
55+
];
56+
57+
function escapeHtml(value: string): string {
58+
return value
59+
.replaceAll('&', '&amp;')
60+
.replaceAll('<', '&lt;')
61+
.replaceAll('>', '&gt;')
62+
.replaceAll('"', '&quot;')
63+
.replaceAll("'", '&#39;');
64+
}
65+
66+
const STYLE = `
67+
:root { color-scheme: light dark; --ink: #16181d; --muted: #5b6270; --paper: #fbfbfc;
68+
--card: #ffffff; --line: #e3e5ea; --accent: #2f4bd8; --accent-ink: #ffffff; }
69+
@media (prefers-color-scheme: dark) { :root { --ink: #eceef2; --muted: #9aa1af;
70+
--paper: #14161a; --card: #191c21; --line: #2a2e36; --accent: #7d93ff; --accent-ink: #10131a; } }
71+
* { box-sizing: border-box; }
72+
body { margin: 0; min-height: 100vh; display: grid; place-items: center; padding: 1.5rem;
73+
background: var(--paper); color: var(--ink);
74+
font: 1rem/1.6 system-ui, -apple-system, "Segoe UI", sans-serif; }
75+
main { width: 100%; max-width: 33rem; background: var(--card);
76+
border: 1px solid var(--line); border-radius: 0.9rem; padding: 1.75rem; }
77+
h1 { margin: 0 0 0.35rem; font-size: 1.3rem; line-height: 1.35; }
78+
.site { margin: 0 0 1.5rem; color: var(--muted); font-size: 0.9rem; word-break: break-all; }
79+
h2 { margin: 1.5rem 0 0.6rem; font-size: 0.8rem; text-transform: uppercase;
80+
letter-spacing: 0.06em; color: var(--muted); }
81+
ul { margin: 0; padding: 0; list-style: none; }
82+
li { padding: 0.7rem 0; border-top: 1px solid var(--line); }
83+
li:first-child { border-top: none; }
84+
li strong { display: block; font-weight: 600; }
85+
li span { color: var(--muted); font-size: 0.88rem; }
86+
.withheld li { padding: 0.3rem 0; border: none; color: var(--muted); font-size: 0.9rem; }
87+
.withheld li::before { content: "—"; margin-right: 0.5rem; }
88+
.who { margin: 1.5rem 0 0; padding: 0.9rem 1rem; border: 1px solid var(--line);
89+
border-radius: 0.6rem; font-size: 0.9rem; color: var(--muted); }
90+
.who strong { color: var(--ink); }
91+
.actions { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-top: 1.5rem; }
92+
button { font: inherit; font-weight: 600; padding: 0.7rem 1.25rem; border-radius: 0.55rem;
93+
border: 1px solid var(--line); cursor: pointer; background: transparent; color: inherit; }
94+
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
95+
.fine { margin: 1.25rem 0 0; font-size: 0.82rem; color: var(--muted); }
96+
`;
97+
98+
export interface SiteConsentPageInput {
99+
clientLabel: string;
100+
clientSiteUrl: string;
101+
scopes: readonly SiteAuthorizationScope[];
102+
accountHandle: string;
103+
accountDisplayName: string;
104+
ticket: string;
105+
csrfToken: string;
106+
/* Kullanıcı vazgeçerse siteye bu adresle dönüyor. */
107+
cancelUrl: string;
108+
}
109+
110+
export function siteConsentPage(input: SiteConsentPageInput): Response {
111+
const label = escapeHtml(input.clientLabel);
112+
const items = input.scopes.map((scope) => {
113+
const text = SCOPE_TEXTS[scope];
114+
return `<li><strong>${escapeHtml(text.title)}</strong><span>${escapeHtml(text.detail)}</span></li>`;
115+
}).join('\n');
116+
const withheld = WITHHELD
117+
.map((line) => `<li>${escapeHtml(line)}</li>`)
118+
.join('\n');
119+
120+
const html = `<!doctype html>
121+
<html lang="tr">
122+
<head>
123+
<meta charset="utf-8" />
124+
<meta name="viewport" content="width=device-width, initial-scale=1" />
125+
<meta name="robots" content="noindex" />
126+
<title>${label} için izin · Equinox Orbit</title>
127+
<style>${STYLE}</style>
128+
</head>
129+
<body>
130+
<main>
131+
<h1>${label} Orbit hesabınla devam etmek istiyor</h1>
132+
<p class="site">${escapeHtml(input.clientSiteUrl)}</p>
133+
134+
<h2>Bu site şunları görecek</h2>
135+
<ul>
136+
${items}
137+
</ul>
138+
139+
<h2>Şunları görmeyecek</h2>
140+
<ul class="withheld">
141+
${withheld}
142+
</ul>
143+
144+
<p class="who">Orbit hesabın: <strong>@${escapeHtml(input.accountHandle)}</strong>
145+
(${escapeHtml(input.accountDisplayName)})</p>
146+
147+
<form method="post" action="/v1/oauth/consent">
148+
<input type="hidden" name="ticket" value="${escapeHtml(input.ticket)}" />
149+
<input type="hidden" name="csrf" value="${escapeHtml(input.csrfToken)}" />
150+
<div class="actions">
151+
<button type="submit" name="decision" value="allow" class="primary">İzin ver ve devam et</button>
152+
<button type="submit" name="decision" value="deny">Vazgeç</button>
153+
</div>
154+
</form>
155+
156+
<p class="fine">Bu izni daha sonra Orbit panelindeki <strong>bağlı siteler</strong>
157+
bölümünden geri alabilirsin. İzni geri almak siteye verilmiş anahtarları anında
158+
düşürür; sitenin kendi veritabanına kopyaladığı bilgileri silmez.</p>
159+
</main>
160+
</body>
161+
</html>`;
162+
163+
return new Response(html, {
164+
status: 200,
165+
headers: {
166+
'content-type': 'text/html; charset=utf-8',
167+
/* Onay ekranı önbelleğe girmiyor: içinde tek kullanımlık bir bilet ve
168+
* CSRF değeri var, ve geri tuşuyla dönülüp yeniden gönderilmesi
169+
* beklenmiyor. */
170+
'cache-control': 'no-store',
171+
'referrer-policy': 'no-referrer',
172+
'x-content-type-options': 'nosniff',
173+
/* Sayfada hiç script yok ve olmasına gerek yok; CSP bunu kalıcı kılıyor.
174+
* `frame-ancestors 'none'`: onay ekranı bir iframe'in içine alınıp
175+
* kullanıcıya başka bir şey gibi gösterilemez. */
176+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'",
177+
},
178+
});
179+
}

0 commit comments

Comments
 (0)