Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/verify-oauth-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Verify the OAuth state parameter in the GitHub callback.
213 changes: 213 additions & 0 deletions packages/keystatic/src/api/generic.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/** @vitest-environment node */
import http from 'node:http';
import { AddressInfo } from 'node:net';
import { afterAll, beforeAll, beforeEach, expect, test, vi } from 'vitest';
import { config } from '../config';
import { makeGenericAPIRouteHandler } from './generic';

// runs the real login and callback handlers behind a node http server and
// points the GitHub token request at a local stub server

const realFetch = globalThis.fetch;

let tokenRequests: URL[] = [];
let githubOrigin: string;
let appOrigin: string;
const servers: http.Server[] = [];

function listen(server: http.Server) {
servers.push(server);
return new Promise<string>(resolve => {
server.listen(0, '127.0.0.1', () => {
const { port } = server.address() as AddressInfo;
resolve(`http://127.0.0.1:${port}`);
});
});
}

beforeAll(async () => {
githubOrigin = await listen(
http.createServer((req, res) => {
const url = new URL(req.url!, 'http://localhost');
if (
req.method === 'POST' &&
url.pathname === '/login/oauth/access_token'
) {
tokenRequests.push(url);
res.setHeader('content-type', 'application/json');
res.end(
JSON.stringify({
access_token: `access-${url.searchParams.get('code')}`,
expires_in: 28800,
refresh_token: 'refresh-token',
refresh_token_expires_in: 15897600,
scope: '',
token_type: 'bearer',
})
);
return;
}
res.statusCode = 404;
res.end();
})
);

const handler = makeGenericAPIRouteHandler({
clientId: 'client-id',
clientSecret: 'client-secret',
secret: 'a'.repeat(32),
config: config({ storage: { kind: 'github', repo: 'owner/repo' } }),
});
appOrigin = await listen(
http.createServer(async (req, res) => {
const result = await handler({
url: new URL(req.url!, appOrigin).toString(),
method: req.method!,
headers: {
get: name => {
const val = req.headers[name.toLowerCase()];
return (Array.isArray(val) ? val[0] : val) ?? null;
},
},
json: async () => ({}),
});
const grouped = new Map<string, string[]>();
for (const [key, value] of (result.headers ?? []) as [string, string][]) {
grouped.set(key, [...(grouped.get(key) ?? []), value]);
}
for (const [key, value] of grouped) res.setHeader(key, value);
res.statusCode = result.status ?? 200;
res.end(result.body);
})
);

vi.stubGlobal(
'fetch',
(input: string | URL | Request, init?: RequestInit) => {
const url = new URL(input instanceof Request ? input.url : input);
if (url.origin === 'https://github.com') {
return realFetch(`${githubOrigin}${url.pathname}${url.search}`, init);
}
return realFetch(input, init);
}
);
});

afterAll(async () => {
vi.unstubAllGlobals();
await Promise.all(
servers.map(server => new Promise(resolve => server.close(resolve)))
);
});

beforeEach(() => {
tokenRequests = [];
});

// a tiny cookie jar for one browser
function makeBrowser() {
const jar = new Map<string, string>();
return {
jar,
async get(path: string) {
const res = await realFetch(`${appOrigin}${path}`, {
redirect: 'manual',
headers: {
cookie: [...jar].map(([key, value]) => `${key}=${value}`).join('; '),
},
});
const setCookies = res.headers.getSetCookie();
for (const setCookie of setCookies) {
const [pair] = setCookie.split(';');
const index = pair.indexOf('=');
const key = pair.slice(0, index);
const value = pair.slice(index + 1);
if (/max-age=0/i.test(setCookie)) jar.delete(key);
else jar.set(key, value);
}
return { res, setCookies, location: res.headers.get('location') };
},
};
}

async function startLogin(browser: ReturnType<typeof makeBrowser>, query = '') {
const { res, location } = await browser.get(
`/api/keystatic/github/login${query}`
);
expect(res.status).toBe(307);
const authorizeUrl = new URL(location!);
expect(authorizeUrl.origin).toBe('https://github.com');
const state = authorizeUrl.searchParams.get('state');
expect(state).toBeTruthy();
return state!;
}

test('normal login signs in and clears the state cookie', async () => {
const browser = makeBrowser();
const state = await startLogin(browser);
expect(browser.jar.has(`ks-${state}`)).toBe(true);

const { res, location } = await browser.get(
`/api/keystatic/github/oauth/callback?code=good&state=${state}`
);
expect(res.status).toBe(307);
expect(location).toBe('/keystatic');
expect(tokenRequests.map(x => x.searchParams.get('code'))).toEqual(['good']);
expect(browser.jar.get('keystatic-gh-access-token')).toBe('access-good');
expect(browser.jar.has(`ks-${state}`)).toBe(false);
});

test('login with from redirects back to the deep link', async () => {
const browser = makeBrowser();
const from = 'branch/main/collection/posts';
const state = await startLogin(browser, `?from=${encodeURIComponent(from)}`);

const { res, location } = await browser.get(
`/api/keystatic/github/oauth/callback?code=good&state=${state}`
);
expect(res.status).toBe(307);
expect(location).toBe(`/keystatic/${from}`);
expect(tokenRequests).toHaveLength(1);
});

