-
Notifications
You must be signed in to change notification settings - Fork 2
security(oauth): replace corsproxy.io with first-party Cloudflare Worker for token exchange #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
iamvirul
merged 10 commits into
OpenDevFlow:main
from
iamvirul:fix/github-oauth-token-proxy
Jun 1, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
50dc399
security(oauth): replace corsproxy.io with first-party Cloudflare Wor…
iamvirul d09e1a9
Implement feature X to enhance user experience and optimize performance
iamvirul 90fc332
ci(deploy): inject GitHub OAuth env vars into Next.js build step
iamvirul 7920c56
Update apps/web/lib/githubAuth.ts
iamvirul b7746b1
Update apps/github-oauth-proxy/src/index.ts
iamvirul dcf81e1
Update apps/github-oauth-proxy/src/index.ts
iamvirul 45ee251
Update apps/github-oauth-proxy/src/index.ts
iamvirul 1898352
chore(release): add 1.2.1 changelog entry and worker gitignore
iamvirul d5215fb
ci(deploy): add fail-fast validation step for required secrets
iamvirul 74c63ef
chore(changelog): remove version entry for 1.2.1 and update security …
iamvirul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .dev.vars | ||
| node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "@md-latex/github-oauth-proxy", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "dev": "wrangler dev", | ||
| "deploy": "wrangler deploy", | ||
| "type-check": "tsc --noEmit" | ||
| }, | ||
| "devDependencies": { | ||
| "@cloudflare/workers-types": "^4.20241230.0", | ||
| "typescript": "^5", | ||
| "wrangler": "^3" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| interface Env { | ||
| ALLOWED_ORIGIN: string; | ||
| } | ||
|
|
||
| interface TokenRequest { | ||
| client_id: string; | ||
| device_code: string; | ||
| grant_type: string; | ||
| } | ||
|
|
||
| const GITHUB_TOKEN_URL = 'https://github.com/login/oauth/access_token'; | ||
|
|
||
| export default { | ||
| async fetch(request: Request, env: Env): Promise<Response> { | ||
| const origin = request.headers.get('Origin') ?? ''; | ||
| const allowedOrigin = env.ALLOWED_ORIGIN; | ||
|
|
||
| if (request.method === 'OPTIONS') { | ||
| if (origin === allowedOrigin) { | ||
| return new Response(null, { status: 204, headers: corsHeaders(allowedOrigin) }); | ||
| } | ||
| return new Response(null, { status: 403 }); | ||
| } | ||
|
|
||
| if (request.method !== 'POST') { | ||
| return jsonResponse({ error: 'method_not_allowed' }, 405); | ||
| } | ||
|
|
||
| if (origin !== allowedOrigin) { | ||
| return jsonResponse({ error: 'forbidden' }, 403); | ||
| } | ||
|
|
||
| let body: TokenRequest; | ||
| try { | ||
| body = (await request.json()) as TokenRequest; | ||
| } catch { | ||
| return jsonResponse( | ||
| { error: 'invalid_request', error_description: 'Request body must be valid JSON.' }, | ||
| 400, | ||
| allowedOrigin, | ||
| ); | ||
| } | ||
|
|
||
| const { client_id, device_code, grant_type } = body; | ||
| if (!client_id || !device_code || !grant_type) { | ||
| return jsonResponse( | ||
| { error: 'invalid_request', error_description: 'Missing required fields: client_id, device_code, grant_type.' }, | ||
| 400, | ||
| allowedOrigin, | ||
| ); | ||
| } | ||
|
iamvirul marked this conversation as resolved.
|
||
| if (grant_type !== 'urn:ietf:params:oauth:grant-type:device_code') { | ||
| return jsonResponse( | ||
| { error: 'invalid_request', error_description: 'Invalid grant_type for device flow.' }, | ||
| 400, | ||
| allowedOrigin, | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| const githubRes = await fetch(GITHUB_TOKEN_URL, { | ||
| method: 'POST', | ||
| headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ client_id, device_code, grant_type }), | ||
| }); | ||
|
|
||
| const data = (await githubRes.json()) as Record<string, unknown>; | ||
| return jsonResponse(data, githubRes.status, allowedOrigin); | ||
| } catch { | ||
| return jsonResponse( | ||
| { error: 'upstream_error', error_description: 'Failed to reach GitHub OAuth endpoint.' }, | ||
| 502, | ||
| allowedOrigin, | ||
| ); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| function corsHeaders(origin: string): Record<string, string> { | ||
| return { | ||
| 'Access-Control-Allow-Origin': origin, | ||
| 'Access-Control-Allow-Methods': 'POST, OPTIONS', | ||
| 'Access-Control-Allow-Headers': 'Content-Type', | ||
| 'Access-Control-Max-Age': '86400', | ||
| }; | ||
| } | ||
|
|
||
| function jsonResponse( | ||
| body: Record<string, unknown>, | ||
| status: number, | ||
| allowedOrigin?: string, | ||
| ): Response { | ||
| return new Response(JSON.stringify(body), { | ||
| status, | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Cache-Control': 'no-store', | ||
| 'Pragma': 'no-cache', | ||
| ...(allowedOrigin ? corsHeaders(allowedOrigin) : {}), | ||
| }, | ||
|
iamvirul marked this conversation as resolved.
|
||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2022", | ||
| "lib": ["ES2022"], | ||
| "module": "ES2022", | ||
| "moduleResolution": "bundler", | ||
| "types": ["@cloudflare/workers-types"], | ||
| "strict": true, | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noImplicitReturns": true, | ||
| "skipLibCheck": true | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| name = "md-latex-github-oauth-proxy" | ||
| main = "src/index.ts" | ||
| compatibility_date = "2024-11-01" | ||
|
|
||
| [vars] | ||
| # Restrict CORS to the GitHub Pages origin. | ||
| # Override this via `wrangler secret put ALLOWED_ORIGIN` for production. | ||
| ALLOWED_ORIGIN = "https://opendevflow.github.io" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.