mirror of
https://github.com/better-auth/better-auth.git
synced 2026-07-30 06:04:15 -05:00
fix (oauth-proxy): use of productionURL in for callback comparison (#4825)
This commit is contained in:
@@ -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");
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user