test('a forged callback with the attacker state is rejected without a token request', async () => {
// the attacker starts a login in their own browser and gets their own code
const attacker = makeBrowser();
const attackerState = await startLogin(attacker);

// the victim is sent to the callback with the attacker's code and state
const victim = makeBrowser();
for (const query of [
`code=attacker&state=${attackerState}`,
'code=attacker',
]) {
const { res, setCookies } = await victim.get(
`/api/keystatic/github/oauth/callback?${query}`
);
expect(res.status).toBe(400);
expect(setCookies).toEqual([]);
}
// also when the victim has a login of their own in progress
await startLogin(victim);
const { res } = await victim.get(
`/api/keystatic/github/oauth/callback?code=attacker&state=${attackerState}`
);
expect(res.status).toBe(400);

expect(tokenRequests).toEqual([]);
expect(victim.jar.has('keystatic-gh-access-token')).toBe(false);
});

test('the GitHub App install popup closes without a token request or cookies', async () => {
const browser = makeBrowser();
const { res, setCookies } = await browser.get(
'/api/keystatic/github/oauth/callback?code=install&state=close'
);
expect(res.status).toBe(200);
expect(res.headers.get('content-type')).toBe('text/html');
const body = await res.text();
expect(body).toContain("localStorage.setItem('ks-refetch-installations'");
expect(body).toContain('window.close()');
expect(setCookies).toEqual([]);
expect(tokenRequests).toEqual([]);
});
135 changes: 135 additions & 0 deletions packages/keystatic/src/api/generic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/** @vitest-environment node */
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import { config } from '../config';
import { makeGenericAPIRouteHandler } from './generic';
import { KeystaticRequest } from './internal-utils';

const origin = 'http://localhost:3000';

const handler = makeGenericAPIRouteHandler({
clientId: 'client-id',
clientSecret: 'client-secret',
secret: 'a'.repeat(32),
config: config({ storage: { kind: 'github', repo: 'owner/repo' } }),
});

function makeRequest(path: string, cookieHeader?: string): KeystaticRequest {
return {
url: `${origin}${path}`,
method: 'GET',
headers: {
get: name =>
name.toLowerCase() === 'cookie' ? cookieHeader ?? null : null,
},
json: async () => ({}),
};
}

function getHeaders(res: { headers?: unknown }) {
return (res.headers ?? []) as [string, string][];
}

function getSetCookies(res: { headers?: unknown }) {
return getHeaders(res)
.filter(([key]) => key === 'Set-Cookie')
.map(([, value]) => value);
}

const fetchMock = vi.fn();

beforeEach(() => {
vi.stubGlobal('fetch', fetchMock);
fetchMock.mockResolvedValue({
ok: true,
json: async () => ({
access_token: 'access-token',
expires_in: 28800,
refresh_token: 'refresh-token',
refresh_token_expires_in: 15897600,
scope: '',
token_type: 'bearer',
}),
});
});

afterEach(() => {
vi.unstubAllGlobals();
});

test('callback without state is rejected without calling GitHub', async () => {
const res = await handler(
makeRequest('/api/keystatic/github/oauth/callback?code=abc')
);
expect(res.status).toBe(400);
expect(getSetCookies(res)).toEqual([]);
expect(fetchMock).not.toHaveBeenCalled();
});

test('callback with state but no state cookie is rejected', async () => {
const res = await handler(
makeRequest('/api/keystatic/github/oauth/callback?code=abc&state=1234')
);
expect(res.status).toBe(400);
expect(fetchMock).not.toHaveBeenCalled();
});

test('callback with a state cookie for a different state is rejected', async () => {
const res = await handler(
makeRequest(
'/api/keystatic/github/oauth/callback?code=abc&state=1234',
'ks-5678=%2F'
)
);
expect(res.status).toBe(400);
expect(fetchMock).not.toHaveBeenCalled();
});

test('callback with a matching state cookie signs in and clears the state cookie', async () => {
const res = await handler(
makeRequest(
'/api/keystatic/github/oauth/callback?code=abc&state=1234',
'ks-1234=branch%2Fmain'
)
);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(res.status).toBe(307);
expect(getHeaders(res)).toContainEqual([
'Location',
'/keystatic/branch/main',
]);
const setCookies = getSetCookies(res);
expect(setCookies.some(x => x.startsWith('keystatic-gh-access-token='))).toBe(
true
);
expect(
setCookies.some(x => x.startsWith('keystatic-gh-refresh-token='))
).toBe(true);
const stateCookie = setCookies.find(x => x.startsWith('ks-1234='));
expect(stateCookie).toMatch(/^ks-1234=;/);
expect(stateCookie).toContain('Max-Age=0');
expect(stateCookie).toContain('Path=/api/keystatic/github/oauth/callback');
});

test('callback for the GitHub App install popup does not exchange the code', async () => {
const res = await handler(
makeRequest('/api/keystatic/github/oauth/callback?code=abc&state=close')
);
expect(res.status).toBe(200);
expect(getSetCookies(res)).toEqual([]);
expect(fetchMock).not.toHaveBeenCalled();
});

test('login without from still sets a state cookie', async () => {
const res = await handler(makeRequest('/api/keystatic/github/login'));
expect(res.status).toBe(307);
const location = getHeaders(res).find(([key]) => key === 'Location')?.[1];
const state = new URL(location!).searchParams.get('state');
expect(state).toMatch(/^[0-9a-f]{20}$/);
const setCookies = getSetCookies(res);
expect(setCookies).toHaveLength(1);
expect(setCookies[0]).toMatch(new RegExp(`^ks-${state}=`));
expect(setCookies[0]).toContain('HttpOnly');
expect(setCookies[0]).toContain('SameSite=Lax');
expect(setCookies[0]).toContain('Max-Age=600');
expect(setCookies[0]).toContain('Path=/api/keystatic/github/oauth/callback');
});
Loading