fix(frontend): show the generic login error for an inherited ?error= code (#8721)

The login page looked the `?error=` value up in the localized error table
with plain indexing, so an inherited key resolved. `__proto__` returned
Object.prototype, which React refuses to render, and `useState` called the
function behind `constructor`, `valueOf` or `hasOwnProperty` as an
initializer. Each one replaced the page with Next's error screen, and the
server render answered 500. `toString` showed "[object Undefined]".

Only the table's own keys now name a message. Any other code shows the
generic "Authentication failed." as unknown codes already did.
This commit is contained in:
Hyeonsang Cho 2026-11-27 23:01:28 +09:00 committed by GitHub
parent 4b6af76252
commit ad58f4de72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 3 deletions

View File

@ -300,6 +300,8 @@ The frontend handles the post-SSO flow at `/auth/callback`:
3. On success: redirects to the workspace (or the original `next` path)
4. On failure: redirects to `/login?error=sso_failed`
The login page shows the localized message for a known `error` code. Any other value, including an inherited object key such as `__proto__` or `constructor`, shows the generic "Authentication failed." message.
## Security Notes
- **State cookies** are HttpOnly, SameSite=Lax, Max-Age=300 seconds, and signed with the JWT secret

View File

@ -23,6 +23,18 @@ import {
} from "@/core/auth/setup";
import { parseAuthError } from "@/core/auth/types";
import { useI18n } from "@/core/i18n/hooks";
import type { Translations } from "@/core/i18n/locales/types";
/** The message for a `?error=` code, or the generic failure. */
function loginErrorMessage(t: Translations, code: string) {
// The code comes from the URL, so only the table's own keys name a message.
// An inherited key such as `__proto__` or `constructor` resolves to an
// object or a function instead. The page renders the object, and
// `useState` calls the function as an initializer; both can crash the page.
return Object.hasOwn(t.login.errors, code)
? t.login.errors[code as keyof typeof t.login.errors]
: t.login.authFailed;
}
export default function LoginPage() {
const router = useRouter();
@ -54,8 +66,7 @@ export default function LoginPage() {
const ssrDisabled = useContext(AuthDisabledContext);
const [error, setError] = useState(
errorParam
? (t.login.errors[errorParam as keyof typeof t.login.errors] ??
t.login.authFailed)
? loginErrorMessage(t, errorParam)
: ssrDisabled
? t.login.errors.account_disabled
: "",

View File

@ -22,9 +22,11 @@ rs.mock("@/core/auth/AuthProvider", () => ({
useAuth: rs.fn(),
}));
rs.mock("@/core/i18n/hooks", () => ({ useI18n: () => ({ t: enUS }) }));
const navigationState = rs.hoisted(() => ({ search: "" }));
rs.mock("next/navigation", () => ({
useRouter: () => ({ push: rs.fn() }),
useSearchParams: () => new URLSearchParams(),
useSearchParams: () => new URLSearchParams(navigationState.search),
}));
rs.mock("next-themes", () => ({
useTheme: () => ({ theme: "light", resolvedTheme: "light" }),
@ -57,6 +59,7 @@ beforeEach(() => {
afterEach(() => {
cleanup();
rs.clearAllMocks();
navigationState.search = "";
});
function fillAndSubmit() {
@ -156,3 +159,26 @@ test("sign-up keeps the backend message so a disabled account cannot be probed",
expect(await screen.findByText("Account disabled")).toBeTruthy();
expect(screen.queryByText(enUS.login.errors.account_disabled)).toBeNull();
});
test("a known ?error= code shows its localized message", () => {
navigationState.search = "?error=sso_failed";
render(<LoginPage />);
expect(screen.getByText(enUS.login.errors.sso_failed)).toBeTruthy();
});
// The code comes from the URL. An inherited key names no message: `__proto__`
// resolves to an object and the others to functions that `useState` would
// call, so each one crashed the page or showed a junk string.
test.each([
"__proto__",
"constructor",
"valueOf",
"hasOwnProperty",
"toString",
])("an inherited ?error=%s shows the generic failure", (code) => {
navigationState.search = `?error=${code}`;
render(<LoginPage />);
expect(screen.getByText(enUS.login.authFailed)).toBeTruthy();
});