Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: restrict mobile auth redirects

Richie McIlroy24ba28eb7eeb
diff --git a/apps/web/__tests__/unit/mobile-api-contract.test.ts b/apps/web/__tests__/unit/mobile-api-contract.test.ts
index c336c187c..58c5b4584 100644
--- a/apps/web/__tests__/unit/mobile-api-contract.test.ts
+++ b/apps/web/__tests__/unit/mobile-api-contract.test.ts
@@ -62,6 +62,35 @@ describe("mobile API contract schemas", () => {
).toBe("org_123");
});
+ it("allows only mobile auth callback redirects for session requests", () => {
+ const decodeRedirect = (redirectUri: string) =>
+ Schema.decodeUnknownSync(Mobile.MobileSessionRequestParams)({
+ redirectUri,
+ provider: "google",
+ });
+
+ expect(decodeRedirect("cap://auth").redirectUri).toBe("cap://auth");
+ expect(
+ decodeRedirect("exp+cap://expo-development-client/--/auth").redirectUri,
+ ).toBe("exp+cap://expo-development-client/--/auth");
+ expect(
+ decodeRedirect(
+ "exp+cap://expo-development-client/?url=http%3A%2F%2F127.0.0.1%3A8081%2F--%2Fauth",
+ ).redirectUri,
+ ).toBe(
+ "exp+cap://expo-development-client/?url=http%3A%2F%2F127.0.0.1%3A8081%2F--%2Fauth",
+ );
+
+ expect(() => decodeRedirect("cap://auth/")).toThrow();
+ expect(() => decodeRedirect("cap://auth?next=cap://settings")).toThrow();
+ expect(() => decodeRedirect("cap://settings")).toThrow();
+ expect(() => decodeRedirect("cap://auth.evil")).toThrow();
+ expect(() => decodeRedirect("https://cap.so/auth")).toThrow();
+ expect(() =>
+ decodeRedirect("exp+cap://expo-development-client/--/settings"),
+ ).toThrow();
+ });
+
it("decodes Cap sharing visibility updates", () => {
const decoded = Schema.decodeUnknownSync(Mobile.MobileCapSharingInput)({
public: false,
diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts
index 1d86f11f8..394a315b2 100644
--- a/apps/web/app/api/mobile/[...route]/route.ts
+++ b/apps/web/app/api/mobile/[...route]/route.ts
@@ -76,7 +76,6 @@ type MobileUploadCreateInput = (typeof Mobile.MobileUploadCreateInput)["Type"];
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const emailCodePattern = /^\d{6}$/;
const emailCodeTtlMs = 10 * 60 * 1000;
-const mobileRedirectProtocols = new Set(["cap:", "exp+cap:"]);
const toIsoString = (value: Date) => value.toISOString();
@@ -122,7 +121,7 @@ const sendMobileEmailCode = async (email: string, code: string) => {
const getMobileRedirectUrl = (redirectUri: string) => {
try {
const redirectUrl = new URL(redirectUri);
- if (!mobileRedirectProtocols.has(redirectUrl.protocol)) return null;
+ if (!Mobile.isMobileAuthRedirectUri(redirectUri)) return null;
return redirectUrl;
} catch {
return null;
diff --git a/packages/web-domain/src/Mobile.ts b/packages/web-domain/src/Mobile.ts
index 3dfafc652..04a27e910 100644
--- a/packages/web-domain/src/Mobile.ts
+++ b/packages/web-domain/src/Mobile.ts
@@ -14,6 +14,56 @@ import { UploadTarget } from "./Storage.ts";
import { UserId } from "./User.ts";
import { UploadPhase, VideoId } from "./Video.ts";
+const isExpoAuthPath = (pathname: string) => pathname === "/--/auth";
+
+export const isMobileAuthRedirectUri = (redirectUri: string) => {
+ try {
+ const redirectUrl = new URL(redirectUri);
+ if (redirectUrl.protocol === "cap:") {
+ return (
+ redirectUrl.hostname === "auth" &&
+ redirectUrl.pathname === "" &&
+ redirectUrl.search === "" &&
+ redirectUrl.hash === ""
+ );
+ }
+
+ if (
+ redirectUrl.protocol !== "exp+cap:" ||
+ redirectUrl.hostname !== "expo-development-client" ||
+ redirectUrl.hash !== ""
+ ) {
+ return false;
+ }
+
+ if (isExpoAuthPath(redirectUrl.pathname)) return redirectUrl.search === "";
+
+ const embeddedUrl = redirectUrl.searchParams.get("url");
+ if (
+ !embeddedUrl ||
+ Array.from(redirectUrl.searchParams.keys()).length !== 1
+ )
+ return false;
+
+ const parsedEmbeddedUrl = new URL(embeddedUrl);
+ return (
+ isExpoAuthPath(parsedEmbeddedUrl.pathname) &&
+ parsedEmbeddedUrl.search === "" &&
+ parsedEmbeddedUrl.hash === ""
+ );
+ } catch {
+ return false;
+ }
+};
+
+const MobileAuthRedirectUri = Schema.String.pipe(
+ Schema.filter(
+ (redirectUri) =>
+ isMobileAuthRedirectUri(redirectUri) ||
+ "Invalid mobile auth redirect URI",
+ ),
+);
+
export const MobileApiKeyResponse = Schema.Struct({
type: Schema.Literal("api_key"),
apiKey: Schema.String,
@@ -30,7 +80,7 @@ export const MobileAuthConfigResponse = Schema.Struct({
});
export const MobileSessionRequestParams = Schema.Struct({
- redirectUri: Schema.optional(Schema.String),
+ redirectUri: Schema.optional(MobileAuthRedirectUri),
provider: Schema.optional(Schema.Literal("google", "workos")),
organizationId: Schema.optional(Schema.String),
});