fix (oauth-proxy): use of productionURL in for callback comparison (#4825)

This commit is contained in:
Dylan Vanmali
2025-09-23 00:47:16 +00:00
committed by GitHub
parent 27753f4826
commit f7e32006be
2 changed files with 79 additions and 1 deletions
@@ -56,6 +56,7 @@ export const oAuthProxy = (opts?: OAuthProxyOptions) => {
return {
id: "oauth-proxy",
options: opts,
endpoints: {
oAuthProxy: createAuthEndpoint(
"/oauth-proxy-callback",
@@ -157,7 +158,11 @@ export const oAuthProxy = (opts?: OAuthProxyOptions) => {
* We don't want to redirect to the proxy URL if the origin is the same
* as the current URL
*/
if (origin === getOrigin(ctx.context.baseURL)) {
const productionURL =
opts?.productionURL ||
ctx.context.options.baseURL ||
ctx.context.baseURL;
if (origin === getOrigin(productionURL)) {
const newLocation = locationURL.searchParams.get("callbackURL");
if (!newLocation) {
return;
@@ -113,4 +113,77 @@ describe("oauth-proxy", async () => {
},
});
});
it("should proxy to the original request url", async () => {
const { client } = await getTestInstance({
baseURL: "https://myapp.com",
plugins: [
oAuthProxy({
productionURL: "https://login.myapp.com",
}),
],
socialProviders: {
google: {
clientId: "test",
clientSecret: "test",
},
},
});
const res = await client.signIn.social(
{
provider: "google",
callbackURL: "/dashboard",
},
{
throw: true,
},
);
const state = new URL(res.url!).searchParams.get("state");
await client.$fetch(`/callback/google?code=test&state=${state}`, {
onError(context) {
const location = context.response.headers.get("location");
if (!location) {
throw new Error("Location header not found");
}
expect(location).toContain(
"https://myapp.com/api/auth/oauth-proxy-callback?callbackURL=%2Fdashboard",
);
const cookies = new URL(location).searchParams.get("cookies");
expect(cookies).toBeTruthy();
},
});
});
it("shouldn't redirect to proxy url on same origin", async () => {
const { client } = await getTestInstance({
baseURL: "https://myapp.com",
plugins: [oAuthProxy()],
socialProviders: {
google: {
clientId: "test",
clientSecret: "test",
},
},
});
const res = await client.signIn.social(
{
provider: "google",
callbackURL: "/dashboard",
},
{
throw: true,
},
);
const state = new URL(res.url!).searchParams.get("state");
await client.$fetch(`/callback/google?code=test&state=${state}`, {
onError(context) {
const location = context.response.headers.get("location");
if (!location) {
throw new Error("Location header not found");
}
expect(location).not.toContain("/api/auth/oauth-proxy-callback");
expect(location).toContain("/dashboard");
},
});
});
});