Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Commit 5a3eabe

Browse files
authored
Support github.dev redirect URL (#340)
1 parent 67d6bd0 commit 5a3eabe

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

redirect/functions/redirect.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
// @ts-check
2-
const schemes = new Set(['vscode', 'vscode-insiders', 'vscodium', 'gitpod-code', 'code-oss'])
3-
const validQueryParams = new Set(['vscode-reqid', 'vscode-scheme', 'vscode-authority', 'vscode-path', 'windowId'])
2+
let schemes = new Set([
3+
'vscode',
4+
'vscode-insiders',
5+
'vscodium',
6+
'gitpod-code',
7+
'code-oss',
8+
])
9+
let validQueryParams = new Set([
10+
'vscode-reqid',
11+
'vscode-scheme',
12+
'vscode-authority',
13+
'vscode-path',
14+
'windowId',
15+
])
416

517
/**
618
* @param {import('@netlify/functions').HandlerEvent} event
@@ -12,7 +24,12 @@ exports.handler = async function (event, _context) {
1224
if (state == null) throw new Error(`Missing "state" query parameter.`)
1325
const url = new URL(Buffer.from(state, 'base64url').toString())
1426
const scheme = url.protocol.slice(0, -1)
15-
if (scheme === 'http' || scheme === 'https') {
27+
if (
28+
url.host === 'https://github.dev' &&
29+
url.pathname === '/extension-auth-callback'
30+
) {
31+
validQueryParams = new Set(['state'])
32+
} else if (scheme === 'http' || scheme === 'https') {
1633
validate(
1734
url.searchParams.get('vscode-scheme'),
1835
url.searchParams.get('vscode-authority'),
@@ -22,20 +39,20 @@ exports.handler = async function (event, _context) {
2239
validate(scheme, url.host, url.pathname)
2340
}
2441

42+
url.searchParams.set('code', code)
2543
url.searchParams.forEach((_, key) => {
2644
if (!validQueryParams.has(key)) url.searchParams.delete(key)
2745
})
2846

29-
url.searchParams.set('code', code)
30-
url.searchParams.set('state', '')
31-
3247
return getResponse(url.toString())
3348
}
3449

3550
function validate(scheme, hostname, pathname) {
3651
if (!schemes.has(scheme)) throw new Error(`Invalid scheme: ${scheme}`)
37-
if (hostname !== 'znck.grammarly') throw new Error(`Invalid authority: ${hostname}`)
38-
if (pathname !== '/auth/callback') throw new Error(`Invalid path: ${pathname}`)
52+
if (hostname !== 'znck.grammarly')
53+
throw new Error(`Invalid authority: ${hostname}`)
54+
if (pathname !== '/auth/callback')
55+
throw new Error(`Invalid path: ${pathname}`)
3956
}
4057

4158
/**

0 commit comments

Comments
 (0)