# Description of Changes Fixes share-link navigation for SSO users. Reported on v2.9.2 with `SSOAutoLogin: true`: clicking a `/share/<token>` link in an email redirected the user to the home page after SSO instead of the shared file. ## Root cause Three compounding issues had to be fixed together; the first was the initial symptom but the other two only surfaced during live verification. 1. **Spring Security blocked `/share/<token>` for unauthenticated users.** The route wasn't in `RequestUriUtils.isPublicAuthEndpoint`, so the server 302'd straight to `/login` before React could load `ShareLinkPage`. The share URL was lost because `NullRequestCache` is configured and never persisted the original destination. 2. **`httpErrorHandler` full-page-redirected to `/login?from=<path>` on any unhandled 401** (fired by `LicenseContext`, `AppConfig`, etc. during normal ShareLinkPage mount). That *did* preserve the return path — but **Spring Security strips query strings from `/login`** (302 to bare `/login`), so `?from=` never reached React. Confirmed via `curl -i http://localhost:8080/login?from=xyz` → `Location: /login`. 3. **`AuthCallback.tsx` unconditionally `navigate("/")`** after the SAML/OAuth round-trip, discarding any intended destination. ## Fix **Backend** — make `/share/<token>` a public SPA bootstrap, data APIs stay protected: - `RequestUriUtils.isPublicAuthEndpoint` — permits `^/share/[^/]+/?$` (tight regex, single token segment only; `/share/<token>/anything` stays protected). - `ReactRoutingController` — dedicated `@GetMapping("/share/{token}")` mirroring `/auth/callback`. - `/api/v1/storage/share-links/**` remains behind Spring Security with its existing `canAccessShareLink` check. **Frontend** — persist the return path across full-page redirects via `sessionStorage` (same-origin, survives the SSO round-trip): - `httpErrorHandler.ts` — stashes current pathname to `stirling_post_login_path` before the 401 → `/login` redirect. - `springAuthClient.ts` — new `isSafePostLoginRedirect` / `setPostLoginRedirectPath` / `consumePostLoginRedirectPath` helpers (rejects protocol-relative URLs and auth-plumbing paths to guard against open-redirect abuse). - `Login.tsx` — on explicit user sign-in, read path from `location.state` or `?from=` query and stash it; don't clobber an already-stashed value. - `AuthCallback.tsx` — consume the stashed path (single-use) and `navigate(target)` instead of always `/`. --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### Translations (if applicable) - [ ] I ran [`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have run `task check` to verify linters, typechecks, and tests pass - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#7-testing) for more details. --------- Co-authored-by: EthanHealy01 <80844253+EthanHealy01@users.noreply.github.com>
483 lines
15 KiB
TypeScript
483 lines
15 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, afterEach } from "vitest";
|
|
import {
|
|
consumePostLoginRedirectPath,
|
|
isSafePostLoginRedirect,
|
|
POST_LOGIN_REDIRECT_STORAGE_KEY,
|
|
setPostLoginRedirectPath,
|
|
springAuth,
|
|
} from "@app/auth/springAuthClient";
|
|
import { startOAuthNavigation } from "@app/extensions/oauthNavigation";
|
|
import apiClient from "@app/services/apiClient";
|
|
import {
|
|
AxiosError,
|
|
type AxiosResponse,
|
|
type InternalAxiosRequestConfig,
|
|
} from "axios";
|
|
|
|
// Mock apiClient
|
|
vi.mock("@app/services/apiClient");
|
|
vi.mock("@app/extensions/oauthNavigation", () => ({
|
|
startOAuthNavigation: vi.fn().mockResolvedValue(false),
|
|
}));
|
|
|
|
describe("SpringAuthClient", () => {
|
|
beforeEach(() => {
|
|
// Clear localStorage before each test
|
|
localStorage.clear();
|
|
// Clear all mocks
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("getSession", () => {
|
|
it("should return null session when no JWT in localStorage", async () => {
|
|
const result = await springAuth.getSession();
|
|
|
|
expect(result.data.session).toBeNull();
|
|
expect(result.error).toBeNull();
|
|
expect(apiClient.get).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should validate JWT and return session when JWT exists", async () => {
|
|
const mockToken = "mock-jwt-token";
|
|
const mockUser = {
|
|
id: "123",
|
|
email: "test@example.com",
|
|
username: "testuser",
|
|
role: "USER",
|
|
};
|
|
|
|
localStorage.setItem("stirling_jwt", mockToken);
|
|
|
|
vi.mocked(apiClient.get).mockResolvedValueOnce({
|
|
status: 200,
|
|
data: { user: mockUser },
|
|
} as unknown as AxiosResponse);
|
|
|
|
const result = await springAuth.getSession();
|
|
|
|
expect(apiClient.get).toHaveBeenCalledWith("/api/v1/auth/me", {
|
|
headers: { Authorization: `Bearer ${mockToken}` },
|
|
suppressErrorToast: true,
|
|
skipAuthRedirect: true,
|
|
});
|
|
expect(result.data.session).toBeTruthy();
|
|
expect(result.data.session?.user).toEqual(mockUser);
|
|
expect(result.data.session?.access_token).toBe(mockToken);
|
|
expect(result.error).toBeNull();
|
|
});
|
|
|
|
it("should clear invalid JWT on 401 error", async () => {
|
|
const mockToken = "invalid-jwt-token";
|
|
localStorage.setItem("stirling_jwt", mockToken);
|
|
|
|
const mockError = new AxiosError(
|
|
"Unauthorized",
|
|
"ERR_BAD_REQUEST",
|
|
undefined,
|
|
undefined,
|
|
{
|
|
status: 401,
|
|
statusText: "Unauthorized",
|
|
data: {},
|
|
headers: {},
|
|
config: {} as InternalAxiosRequestConfig,
|
|
},
|
|
);
|
|
|
|
vi.mocked(apiClient.get).mockRejectedValueOnce(mockError);
|
|
|
|
const result = await springAuth.getSession();
|
|
|
|
expect(localStorage.getItem("stirling_jwt")).toBeNull();
|
|
expect(result.data.session).toBeNull();
|
|
// 401 is handled gracefully, so error should be null
|
|
expect(result.error).toBeNull();
|
|
});
|
|
|
|
it("should clear invalid JWT on 403 error", async () => {
|
|
const mockToken = "forbidden-jwt-token";
|
|
localStorage.setItem("stirling_jwt", mockToken);
|
|
|
|
const mockError = new AxiosError(
|
|
"Forbidden",
|
|
"ERR_BAD_REQUEST",
|
|
undefined,
|
|
undefined,
|
|
{
|
|
status: 403,
|
|
statusText: "Forbidden",
|
|
data: {},
|
|
headers: {},
|
|
config: {} as InternalAxiosRequestConfig,
|
|
},
|
|
);
|
|
|
|
vi.mocked(apiClient.get).mockRejectedValueOnce(mockError);
|
|
|
|
const result = await springAuth.getSession();
|
|
|
|
expect(localStorage.getItem("stirling_jwt")).toBeNull();
|
|
expect(result.data.session).toBeNull();
|
|
// 403 is handled gracefully, so error should be null
|
|
expect(result.error).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("signInWithPassword", () => {
|
|
it("should successfully sign in with email and password", async () => {
|
|
const credentials = {
|
|
email: "test@example.com",
|
|
password: "password123",
|
|
};
|
|
|
|
const mockToken = "new-jwt-token";
|
|
const mockUser = {
|
|
id: "123",
|
|
email: credentials.email,
|
|
username: credentials.email,
|
|
role: "USER",
|
|
};
|
|
|
|
vi.mocked(apiClient.post).mockResolvedValueOnce({
|
|
status: 200,
|
|
data: {
|
|
user: mockUser,
|
|
session: {
|
|
access_token: mockToken,
|
|
expires_in: 3600,
|
|
},
|
|
},
|
|
} as unknown as AxiosResponse);
|
|
|
|
// Spy on window.dispatchEvent
|
|
const dispatchEventSpy = vi.spyOn(window, "dispatchEvent");
|
|
|
|
const result = await springAuth.signInWithPassword(credentials);
|
|
|
|
expect(apiClient.post).toHaveBeenCalledWith(
|
|
"/api/v1/auth/login",
|
|
{
|
|
username: credentials.email,
|
|
password: credentials.password,
|
|
},
|
|
{ withCredentials: true },
|
|
);
|
|
expect(localStorage.getItem("stirling_jwt")).toBe(mockToken);
|
|
expect(dispatchEventSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({ type: "jwt-available" }),
|
|
);
|
|
expect(result.user).toEqual(mockUser);
|
|
expect(result.session?.access_token).toBe(mockToken);
|
|
expect(result.error).toBeNull();
|
|
});
|
|
|
|
it("should return error on failed login", async () => {
|
|
const credentials = {
|
|
email: "wrong@example.com",
|
|
password: "wrongpassword",
|
|
};
|
|
|
|
const errorMessage = "Invalid credentials";
|
|
const mockError = Object.assign(new Error(errorMessage), {
|
|
isAxiosError: true,
|
|
response: {
|
|
status: 401,
|
|
data: { message: errorMessage },
|
|
},
|
|
});
|
|
|
|
vi.mocked(apiClient.post).mockRejectedValueOnce(mockError);
|
|
|
|
const result = await springAuth.signInWithPassword(credentials);
|
|
|
|
expect(result.user).toBeNull();
|
|
expect(result.session).toBeNull();
|
|
expect(result.error).toBeTruthy();
|
|
expect(result.error?.message).toBe(errorMessage);
|
|
});
|
|
});
|
|
|
|
describe("signUp", () => {
|
|
it("should successfully register new user", async () => {
|
|
const credentials = {
|
|
email: "newuser@example.com",
|
|
password: "newpassword123",
|
|
};
|
|
|
|
const mockUser = {
|
|
id: "456",
|
|
email: credentials.email,
|
|
username: credentials.email,
|
|
role: "USER",
|
|
};
|
|
|
|
vi.mocked(apiClient.post).mockResolvedValueOnce({
|
|
status: 200,
|
|
data: { user: mockUser },
|
|
} as unknown as AxiosResponse);
|
|
|
|
const result = await springAuth.signUp(credentials);
|
|
|
|
expect(apiClient.post).toHaveBeenCalledWith(
|
|
"/api/v1/user/register",
|
|
{
|
|
username: credentials.email,
|
|
password: credentials.password,
|
|
},
|
|
{ withCredentials: true },
|
|
);
|
|
expect(result.user).toEqual(mockUser);
|
|
expect(result.session).toBeNull(); // No auto-login on signup
|
|
expect(result.error).toBeNull();
|
|
});
|
|
|
|
it("should return error on failed registration", async () => {
|
|
const credentials = {
|
|
email: "existing@example.com",
|
|
password: "password123",
|
|
};
|
|
|
|
const errorMessage = "User already exists";
|
|
const mockError = Object.assign(new Error(errorMessage), {
|
|
isAxiosError: true,
|
|
response: {
|
|
status: 409,
|
|
data: { message: errorMessage },
|
|
},
|
|
});
|
|
|
|
vi.mocked(apiClient.post).mockRejectedValueOnce(mockError);
|
|
|
|
const result = await springAuth.signUp(credentials);
|
|
|
|
expect(result.user).toBeNull();
|
|
expect(result.session).toBeNull();
|
|
expect(result.error).toBeTruthy();
|
|
expect(result.error?.message).toBe(errorMessage);
|
|
});
|
|
});
|
|
|
|
describe("signOut", () => {
|
|
it("should successfully sign out and clear JWT", async () => {
|
|
const mockToken = "jwt-to-clear";
|
|
localStorage.setItem("stirling_jwt", mockToken);
|
|
|
|
vi.mocked(apiClient.post).mockResolvedValueOnce({
|
|
status: 200,
|
|
data: {},
|
|
} as unknown as AxiosResponse);
|
|
|
|
const result = await springAuth.signOut();
|
|
|
|
expect(apiClient.post).toHaveBeenCalledWith(
|
|
"/api/v1/auth/logout",
|
|
null,
|
|
expect.objectContaining({ withCredentials: true }),
|
|
);
|
|
expect(localStorage.getItem("stirling_jwt")).toBeNull();
|
|
expect(result.error).toBeNull();
|
|
});
|
|
|
|
it("should clear JWT even if logout request fails", async () => {
|
|
const mockToken = "jwt-to-clear";
|
|
localStorage.setItem("stirling_jwt", mockToken);
|
|
|
|
vi.mocked(apiClient.post).mockRejectedValueOnce({
|
|
isAxiosError: true,
|
|
response: { status: 500 },
|
|
message: "Server error",
|
|
});
|
|
|
|
const result = await springAuth.signOut();
|
|
|
|
expect(localStorage.getItem("stirling_jwt")).toBeNull();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe("refreshSession", () => {
|
|
it("should refresh JWT token successfully", async () => {
|
|
const newToken = "refreshed-jwt-token";
|
|
const mockUser = {
|
|
id: "123",
|
|
email: "test@example.com",
|
|
username: "testuser",
|
|
role: "USER",
|
|
};
|
|
|
|
vi.mocked(apiClient.post).mockResolvedValueOnce({
|
|
status: 200,
|
|
data: {
|
|
user: mockUser,
|
|
session: {
|
|
access_token: newToken,
|
|
expires_in: 3600,
|
|
},
|
|
},
|
|
} as unknown as AxiosResponse);
|
|
|
|
const result = await springAuth.refreshSession();
|
|
|
|
expect(localStorage.getItem("stirling_jwt")).toBe(newToken);
|
|
// Note: refreshSession does not dispatch jwt-available event, only notifies listeners
|
|
expect(result.data.session?.access_token).toBe(newToken);
|
|
expect(result.error).toBeNull();
|
|
});
|
|
|
|
it("should clear JWT and return error on 401", async () => {
|
|
localStorage.setItem("stirling_jwt", "expired-token");
|
|
|
|
vi.mocked(apiClient.post).mockRejectedValueOnce({
|
|
isAxiosError: true,
|
|
response: { status: 401 },
|
|
message: "Token expired",
|
|
});
|
|
|
|
const result = await springAuth.refreshSession();
|
|
|
|
expect(localStorage.getItem("stirling_jwt")).toBeNull();
|
|
expect(result.data.session).toBeNull();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe("signInWithOAuth", () => {
|
|
it("should redirect to OAuth provider", async () => {
|
|
const mockAssign = vi.fn();
|
|
Object.defineProperty(window, "location", {
|
|
value: { assign: mockAssign },
|
|
writable: true,
|
|
});
|
|
|
|
vi.mocked(startOAuthNavigation).mockResolvedValueOnce(false);
|
|
|
|
const result = await springAuth.signInWithOAuth({
|
|
provider: "/oauth2/authorization/github",
|
|
options: { redirectTo: "/auth/callback" },
|
|
});
|
|
|
|
expect(startOAuthNavigation).toHaveBeenCalledWith(
|
|
"/oauth2/authorization/github",
|
|
);
|
|
expect(mockAssign).toHaveBeenCalledWith("/oauth2/authorization/github");
|
|
expect(result.error).toBeNull();
|
|
});
|
|
|
|
it("should skip redirect when handled by extension", async () => {
|
|
const mockAssign = vi.fn();
|
|
Object.defineProperty(window, "location", {
|
|
value: { assign: mockAssign },
|
|
writable: true,
|
|
});
|
|
|
|
vi.mocked(startOAuthNavigation).mockResolvedValueOnce(true);
|
|
|
|
const result = await springAuth.signInWithOAuth({
|
|
provider: "/oauth2/authorization/github",
|
|
options: { redirectTo: "/auth/callback" },
|
|
});
|
|
|
|
expect(startOAuthNavigation).toHaveBeenCalledWith(
|
|
"/oauth2/authorization/github",
|
|
);
|
|
expect(mockAssign).not.toHaveBeenCalled();
|
|
expect(result.error).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("post-login redirect path", () => {
|
|
beforeEach(() => {
|
|
sessionStorage.clear();
|
|
});
|
|
|
|
describe("isSafePostLoginRedirect", () => {
|
|
it("accepts same-origin paths with a single leading slash", () => {
|
|
expect(isSafePostLoginRedirect("/share/abc123")).toBe(true);
|
|
expect(isSafePostLoginRedirect("/workbench")).toBe(true);
|
|
expect(isSafePostLoginRedirect("/share/abc?x=1")).toBe(true);
|
|
});
|
|
|
|
it("rejects empty, null, or non-string values", () => {
|
|
expect(isSafePostLoginRedirect("")).toBe(false);
|
|
expect(isSafePostLoginRedirect(null)).toBe(false);
|
|
expect(isSafePostLoginRedirect(undefined)).toBe(false);
|
|
expect(isSafePostLoginRedirect(42 as unknown)).toBe(false);
|
|
});
|
|
|
|
it("rejects protocol-relative and absolute URLs (open-redirect guard)", () => {
|
|
expect(isSafePostLoginRedirect("//evil.example")).toBe(false);
|
|
expect(isSafePostLoginRedirect("http://evil.example")).toBe(false);
|
|
expect(isSafePostLoginRedirect("https://evil.example/x")).toBe(false);
|
|
expect(isSafePostLoginRedirect("/\\evil")).toBe(false);
|
|
});
|
|
|
|
it("rejects auth-plumbing paths to avoid login loops", () => {
|
|
expect(isSafePostLoginRedirect("/login")).toBe(false);
|
|
expect(isSafePostLoginRedirect("/login?foo=1")).toBe(false);
|
|
expect(isSafePostLoginRedirect("/auth/callback")).toBe(false);
|
|
expect(isSafePostLoginRedirect("/oauth2/authorization/google")).toBe(
|
|
false,
|
|
);
|
|
expect(isSafePostLoginRedirect("/saml2/authenticate/x")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("setPostLoginRedirectPath", () => {
|
|
it("stores a safe path in sessionStorage", () => {
|
|
setPostLoginRedirectPath("/share/abc123");
|
|
expect(sessionStorage.getItem(POST_LOGIN_REDIRECT_STORAGE_KEY)).toBe(
|
|
"/share/abc123",
|
|
);
|
|
});
|
|
|
|
it("clears any existing entry when given an unsafe value", () => {
|
|
sessionStorage.setItem(POST_LOGIN_REDIRECT_STORAGE_KEY, "/share/old");
|
|
setPostLoginRedirectPath("//evil.example");
|
|
expect(
|
|
sessionStorage.getItem(POST_LOGIN_REDIRECT_STORAGE_KEY),
|
|
).toBeNull();
|
|
});
|
|
|
|
it("clears any existing entry when given null", () => {
|
|
sessionStorage.setItem(POST_LOGIN_REDIRECT_STORAGE_KEY, "/share/old");
|
|
setPostLoginRedirectPath(null);
|
|
expect(
|
|
sessionStorage.getItem(POST_LOGIN_REDIRECT_STORAGE_KEY),
|
|
).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("consumePostLoginRedirectPath", () => {
|
|
it("returns the stored path and clears it (single-use)", () => {
|
|
sessionStorage.setItem(
|
|
POST_LOGIN_REDIRECT_STORAGE_KEY,
|
|
"/share/abc123",
|
|
);
|
|
expect(consumePostLoginRedirectPath()).toBe("/share/abc123");
|
|
expect(
|
|
sessionStorage.getItem(POST_LOGIN_REDIRECT_STORAGE_KEY),
|
|
).toBeNull();
|
|
});
|
|
|
|
it("returns null (and still clears) when the stored value is unsafe", () => {
|
|
sessionStorage.setItem(
|
|
POST_LOGIN_REDIRECT_STORAGE_KEY,
|
|
"//evil.example",
|
|
);
|
|
expect(consumePostLoginRedirectPath()).toBeNull();
|
|
expect(
|
|
sessionStorage.getItem(POST_LOGIN_REDIRECT_STORAGE_KEY),
|
|
).toBeNull();
|
|
});
|
|
|
|
it("returns null when nothing is stored", () => {
|
|
expect(consumePostLoginRedirectPath()).toBeNull();
|
|
});
|
|
});
|
|
});
|
|
});
|