Skip to content

: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.

fix(exports): fetch png export bytes directly under the csp (#104011)

Tom Piccirello719f83fb3cbe
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 74e7ce90e172..ca70853aa511 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -3048,6 +3048,16 @@ const api = {
.assembleFullUrl(true)
},
+ // For fetch() callers. The download URL redirects to object storage, and connect-src does not
+ // allow that origin, so the fetch fails. direct=true serves the bytes from our origin (PNG only).
+ determineExportFetchUrl(exportId: number, teamId: TeamType['id'] = ApiConfig.getCurrentTeamId()): string {
+ return new ApiRequest()
+ .export(exportId, teamId)
+ .withAction('content')
+ .withQueryString('direct=true')
+ .assembleFullUrl(true)
+ },
+
async create(
data: Partial<ExportedAssetType>,
params: Record<string, any> = {},
diff --git a/frontend/src/lib/components/ExportButton/exporter.test.ts b/frontend/src/lib/components/ExportButton/exporter.test.ts
index 827d43348338..2662c3097d8a 100644
--- a/frontend/src/lib/components/ExportButton/exporter.test.ts
+++ b/frontend/src/lib/components/ExportButton/exporter.test.ts
@@ -1,25 +1,30 @@
+import { ApiConfig } from 'lib/api'
+
import { ExportedAssetType } from '~/types'
-import { downloadExportedAsset } from './exporter'
+import { downloadExportedAsset, exportedAssetBlob } from './exporter'
const getResponse = jest.fn()
-jest.mock('lib/api', () => ({
- __esModule: true,
- default: {
- getResponse: (...args: any[]) => getResponse(...args),
- exports: {
- determineExportUrl: jest.fn((id: number) => `/api/environments/1/exports/${id}/content?download=true`),
+jest.mock('lib/api', () => {
+ const actual = jest.requireActual('lib/api')
+ return {
+ ...actual,
+ __esModule: true,
+ default: {
+ ...actual.default,
+ getResponse: (...args: any[]) => getResponse(...args),
},
- },
-}))
+ }
+})
-describe('downloadExportedAsset', () => {
+describe('exporter', () => {
let fakeAnchor: HTMLAnchorElement
let appendSpy: jest.SpyInstance
let removeSpy: jest.SpyInstance
beforeEach(() => {
+ ApiConfig.setCurrentTeamId(1)
jest.useFakeTimers()
fakeAnchor = { style: {}, click: jest.fn() } as unknown as HTMLAnchorElement
jest.spyOn(document, 'createElement').mockReturnValue(fakeAnchor)
@@ -49,4 +54,12 @@ describe('downloadExportedAsset', () => {
jest.runOnlyPendingTimers()
expect(removeSpy).toHaveBeenCalledWith(fakeAnchor)
})
+
+ it('fetches the bytes for the editor without following the object storage redirect', async () => {
+ getResponse.mockResolvedValue({ blob: async () => new Blob(['png']) })
+
+ await exportedAssetBlob({ id: 123 } as ExportedAssetType)
+
+ expect(getResponse).toHaveBeenCalledWith('/api/environments/1/exports/123/content?direct=true')
+ })
})
diff --git a/frontend/src/lib/components/ExportButton/exporter.tsx b/frontend/src/lib/components/ExportButton/exporter.tsx
index 19f3184f2a9f..fe8e6894f57c 100644
--- a/frontend/src/lib/components/ExportButton/exporter.tsx
+++ b/frontend/src/lib/components/ExportButton/exporter.tsx
@@ -19,8 +19,8 @@ export function downloadBlob(content: Blob, filename: string): void {
}
export async function exportedAssetBlob(asset: ExportedAssetType): Promise<Blob> {
- const downloadUrl = api.exports.determineExportUrl(asset.id)
- const response = await api.getResponse(downloadUrl)
+ const contentUrl = api.exports.determineExportFetchUrl(asset.id)
+ const response = await api.getResponse(contentUrl)
return await response.blob()
}
diff --git a/products/exports/backend/tests/test_exported_asset_model.py b/products/exports/backend/tests/test_exported_asset_model.py
index ab80a36a60a2..f41acfd0e6e4 100644
--- a/products/exports/backend/tests/test_exported_asset_model.py
+++ b/products/exports/backend/tests/test_exported_asset_model.py
@@ -333,12 +333,17 @@ class TestDirectContentResponse(APIBaseTest):
asset = ExportedAsset.objects.create(
team=self.team,
export_format=export_format,
- content=b"bytes" if expected_status == 200 else None,
- content_location="exports/some/key" if expected_status == 302 else None,
+ content_location="exports/some/key",
)
- with patch(
- "products.exports.backend.models.exported_asset.object_storage.get_presigned_url",
- return_value="https://storage.example.com/presigned",
+ with (
+ patch(
+ "products.exports.backend.models.exported_asset.object_storage.get_presigned_url",
+ return_value="https://storage.example.com/presigned",
+ ),
+ patch(
+ "products.exports.backend.models.exported_asset.object_storage.read_bytes",
+ return_value=b"bytes",
+ ),
):
response = get_content_response(asset, direct=True)
assert response.status_code == expected_status
diff --git a/products/subscriptions/frontend/components/Subscriptions/subscriptionLogic.ts b/products/subscriptions/frontend/components/Subscriptions/subscriptionLogic.ts
index 4edbf585abb4..cc2cd9d85843 100644
--- a/products/subscriptions/frontend/components/Subscriptions/subscriptionLogic.ts
+++ b/products/subscriptions/frontend/components/Subscriptions/subscriptionLogic.ts
@@ -1169,7 +1169,7 @@ async function fetchPreviewImage(
asset: ExportedAssetType,
actions: { setPreviewImageUrl: (url: string | null) => void; setPreviewError: (error: string | null) => void }
): Promise<void> {
- const url = api.exports.determineExportUrl(asset.id)
+ const url = api.exports.determineExportFetchUrl(asset.id)
const response = await fetch(url, { credentials: 'include' })
if (!response.ok) {
actions.setPreviewError('Failed to load preview image')