mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-10 04:19:32 +00:00
fix: assert state is short enough to be stored in cookie
This commit is contained in:
@@ -124,7 +124,6 @@ export default function SignIn() {
|
|||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
await signIn.social({
|
await signIn.social({
|
||||||
provider: "discord",
|
provider: "discord",
|
||||||
callbackURL: "/dashboard",
|
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -59,8 +59,7 @@ export const signInOAuth = createAuthEndpoint(
|
|||||||
: `${currentURL?.origin}${c.body.callbackURL || ""}`;
|
: `${currentURL?.origin}${c.body.callbackURL || ""}`;
|
||||||
|
|
||||||
const state = generateState(
|
const state = generateState(
|
||||||
callbackURL || currentURL?.origin || c.context.baseURL,
|
callbackURL || currentURL?.origin || c.context.options.baseURL,
|
||||||
c.query?.currentURL,
|
|
||||||
);
|
);
|
||||||
await c.setSignedCookie(
|
await c.setSignedCookie(
|
||||||
cookie.state.name,
|
cookie.state.name,
|
||||||
|
|||||||
@@ -204,8 +204,7 @@ export const genericOAuth = (options: GenericOAuthOptions) => {
|
|||||||
? ctx.body.callbackURL
|
? ctx.body.callbackURL
|
||||||
: `${currentURL?.origin}${ctx.body.callbackURL || ""}`;
|
: `${currentURL?.origin}${ctx.body.callbackURL || ""}`;
|
||||||
const state = generateState(
|
const state = generateState(
|
||||||
callbackURL || currentURL?.origin || ctx.context.baseURL,
|
callbackURL || currentURL?.origin || ctx.context.options.baseURL,
|
||||||
ctx.query?.currentURL,
|
|
||||||
);
|
);
|
||||||
const cookie = ctx.context.authCookies;
|
const cookie = ctx.context.authCookies;
|
||||||
await ctx.setSignedCookie(
|
await ctx.setSignedCookie(
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
import { generateState as generateStateOAuth } from "oslo/oauth2";
|
import { generateState as generateStateOAuth } from "oslo/oauth2";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { BetterAuthError } from "../error/better-auth-error";
|
||||||
|
|
||||||
export function generateState(callbackURL?: string, currentURL?: string) {
|
export function generateState(callbackURL?: string) {
|
||||||
const code = generateStateOAuth();
|
const code = generateStateOAuth();
|
||||||
const state = JSON.stringify({
|
const state = JSON.stringify({
|
||||||
code,
|
code,
|
||||||
callbackURL,
|
callbackURL,
|
||||||
currentURL,
|
|
||||||
});
|
});
|
||||||
|
if (state.length > 4000) {
|
||||||
|
throw new BetterAuthError(
|
||||||
|
"State is too long to be safely stored in a cookie. Make sure the callbackURL is not too long.",
|
||||||
|
);
|
||||||
|
}
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user