:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): keep fonts in copied images under a strict base-uri (#100650)
Tom Piccirello6b6dec9f9e58
diff --git a/frontend/src/lib/utils/htmlToImagePatch.test.ts b/frontend/src/lib/utils/htmlToImagePatch.test.tsnew file mode 100644index 000000000000..3a0d2d73f98a--- /dev/null+++ b/frontend/src/lib/utils/htmlToImagePatch.test.ts@@ -0,0 +1,53 @@+import { readFileSync } from 'fs'+import { resolveUrl } from 'html-to-image/lib/util'+import { dirname, join } from 'path'++// html-to-image resolves a relative `url()` inside @font-face against the stylesheet's own href.+// Upstream does that by building a detached document, putting a <base href> in it and reading back+// a resolved anchor. Chromium judges that assignment against `base-uri`, so under our policy the+// base is refused, the URL resolves against about:blank and the fetch returns whatever that lands+// on. The library base64s the result and embeds it as a font, so a copied insight image loses its+// typeface with no error anywhere. patches/[email protected] swaps the trick for `new URL`.+//+// The two halves below cover different regressions, because no single assertion covers both.+//+// Resolution is checked by calling the function, so a patch that resolves incorrectly fails here.+// It cannot also catch the patch being dropped: without a CSP header both implementations return+// the same string, which is the point of the change, and jsdom has no CSP. Confirmed by running+// both against the same input under jsdom — identical output.+//+// So the patch being dropped is caught by asserting the <base href> assignment is absent. That is+// the construct the policy rejects. pnpm fails loudly when a patch cannot apply, but not when+// someone removes the entry to unblock an upgrade, which is the path this guards. The check is+// deliberately negative: it says nothing about how the replacement resolves, so an equivalent+// refactor survives.+//+// Both builds this repo loads are guarded, because they are reached differently: esbuild takes+// `module` (es/) and Jest and Node take `main` (lib/). Patching only one of them looks fine locally+// and ships unpatched code to the browser. The package also ships dist/, which only `unpkg` serves,+// so nothing in this repo loads it.+describe('html-to-image patch', () => {+ const packageRoot = dirname(dirname(require.resolve('html-to-image')))+ const STYLESHEET = 'https://app-static-prod.posthog.com/static/index-46THL72U.css'++ // Expectations are written out rather than computed, so the assertion does not restate the+ // implementation it is checking.+ it.each([+ [+ 'root-relative, the shape our own index.css ships',+ '/static/assets/Inter-YI3GW4KE.woff2',+ 'https://app-static-prod.posthog.com/static/assets/Inter-YI3GW4KE.woff2',+ ],+ ['path-relative', 'Inter.woff2', 'https://app-static-prod.posthog.com/static/Inter.woff2'],+ ['parent segment', '../assets/Inter.woff2', 'https://app-static-prod.posthog.com/assets/Inter.woff2'],+ ['absolute, returned untouched', 'https://cdn.example.com/Inter.woff2', 'https://cdn.example.com/Inter.woff2'],+ ])('resolves a %s font URL against the stylesheet', (_label, url, expected) => {+ expect(resolveUrl(url, STYLESHEET)).toBe(expected)+ })++ it.each(['es/util.js', 'lib/util.js'])('does not assign <base href> on a detached document in %s', (file) => {+ const source = readFileSync(join(packageRoot, file), 'utf-8')++ expect(source).not.toMatch(/createElement\(['"]base['"]\)/)+ })+})diff --git a/patches/[email protected] b/patches/[email protected]new file mode 100644index 000000000000..357cf82aae12--- /dev/null+++ b/patches/[email protected]@@ -0,0 +1,62 @@+diff --git a/es/util.js b/es/util.js+index 90a3cd19a6bcc763d98cbd476fde17f4bc0428fa..4c1f1b94da109566bb184e420ed158fdf74fc62d 100644+--- a/es/util.js++++ b/es/util.js+@@ -11,16 +11,17 @@ export function resolveUrl(url, baseUrl) {+ if (url.match(/^[a-z]+:/i)) {+ return url;+ }+- const doc = document.implementation.createHTMLDocument();+- const base = doc.createElement('base');+- const a = doc.createElement('a');+- doc.head.appendChild(base);+- doc.body.appendChild(a);+- if (baseUrl) {+- base.href = baseUrl;++ // Assigning <base href> is judged against `base-uri` even on a detached document, so under a++ // strict policy Chromium refuses it and the URL resolves against about:blank. The URL++ // constructor does the same resolution with no element and no policy surface. The only++ // caller guards on a truthy baseUrl, and a base the parser rejects returns the input++ // unchanged, which is what the anchor did too.++ try {++ return new URL(url, baseUrl).href;++ }++ catch (_e) {++ return url;+ }+- a.href = url;+- return a.href;+ }+ export const uuid = (() => {+ // generate uuid for className of pseudo elements.+diff --git a/lib/util.js b/lib/util.js+index 6a6ae9c8bd15b276d46773d853c2182aaba8326e..8e6a8aaf6e97a51c05f99aa698deb262a875f8f5 100644+--- a/lib/util.js++++ b/lib/util.js+@@ -50,16 +50,17 @@ function resolveUrl(url, baseUrl) {+ if (url.match(/^[a-z]+:/i)) {+ return url;+ }+- var doc = document.implementation.createHTMLDocument();+- var base = doc.createElement('base');+- var a = doc.createElement('a');+- doc.head.appendChild(base);+- doc.body.appendChild(a);+- if (baseUrl) {+- base.href = baseUrl;++ // Assigning <base href> is judged against `base-uri` even on a detached document, so under a++ // strict policy Chromium refuses it and the URL resolves against about:blank. The URL++ // constructor does the same resolution with no element and no policy surface. The only++ // caller guards on a truthy baseUrl, and a base the parser rejects returns the input++ // unchanged, which is what the anchor did too.++ try {++ return new URL(url, baseUrl).href;++ }++ catch (_e) {++ return url;+ }+- a.href = url;+- return a.href;+ }+ exports.resolveUrl = resolveUrl;+ exports.uuid = (function () {diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 2859deccbf87..47c153a6c026 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -198,6 +198,9 @@ patchedDependencies:hash: d95e6638894935a75bd93b60b5243cb825892a1eedcc4c9a125a3e7dce8b64f0path: patches/[email protected]+ hash: c51d4e8d9f649ee8a8e61577347ca1e4a9da029e91a073a91aae08ebf783d5d2+ path: patches/[email protected]hash: 139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5path: patches/[email protected]@@ -1069,7 +1072,7 @@ importers:version: 1.5.15html-to-image:specifier: ^1.11.13- version: 1.11.13+ version: 1.11.13(patch_hash=c51d4e8d9f649ee8a8e61577347ca1e4a9da029e91a073a91aae08ebf783d5d2)husky:specifier: 'catalog:'version: 7.0.4@@ -37784,7 +37787,7 @@ snapshots:- [email protected]: {}+ [email protected](patch_hash=c51d4e8d9f649ee8a8e61577347ca1e4a9da029e91a073a91aae08ebf783d5d2): {}diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex ac15a9672307..0fcefe98fe84 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -105,6 +105,7 @@ patchedDependencies:'@connectrpc/[email protected]': patches/@[email protected][email protected]: patches/[email protected][email protected]: patches/[email protected]+ [email protected]: patches/[email protected][email protected]: patches/[email protected]# Dependencies that require preinstall scripts. Add entries with caution.