Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix(notion): send a User-Agent so Cloudflare stops 403ing page fetches

Marc Seitzee076d7a415b
diff --git a/lib/api/documents/validate-external-url.ts b/lib/api/documents/validate-external-url.ts
index 8198e2f4..5b7347a9 100644
--- a/lib/api/documents/validate-external-url.ts
+++ b/lib/api/documents/validate-external-url.ts
@@ -7,6 +7,21 @@ import notion from "@/lib/notion";
import { getNotionPageIdFromSlug } from "@/lib/notion/utils";
import { log } from "@/lib/utils";
+// Query strings and fragments can carry tokens or PII, so log origin + path
+// only — the same rule the blocked-keyword alert below follows.
+function describeUrlForLog(value: string): string {
+ try {
+ const parsed = new URL(value);
+ return `${parsed.origin}${parsed.pathname}`;
+ } catch {
+ return value;
+ }
+}
+
+function causeForLog(error: unknown): string {
+ return error instanceof Error ? error.message : String(error);
+}
+
/**
* Validates the external URL backing a `notion` or `link` document: Notion
* pages must be publicly accessible, and link URLs must be well-formed and not
@@ -44,6 +59,11 @@ export async function validateExternalDocumentUrl({
}
await notion.getPage(pageId);
} catch (error) {
+ console.error("[validateExternalDocumentUrl] Notion page check failed", {
+ teamId,
+ url: describeUrlForLog(key),
+ cause: causeForLog(error),
+ });
throw new PapermarkApiError(
"unprocessable_entity",
"This Notion page isn't publically available.",
@@ -81,6 +101,11 @@ export async function validateExternalDocumentUrl({
}
}
} catch (error) {
+ console.error("[validateExternalDocumentUrl] Link URL check failed", {
+ teamId,
+ url: describeUrlForLog(key),
+ cause: causeForLog(error),
+ });
throw new PapermarkApiError(
"unprocessable_entity",
"Invalid URL format for link document.",
diff --git a/lib/notion/index.ts b/lib/notion/index.ts
index 077bcdfe..b207bae3 100644
--- a/lib/notion/index.ts
+++ b/lib/notion/index.ts
@@ -1,4 +1,15 @@
import { NotionAPI } from "notion-client";
-const notion = new NotionAPI();
+// Cloudflare 403s requests to www.notion.so that carry no User-Agent, and
+// neither undici nor notion-client sets one. It has to look like a browser.
+// https://github.com/NotionX/react-notion-x/issues/710
+const notion = new NotionAPI({
+ ofetchOptions: {
+ headers: {
+ "User-Agent":
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
+ },
+ },
+});
+
export default notion;