Skip to content

Commit a92074d

Browse files
committed
feat: encode origin in OAuth state parameter
Encode both CSRF token and referring origin in the OAuth state parameter as base64-encoded JSON. This allows passing the original domain through the OAuth flow for secure postMessage targeting.
1 parent c859e21 commit a92074d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ const handleAuth = async (request, env) => {
102102
// Generate a random string for CSRF protection
103103
const csrfToken = globalThis.crypto.randomUUID().replaceAll('-', '');
104104
let authURL = '';
105+
106+
// Create state parameter that includes both CSRF token and original domain
107+
const stateData = {
108+
csrf: csrfToken,
109+
origin: referringOrigin || origin
110+
};
111+
const state = btoa(JSON.stringify(stateData));
105112

106113
// GitHub
107114
if (provider === 'github') {
@@ -116,7 +123,7 @@ const handleAuth = async (request, env) => {
116123
const params = new URLSearchParams({
117124
client_id: GITHUB_CLIENT_ID,
118125
scope: 'repo,user',
119-
state: csrfToken,
126+
state: state,
120127
});
121128

122129
authURL = `https://${GITHUB_HOSTNAME}/login/oauth/authorize?${params.toString()}`;
@@ -137,7 +144,7 @@ const handleAuth = async (request, env) => {
137144
redirect_uri: `${origin}/callback`,
138145
response_type: 'code',
139146
scope: 'api',
140-
state: csrfToken,
147+
state: state,
141148
});
142149

143150
authURL = `https://${GITLAB_HOSTNAME}/oauth/authorize?${params.toString()}`;

0 commit comments

Comments
 (0)