Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: only bypass the self-hosted redirect for real files under public/

Shae Feltzc0b8a60ac2b8
diff --git a/apps/web/__tests__/unit/proxy-self-hosted.test.ts b/apps/web/__tests__/unit/proxy-self-hosted.test.ts
index e1d3019fe..f20abc7d9 100644
--- a/apps/web/__tests__/unit/proxy-self-hosted.test.ts
+++ b/apps/web/__tests__/unit/proxy-self-hosted.test.ts
@@ -25,6 +25,20 @@ vi.mock("@cap/env", () => ({
const request = (path: string) =>
proxy(new NextRequest(`https://cap.example.com${path}`));
+const expectServed = async (path: string) => {
+ const response = await request(path);
+ expect(response.status).toBe(200);
+ expect(response.headers.get("location")).toBeNull();
+};
+
+const expectLoginRedirect = async (path: string) => {
+ const response = await request(path);
+ expect(response.status).toBe(307);
+ expect(response.headers.get("location")).toBe(
+ "https://cap.example.com/login",
+ );
+};
+
describe("self-hosted proxy routes", () => {
it("allows browser-based CLI authorization pages", () => {
const source = readFileSync(join(process.cwd(), "proxy.ts"), "utf8");
@@ -33,30 +47,29 @@ describe("self-hosted proxy routes", () => {
it.each([
"/logos/browsers/google-chrome.svg",
- "/illustrations/mask-bg.webp",
- "/sounds/recording-start.mp3",
+ "/illustrations/app.webp",
+ "/sounds/start-recording.ogg",
"/rive/main.riv",
- "/fonts/Inter.woff2",
- ])("serves the public asset %s instead of redirecting", async (path) => {
- const response = await request(path);
+ "/fonts/Geist-Regular.woff2",
+ "/site.webmanifest",
+ ])("serves the public asset %s instead of redirecting", (path) =>
+ expectServed(path),
+ );
- expect(response.status).toBe(200);
- expect(response.headers.get("location")).toBeNull();
- });
+ it("still redirects page routes to /login", () =>
+ expectLoginRedirect("/pricing"));
- it("still redirects unauthenticated page routes to /login", async () => {
- const response = await request("/pricing");
+ it("still redirects extension-suffixed route handlers to /login", () =>
+ expectLoginRedirect("/install-cli.sh"));
- expect(response.status).toBe(307);
- expect(response.headers.get("location")).toBe(
- "https://cap.example.com/login",
- );
- });
+ it("does not let a missing file through", () =>
+ expectLoginRedirect("/logos/missing.svg"));
- it("does not treat a share link as a static asset", async () => {
- const response = await request("/s/video123");
+ it("does not let a directory through", () => expectLoginRedirect("/logos"));
- expect(response.status).toBe(200);
- expect(response.headers.get("location")).toBeNull();
- });
+ it("rejects path traversal out of public/", () =>
+ expectLoginRedirect("/logos/..%2F..%2Fproxy.ts"));
+
+ it("does not treat a share link as an asset", () =>
+ expectServed("/s/video123"));
});
diff --git a/apps/web/proxy.ts b/apps/web/proxy.ts
index 85d40460e..be53d84c3 100644
--- a/apps/web/proxy.ts
+++ b/apps/web/proxy.ts
@@ -1,3 +1,5 @@
+import { statSync } from "node:fs";
+import { resolve, sep } from "node:path";
import { db } from "@cap/database";
import { organizations } from "@cap/database/schema";
import { buildEnv, serverEnv } from "@cap/env";
@@ -11,6 +13,20 @@ const addHttps = (s?: string) => {
return `https://${s}`;
};
+const publicDir = resolve(process.cwd(), "public");
+
+const isPublicAsset = (path: string) => {
+ let decoded: string;
+ try {
+ decoded = decodeURIComponent(path);
+ } catch {
+ return false;
+ }
+ const file = resolve(publicDir, `.${decoded}`);
+ if (!file.startsWith(`${publicDir}${sep}`)) return false;
+ return statSync(file, { throwIfNoEntry: false })?.isFile() ?? false;
+};
+
const mainOrigins = [
"https://cap.so",
"https://cap.link",
@@ -56,10 +72,9 @@ export async function proxy(request: NextRequest) {
if (buildEnv.NEXT_PUBLIC_IS_CAP !== "true") {
// Files under public/ have no route of their own, so without this every
// <img src="/logos/..."> on a self-hosted instance redirects to /login.
- const isStaticAsset = /\.[a-z0-9]+$/i.test(path);
if (
!(
- isStaticAsset ||
+ isPublicAsset(path) ||
path.startsWith("/s/") ||
path.startsWith("/c/") ||
path.startsWith("/cli/") ||