: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(csp): serve the country flag font ourselves (#99625)
Tom Piccirellob345278f7120
diff --git a/.semgrep/rules/security/country-flag-polyfill-via-wrapper.test.ts b/.semgrep/rules/security/country-flag-polyfill-via-wrapper.test.tsnew file mode 100644index 000000000000..081592452acb--- /dev/null+++ b/.semgrep/rules/security/country-flag-polyfill-via-wrapper.test.ts@@ -0,0 +1,32 @@+// @ts-nocheck+// Test fixture for the country-flag-polyfill-via-wrapper rule.++// ruleid: country-flag-polyfill-via-wrapper+import { polyfillCountryFlagEmojis } from 'country-flag-emoji-polyfill'++// ruleid: country-flag-polyfill-via-wrapper+import polyfill from 'country-flag-emoji-polyfill'++// ruleid: country-flag-polyfill-via-wrapper+import * as flags from 'country-flag-emoji-polyfill'++// ruleid: country-flag-polyfill-via-wrapper+import 'country-flag-emoji-polyfill'++// A direct import stays a finding even when the call passes a URL, because the next call+// site added next to it is the one that forgets.+// ruleid: country-flag-polyfill-via-wrapper+const dynamic = import('country-flag-emoji-polyfill')++// ruleid: country-flag-polyfill-via-wrapper+const required = require('country-flag-emoji-polyfill')++// ok: country-flag-polyfill-via-wrapper+import { polyfillCountryFlags } from 'lib/countryFlagEmojiPolyfill'++// ok: country-flag-polyfill-via-wrapper+const wrapped = import('lib/countryFlagEmojiPolyfill')++// A package whose name merely contains the same words is not this one.+// ok: country-flag-polyfill-via-wrapper+import { other } from 'country-flag-emoji-polyfill-helpers'diff --git a/.semgrep/rules/security/country-flag-polyfill-via-wrapper.yaml b/.semgrep/rules/security/country-flag-polyfill-via-wrapper.yamlnew file mode 100644index 000000000000..db7869ce96bb--- /dev/null+++ b/.semgrep/rules/security/country-flag-polyfill-via-wrapper.yaml@@ -0,0 +1,48 @@+rules:+ - id: country-flag-polyfill-via-wrapper+ message: |+ `country-flag-emoji-polyfill` imported directly. Import+ `lib/countryFlagEmojiPolyfill` and call `polyfillCountryFlags()` instead.++ The package's second argument is the font URL, and it defaults to+ `https://cdn.jsdelivr.net/npm/[email protected]/dist/TwemojiCountryFlags.woff2`.+ A call site that passes only the family name silently takes that default and+ loads a font from an open CDN. The URL lives inside the dependency, not in our+ source, so searching the repository for the CDN does not find those call sites.++ That matters because the app's `font-src` does not list jsdelivr, and should not.+ An open CDN there serves bytes anyone can publish at a path an attacker picks,+ which is both a wider surface for the browser's font parser and exactly the+ remote origin a CSS-injection exfiltration needs to reach.++ The wrapper holds the family name and the same-origin URL in one place, so the+ policy needs no CDN entry. It also keeps the family name matched to the+ `font-family` our CSS declares for flag glyphs.++ If you genuinely need the package directly, put+ `// nosemgrep: country-flag-polyfill-via-wrapper` on the line before the import+ with a reason, and pass an explicit same-origin URL as the second argument.+ severity: ERROR+ languages: [typescript, javascript]+ paths:+ # The leading slash pins these to the repository root, which is where the app+ # and the wrapper live. Without it semgrep warns that it will anchor them anyway.+ include:+ - /frontend/src+ - /products+ - /common+ exclude:+ # The wrapper is the one place that may import the package.+ - /frontend/src/lib/countryFlagEmojiPolyfill.ts+ pattern-either:+ - pattern: import { ... } from 'country-flag-emoji-polyfill'+ - pattern: import $X from 'country-flag-emoji-polyfill'+ - pattern: import * as $X from 'country-flag-emoji-polyfill'+ - pattern: import 'country-flag-emoji-polyfill'+ - pattern: import('country-flag-emoji-polyfill')+ - pattern: require('country-flag-emoji-polyfill')+ metadata:+ category: security+ confidence: HIGH+ references:+ - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/font-srcdiff --git a/frontend/public/fonts/TwemojiCountryFlags.woff2 b/frontend/public/fonts/TwemojiCountryFlags.woff2new file mode 100644index 000000000000..b9d6ea84b392Binary files /dev/null and b/frontend/public/fonts/TwemojiCountryFlags.woff2 differdiff --git a/frontend/src/exporter/index.tsx b/frontend/src/exporter/index.tsxindex 5d1eee87d7c2..c4363d48ee4f 100644--- a/frontend/src/exporter/index.tsx+++ b/frontend/src/exporter/index.tsx@@ -5,10 +5,11 @@ import './Exporter.scss'// before any module that builds a zod schema. See lib/configureZod.import '../lib/configureZod'-import { polyfillCountryFlagEmojis } from 'country-flag-emoji-polyfill'import { BeforeSendFn, CapturedNetworkRequest } from 'posthog-js'import { createRoot } from 'react-dom/client'+import { polyfillCountryFlags } from 'lib/countryFlagEmojiPolyfill'+import { Exporter } from '~/exporter/Exporter'import { ExportType, ExportedData } from '~/exporter/types'import { initKea } from '~/initKea'@@ -69,13 +70,10 @@ initKea({ replaceInitialPathInWindow: false })// On Chrome + Windows, the country flag emojis don't render correctly. This is a polyfill for that.// It won't be applied on other platforms.//-// NOTE: The first argument is the name of the polyfill to use. This is used to set the font family in our CSS.-// Make sure to update the font family in the CSS if you change this.-//// The polyfill runs canvas-based feature detection (getImageData) which can throw on some browser// states (e.g. Safari/macOS). It's purely cosmetic and best-effort, so swallow any failure.try {- polyfillCountryFlagEmojis('Emoji Flags Polyfill')+ polyfillCountryFlags()} catch (error) {console.warn('[exporter] Country flag emoji polyfill detection failed:', error)}diff --git a/frontend/src/lib/countryFlagEmojiPolyfill.ts b/frontend/src/lib/countryFlagEmojiPolyfill.tsnew file mode 100644index 000000000000..2fe7b3c0bd67--- /dev/null+++ b/frontend/src/lib/countryFlagEmojiPolyfill.ts@@ -0,0 +1,14 @@+import { polyfillCountryFlagEmojis } from 'country-flag-emoji-polyfill'++// The package defaults its font URL to cdn.jsdelivr.net. We serve the file ourselves so that an open+// CDN stays out of the app's `font-src`: there it is both a wider font-parser surface and exactly the+// remote origin a CSS-injection exfiltration needs. Call this instead of the package, so a new call+// site cannot reintroduce the default.+//+// The family name has to match the `font-family` our CSS declares for flag glyphs.+const FONT_FAMILY = 'Emoji Flags Polyfill'+const FONT_URL = '/static/fonts/TwemojiCountryFlags.woff2'++export function polyfillCountryFlags(): boolean {+ return polyfillCountryFlagEmojis(FONT_FAMILY, FONT_URL)+}diff --git a/frontend/src/scenes/bootApp.ts b/frontend/src/scenes/bootApp.tsindex 4153ac94e7c4..cc70be36ca4c 100644--- a/frontend/src/scenes/bootApp.ts+++ b/frontend/src/scenes/bootApp.ts@@ -43,11 +43,10 @@ export function bootApp(): void {})// On Chrome + Windows, the country flag emojis don't render correctly. This polyfill fixes that.- // NOTE: The first argument sets the polyfill's font family name, which our CSS references —- // keep the two in sync. Detection is canvas-based and can throw on some browser states- // (e.g. Safari/macOS); it's purely cosmetic and best-effort.- void import('country-flag-emoji-polyfill')- .then(({ polyfillCountryFlagEmojis }) => polyfillCountryFlagEmojis('Emoji Flags Polyfill'))+ // The import stays dynamic so the package loads after boot. Detection is canvas-based and can+ // throw on some browser states (e.g. Safari/macOS); it's purely cosmetic and best-effort.+ void import('lib/countryFlagEmojiPolyfill')+ .then(({ polyfillCountryFlags }) => polyfillCountryFlags()).catch((error) => {console.warn('[App] Country flag emoji polyfill failed:', error)})