Skip to content

Commit 5523569

Browse files
committed
a few tweaks via CodeRabbit
1 parent c95b0f5 commit 5523569

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { type ActionFunction, type LoaderFunction, redirect, createCookie } from "@remix-run/node";
22
import { authenticator } from "~/services/auth.server";
3+
import { env } from "~/env.server";
4+
import { sanitizeRedirectPath } from "~/utils";
35

46
export let loader: LoaderFunction = () => redirect("/login");
57

68
export let action: ActionFunction = async ({ request }) => {
79
const url = new URL(request.url);
810
const redirectTo = url.searchParams.get("redirectTo");
11+
const safeRedirect = sanitizeRedirectPath(redirectTo, "/");
912

1013
try {
1114
// call authenticate as usual, in successRedirect use returnTo or a fallback
1215
return await authenticator.authenticate("github", request, {
13-
successRedirect: redirectTo ?? "/",
16+
successRedirect: safeRedirect,
1417
failureRedirect: "/login",
1518
});
1619
} catch (error) {
@@ -19,8 +22,8 @@ export let action: ActionFunction = async ({ request }) => {
1922
// if the error is a Response and is a redirect
2023
if (error instanceof Response) {
2124
// we need to append a Set-Cookie header with a cookie storing the
22-
// returnTo value
23-
error.headers.append("Set-Cookie", await redirectCookie.serialize(redirectTo));
25+
// returnTo value (store the sanitized path)
26+
error.headers.append("Set-Cookie", await redirectCookie.serialize(safeRedirect));
2427
}
2528
throw error;
2629
}
@@ -29,4 +32,6 @@ export let action: ActionFunction = async ({ request }) => {
2932
export const redirectCookie = createCookie("redirect-to", {
3033
maxAge: 60 * 60, // 1 hour
3134
httpOnly: true,
35+
sameSite: "lax",
36+
secure: env.NODE_ENV === "production",
3237
});
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { type ActionFunction, type LoaderFunction, redirect, createCookie } from "@remix-run/node";
22
import { authenticator } from "~/services/auth.server";
3+
import { env } from "~/env.server";
4+
import { sanitizeRedirectPath } from "~/utils";
35

46
export let loader: LoaderFunction = () => redirect("/login");
57

68
export let action: ActionFunction = async ({ request }) => {
79
const url = new URL(request.url);
810
const redirectTo = url.searchParams.get("redirectTo");
11+
const safeRedirect = sanitizeRedirectPath(redirectTo, "/");
912

1013
try {
1114
// call authenticate as usual, in successRedirect use returnTo or a fallback
1215
return await authenticator.authenticate("google", request, {
13-
successRedirect: redirectTo ?? "/",
16+
successRedirect: safeRedirect,
1417
failureRedirect: "/login",
1518
});
1619
} catch (error) {
@@ -19,8 +22,8 @@ export let action: ActionFunction = async ({ request }) => {
1922
// if the error is a Response and is a redirect
2023
if (error instanceof Response) {
2124
// we need to append a Set-Cookie header with a cookie storing the
22-
// returnTo value
23-
error.headers.append("Set-Cookie", await redirectCookie.serialize(redirectTo));
25+
// returnTo value (store the sanitized path)
26+
error.headers.append("Set-Cookie", await redirectCookie.serialize(safeRedirect));
2427
}
2528
throw error;
2629
}
@@ -29,5 +32,7 @@ export let action: ActionFunction = async ({ request }) => {
2932
export const redirectCookie = createCookie("google-redirect-to", {
3033
maxAge: 60 * 60, // 1 hour
3134
httpOnly: true,
35+
sameSite: "lax",
36+
secure: env.NODE_ENV === "production",
3237
});
3338

apps/webapp/app/services/lastAuthMethod.server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createCookie } from "@remix-run/node";
2+
import { env } from "~/env.server";
23

34
export type LastAuthMethod = "github" | "google" | "email";
45

@@ -7,7 +8,7 @@ export const lastAuthMethodCookie = createCookie("last-auth-method", {
78
maxAge: 60 * 60 * 24 * 365, // 1 year
89
httpOnly: true,
910
sameSite: "lax",
10-
secure: process.env.NODE_ENV === "production",
11+
secure: env.NODE_ENV === "production",
1112
});
1213

1314
export async function getLastAuthMethod(request: Request): Promise<LastAuthMethod | null> {

0 commit comments

Comments
 (0)