fix(cloud): validate relay URL origins
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
26f36ca992
commit
7b3bc331cc
11 files changed
+144
-26
No files matched your search
@@ -31,4 +31,17 @@ describe("resolveCloudPublicConfig", () => {
|
||||
relayUrl: "https://relay.example.test",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects an insecure relay URL", () => {
|
||||
expect(
|
||||
resolveCloudPublicConfig({
|
||||
clerk: { publishableKey: "pk_test_example", jwtTemplate: "t3-relay" },
|
||||
relay: { url: "http://relay.example.test" },
|
||||
}),
|
||||
).toEqual({
|
||||
clerkPublishableKey: "pk_test_example",
|
||||
clerkJwtTemplate: "t3-relay",
|
||||
relayUrl: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import Constants from "expo-constants";
|
||||
import { relayClerkTokenOptions } from "@t3tools/shared/relayAuth";
|
||||
import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
|
||||
|
||||
type ExpoExtra = Readonly<Record<string, unknown>> | undefined;
|
||||
|
||||
@@ -22,7 +23,7 @@ export function resolveCloudPublicConfig(extra: ExpoExtra = Constants.expoConfig
|
||||
return {
|
||||
clerkPublishableKey: trimNonEmpty(clerk?.publishableKey),
|
||||
clerkJwtTemplate: trimNonEmpty(clerk?.jwtTemplate),
|
||||
relayUrl: trimNonEmpty(relay?.url)?.replace(/\/+$/u, "") ?? null,
|
||||
relayUrl: normalizeSecureRelayUrl(trimNonEmpty(relay?.url) ?? ""),
|
||||
} satisfies CloudPublicConfig;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ import {
|
||||
signRelayJwt,
|
||||
verifyRelayJwt,
|
||||
} from "@t3tools/shared/relayJwt";
|
||||
import { isSecureRelayUrl } from "@t3tools/shared/relayUrl";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Crypto from "effect/Crypto";
|
||||
import * as Duration from "effect/Duration";
|
||||
@@ -155,20 +156,6 @@ function validateCloudMintPublicKey(
|
||||
);
|
||||
}
|
||||
|
||||
function isSecureRelayUrl(value: string): boolean {
|
||||
try {
|
||||
const url = new URL(value);
|
||||
return (
|
||||
url.protocol === "https:" &&
|
||||
url.username.length === 0 &&
|
||||
url.password.length === 0 &&
|
||||
url.hash.length === 0
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function validateRelayConfigPayload(
|
||||
payload: RelayEnvironmentConfigRequest,
|
||||
): Effect.Effect<void, EnvironmentHttpBadRequestError> {
|
||||
|
||||
@@ -2127,6 +2127,11 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
relayIssuer: "http://relay.example.test",
|
||||
environmentCredential: "t3env_test_credential",
|
||||
});
|
||||
const nonOriginRelayUrl = yield* postRelayConfig({
|
||||
relayUrl: "https://relay.example.test/path",
|
||||
cloudUserId: "user_123",
|
||||
environmentCredential: "t3env_test_credential",
|
||||
});
|
||||
const emptyCredential = yield* postRelayConfig({
|
||||
relayUrl: "https://relay.example.test",
|
||||
cloudUserId: "user_123",
|
||||
@@ -2138,6 +2143,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
const insecureRelayIssuerBody = yield* responseJsonEffect<{ readonly message?: string }>(
|
||||
insecureRelayIssuer,
|
||||
);
|
||||
const nonOriginRelayUrlBody = yield* responseJsonEffect<{ readonly message?: string }>(
|
||||
nonOriginRelayUrl,
|
||||
);
|
||||
const emptyCredentialBody = yield* responseJsonEffect<{ readonly message?: string }>(
|
||||
emptyCredential,
|
||||
);
|
||||
@@ -2149,6 +2157,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
insecureRelayIssuerBody.message,
|
||||
"Relay issuer must be a secure absolute HTTPS URL.",
|
||||
);
|
||||
assert.equal(nonOriginRelayUrl.status, 400);
|
||||
assert.equal(nonOriginRelayUrlBody.message, "Relay URL must be a secure absolute HTTPS URL.");
|
||||
assert.equal(emptyCredential.status, 400);
|
||||
assert.equal(emptyCredentialBody.message, "Relay environment credential is required.");
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
|
||||
@@ -22,4 +22,12 @@ describe("hasCloudPublicConfig", () => {
|
||||
vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.test");
|
||||
expect(hasCloudPublicConfig()).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects an insecure relay URL", () => {
|
||||
vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", "pk_test_example");
|
||||
vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "t3-relay");
|
||||
vi.stubEnv("VITE_T3CODE_RELAY_URL", "http://relay.example.test");
|
||||
|
||||
expect(hasCloudPublicConfig()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { relayClerkTokenOptions } from "@t3tools/shared/relayAuth";
|
||||
import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
|
||||
|
||||
export interface CloudPublicConfig {
|
||||
readonly clerkPublishableKey: string | null;
|
||||
@@ -16,11 +17,9 @@ export function resolveCloudPublicConfig(): CloudPublicConfig {
|
||||
import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined,
|
||||
),
|
||||
clerkJwtTemplate: trimNonEmpty(import.meta.env.VITE_CLERK_JWT_TEMPLATE as string | undefined),
|
||||
relayUrl:
|
||||
trimNonEmpty(import.meta.env.VITE_T3CODE_RELAY_URL as string | undefined)?.replace(
|
||||
/\/+$/u,
|
||||
"",
|
||||
) ?? null,
|
||||
relayUrl: normalizeSecureRelayUrl(
|
||||
(import.meta.env.VITE_T3CODE_RELAY_URL as string | undefined) ?? "",
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user