Skip to content

Open source alternative to Semrush and Ahrefs

Sign Shopify site-audit crawls with a dedicated CRAWLER_ACCESS_SECRET (EVE-403) (#889)

Carter Smith1b5eaf8f40e8
diff --git a/.env.example b/.env.example
index 65a17006..114f7935 100644
--- a/.env.example
+++ b/.env.example
@@ -46,6 +46,10 @@
# GOOGLE_CLIENT_SECRET=replace-with-your-google-oauth-client-secret
# BETTER_AUTH_SECRET=replace-with-a-long-random-secret-at-least-32-characters
+# Optional: lets site audits replay a saved Shopify crawler signature. A long
+# random secret (32+ characters) that encrypts those signatures.
+# CRAWLER_ACCESS_SECRET=replace-with-a-different-long-random-secret
+
# Optional: Context.dev key for the site audit "Render JavaScript" option.
# Docker has no browser, so the option is disabled until this is set.
# CONTEXT_API_KEY=
diff --git a/deploy/.env.production.example b/deploy/.env.production.example
index 59186629..121233f4 100644
--- a/deploy/.env.production.example
+++ b/deploy/.env.production.example
@@ -8,6 +8,9 @@
AUTH_MODE=hosted
BETTER_AUTH_URL=https://app.openseo.so
# BETTER_AUTH_SECRET=
+# Encrypts saved Shopify crawler signatures; 32+ characters, distinct from
+# BETTER_AUTH_SECRET.
+# CRAWLER_ACCESS_SECRET=
# DATAFORSEO_API_KEY=
# Postgres through the existing Hyperdrive config. Cloudflare never returns
diff --git a/deploy/alchemy/alchemy.run.ts b/deploy/alchemy/alchemy.run.ts
index 819c23b8..376e67bd 100644
--- a/deploy/alchemy/alchemy.run.ts
+++ b/deploy/alchemy/alchemy.run.ts
@@ -269,6 +269,7 @@ const dataEnv = {
DATAFORSEO_API_KEY: Config.redacted("DATAFORSEO_API_KEY"),
BYPASS_EMAIL_VERIFICATION: optionalVar("BYPASS_EMAIL_VERIFICATION"),
BETTER_AUTH_SECRET: optionalSecret("BETTER_AUTH_SECRET"),
+ CRAWLER_ACCESS_SECRET: optionalSecret("CRAWLER_ACCESS_SECRET"),
GOOGLE_CLIENT_ID: optionalVar("GOOGLE_CLIENT_ID"),
GOOGLE_CLIENT_SECRET: optionalSecret("GOOGLE_CLIENT_SECRET"),
OPENROUTER_API_KEY: optionalSecret("OPENROUTER_API_KEY"),
@@ -398,8 +399,10 @@ export default Alchemy.Stack(
// Deliberately NOT ...dataEnv: this worker crawls and parses
// attacker-influenced HTML, so it gets only the secrets its code
// path reads — DataForSEO (Lighthouse), Autumn (metering), PostHog
- // (capture). No auth/OAuth/Loops/Turnstile secrets.
+ // (capture), CRAWLER_ACCESS_SECRET (Shopify signatures). No
+ // auth/OAuth/Loops/Turnstile secrets.
BROWSER: Cloudflare.Browser(),
+ CRAWLER_ACCESS_SECRET: dataEnv.CRAWLER_ACCESS_SECRET,
CONTEXT_API_KEY: dataEnv.CONTEXT_API_KEY,
DATAFORSEO_API_KEY: dataEnv.DATAFORSEO_API_KEY,
AUTUMN_SECRET_KEY: dataEnv.AUTUMN_SECRET_KEY,
diff --git a/src/env.d.ts b/src/env.d.ts
index 28b172ae..518ce1b8 100644
--- a/src/env.d.ts
+++ b/src/env.d.ts
@@ -36,6 +36,9 @@ declare namespace Cloudflare {
POSTHOG_HOST?: string;
BETTER_AUTH_SECRET?: string;
BETTER_AUTH_URL?: string;
+ // Encrypts saved Shopify crawler signatures. Kept separate from
+ // BETTER_AUTH_SECRET so the audit worker never holds the auth secret.
+ CRAWLER_ACCESS_SECRET?: string;
DATABASE_PROVIDER?: "d1" | "postgres";
HYPERDRIVE?: {
connectionString: string;
diff --git a/src/server/features/audit/services/CrawlerCredentialService.test.ts b/src/server/features/audit/services/CrawlerCredentialService.test.ts
index cd0d6e3e..f3396613 100644
--- a/src/server/features/audit/services/CrawlerCredentialService.test.ts
+++ b/src/server/features/audit/services/CrawlerCredentialService.test.ts
@@ -1,4 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { getOptionalEnvValue } from "@/server/lib/runtime-env";
const { upsertMock, listMock } = vi.hoisted(() => ({
upsertMock: vi.fn(),
@@ -122,3 +123,18 @@ describe("saveCrawlerCredential", () => {
expect(result).toMatchObject({ credential: { host: "store.example.com" } });
});
});
+
+describe("openCrawlerAccess", () => {
+ it("fails instead of crawling unsigned when the worker has no key", async () => {
+ vi.mocked(getOptionalEnvValue).mockResolvedValueOnce(undefined);
+
+ await expect(
+ CrawlerCredentialService.openCrawlerAccess({
+ host: "store.example.com",
+ signatureInput: "sealed",
+ signature: "sealed",
+ expiresAt: null,
+ }),
+ ).rejects.toMatchObject({ code: "AUTH_CONFIG_MISSING" });
+ });
+});
diff --git a/src/server/features/audit/services/CrawlerCredentialService.ts b/src/server/features/audit/services/CrawlerCredentialService.ts
index 490fdebd..4b8cde20 100644
--- a/src/server/features/audit/services/CrawlerCredentialService.ts
+++ b/src/server/features/audit/services/CrawlerCredentialService.ts
@@ -26,14 +26,14 @@ export interface SealedCrawlerAccess {
expiresAt: string | null;
}
-// Same key as the stored Google OAuth tokens, so self-hosters have one secret
-// to set and hosted mode always has it.
+// A key used only for these signatures. The audit worker that replays them
+// crawls untrusted sites, so it must not hold BETTER_AUTH_SECRET.
async function getEncryptionKey(): Promise<string> {
- const secret = (await getOptionalEnvValue("BETTER_AUTH_SECRET"))?.trim();
+ const secret = (await getOptionalEnvValue("CRAWLER_ACCESS_SECRET"))?.trim();
if (!secret || secret.length < MIN_BETTER_AUTH_SECRET_LENGTH) {
throw new AppError(
"AUTH_CONFIG_MISSING",
- `Set BETTER_AUTH_SECRET to at least ${MIN_BETTER_AUTH_SECRET_LENGTH} characters to store crawler access signatures. It encrypts them.`,
+ `Set CRAWLER_ACCESS_SECRET to at least ${MIN_BETTER_AUTH_SECRET_LENGTH} characters to store crawler access signatures. It encrypts them.`,
);
}
return secret;
@@ -159,8 +159,10 @@ async function openCrawlerAccess(
sealed: SealedCrawlerAccess | null | undefined,
): Promise<CrawlerAccess | null> {
if (!sealed) return null;
+ // Outside the try: a worker deployed without the key is a config bug that
+ // must fail the audit loudly, not crawl unsigned as if nothing were saved.
+ const key = await getEncryptionKey();
try {
- const key = await getEncryptionKey();
return {
host: sealed.host,
expiresAt: sealed.expiresAt,
@@ -170,7 +172,7 @@ async function openCrawlerAccess(
),
};
} catch {
- // A rotated BETTER_AUTH_SECRET makes stored signatures unreadable. Crawl
+ // A rotated CRAWLER_ACCESS_SECRET makes stored signatures unreadable. Crawl
// unsigned rather than fail the audit; the report asks for a new one.
console.warn(`Could not decrypt crawler access for ${sealed.host}`);
return null;