Skip to content

Commit 7e40af0

Browse files
chore(better-auth): fix inferred trusted origins on cloned context (better-auth#7007)
1 parent 9c54c10 commit 7e40af0

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

packages/better-auth/src/api/middlewares/origin-check.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ describe("origin check middleware", async (it) => {
501501
describe("trusted origins with baseURL inferred from request", async (it) => {
502502
it("should respect trustedOrigins array when baseURL is NOT in config", async () => {
503503
const { customFetchImpl, testUser } = await getTestInstance({
504+
baseURL: undefined,
504505
trustedOrigins: ["http://my-frontend.com"],
505506
emailAndPassword: {
506507
enabled: true,
@@ -533,6 +534,7 @@ describe("trusted origins with baseURL inferred from request", async (it) => {
533534

534535
it("should reject untrusted origins even when baseURL is inferred", async () => {
535536
const { customFetchImpl, testUser } = await getTestInstance({
537+
baseURL: undefined,
536538
trustedOrigins: ["http://my-frontend.com"],
537539
emailAndPassword: {
538540
enabled: true,
@@ -567,6 +569,7 @@ describe("trusted origins with baseURL inferred from request", async (it) => {
567569

568570
try {
569571
const { customFetchImpl, testUser } = await getTestInstance({
572+
baseURL: undefined,
570573
emailAndPassword: {
571574
enabled: true,
572575
},
@@ -601,6 +604,7 @@ describe("trusted origins with baseURL inferred from request", async (it) => {
601604

602605
it("should allow requests from inferred baseURL origin", async () => {
603606
const { customFetchImpl, testUser } = await getTestInstance({
607+
baseURL: undefined,
604608
emailAndPassword: {
605609
enabled: true,
606610
},
@@ -635,6 +639,7 @@ describe("trusted origins with baseURL inferred from request", async (it) => {
635639

636640
try {
637641
const { customFetchImpl, testUser } = await getTestInstance({
642+
baseURL: undefined,
638643
trustedOrigins: ["http://config-origin.com"],
639644
emailAndPassword: {
640645
enabled: true,

packages/better-auth/src/auth/trusted-origins.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ async function createAuthTestInstance(overrides?: Partial<BetterAuthOptions>) {
3535

3636
const { auth, client } = await getTestInstance(
3737
{
38-
plugins: [testServerPlugin],
3938
...overrides,
39+
plugins: [testServerPlugin, ...(overrides?.plugins || [])],
4040
},
4141
{ clientOptions: { plugins: [testClientPlugin] } },
4242
);
@@ -73,6 +73,44 @@ describe("trusted origins", () => {
7373
).resolves.toBe(true);
7474
});
7575

76+
it("should always allow the app's origin (inferred from baseURL)", async () => {
77+
const { isTrustedOrigin } = await createAuthTestInstance({
78+
baseURL: undefined,
79+
});
80+
81+
await expect(isTrustedOrigin("http://localhost:3000")).resolves.toBe(
82+
true,
83+
);
84+
85+
await expect(
86+
isTrustedOrigin("http://localhost:3000/some/path"),
87+
).resolves.toBe(true);
88+
});
89+
90+
it("should always allow the app's origin (even if context is updated)", async () => {
91+
const { isTrustedOrigin } = await createAuthTestInstance({
92+
baseURL: undefined,
93+
plugins: [
94+
{
95+
id: "test-init-plugin",
96+
init() {
97+
return {
98+
context: {},
99+
};
100+
},
101+
},
102+
],
103+
});
104+
105+
await expect(isTrustedOrigin("http://localhost:3000")).resolves.toBe(
106+
true,
107+
);
108+
109+
await expect(
110+
isTrustedOrigin("http://localhost:3000/some/path"),
111+
).resolves.toBe(true);
112+
});
113+
76114
it("should reject origins that start with a trusted origin", async () => {
77115
const { isTrustedOrigin } = await createAuthTestInstance({
78116
trustedOrigins: ["https://trusted.com"],

packages/better-auth/src/context/create-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export async function createAuthContext(
189189
allowRelativePaths: boolean;
190190
},
191191
) {
192-
return ctx.trustedOrigins.some((origin) =>
192+
return this.trustedOrigins.some((origin) =>
193193
matchesOriginPattern(url, origin, settings),
194194
);
195195
},

packages/sso/src/routes/sso.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ export const registerSSOProvider = <O extends SSOOptions>(options: O) => {
735735
tokenEndpointAuthentication:
736736
body.oidcConfig.tokenEndpointAuthentication,
737737
},
738-
isTrustedOrigin: ctx.context.isTrustedOrigin,
738+
isTrustedOrigin: (url: string) => ctx.context.isTrustedOrigin(url),
739739
});
740740
} catch (error) {
741741
if (error instanceof DiscoveryError) {

0 commit comments

Comments
 (0)