The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Verify front component checksums before execution (#26454)
Raphaël Bosi4f78aab03f3d
diff --git a/packages/twenty-front-component-renderer/src/host/component-source/utils/__tests__/fetchComponentSource.spec.ts b/packages/twenty-front-component-renderer/src/host/component-source/utils/__tests__/fetchComponentSource.spec.tsindex acb67366d5c..5e4af051c1c 100644--- a/packages/twenty-front-component-renderer/src/host/component-source/utils/__tests__/fetchComponentSource.spec.ts+++ b/packages/twenty-front-component-renderer/src/host/component-source/utils/__tests__/fetchComponentSource.spec.ts@@ -14,7 +14,13 @@ const buildFingerprintedUrl = (checksum: string): string =>const FINGERPRINTED_URL = buildFingerprintedUrl(computeSha256Hex(COMPONENT_SOURCE),);+const STALE_URL = buildFingerprintedUrl(+ computeSha256Hex('some other build output'),+);const BARE_URL = 'https://api.twenty.com/rest/front-components/component-id';+const PRESIGNED_URL =+ 'https://bucket.example.com/built.mjs?X-Amz-Signature=SECRET_SIGNATURE';+const CHECKSUM_MISMATCH_MESSAGE = 'Front component source checksum mismatch';const SHARED_DEPENDENCIES_SOURCE ='export const __shared_dependencies_react__ = {};';@@ -42,6 +48,18 @@ const createFakeJsResponse = (body: string) => ({json: jest.fn(async () => undefined),});+const createFakeJsonHandoffResponse = (presignedUrl: string) => ({+ ok: true,+ status: 200,+ statusText: 'OK',+ headers: {+ get: (name: string) =>+ name === 'content-type' ? 'application/json' : null,+ },+ text: jest.fn(async () => ''),+ json: jest.fn(async () => ({ url: presignedUrl })),+});+class FakeCache {private readonly store = new Map<string, string>();@@ -237,24 +255,58 @@ describe('fetchComponentSource', () => {expect(fetchMock).toHaveBeenCalledTimes(1);});- it('does not cache a network response whose checksum does not match the URL', async () => {+ it('rejects a network response whose checksum does not match the URL and does not cache it', async () => {const cache = new FakeCache();setupCaches(cache);- const staleUrl = buildFingerprintedUrl(- computeSha256Hex('some other build output'),- );-const fetchMock = jest.fn(async () =>createFakeJsResponse(COMPONENT_SOURCE),);globalThis.fetch = fetchMock as unknown as typeof fetch;- const source = await fetchComponentSource({ url: staleUrl });+ await expect(fetchComponentSource({ url: STALE_URL })).rejects.toThrow(+ CHECKSUM_MISMATCH_MESSAGE,+ );+ expect(cache.put).not.toHaveBeenCalled();+ });++ it('rejects a mismatched presigned response without leaking the presigned URL, the token, or the source', async () => {+ const cache = new FakeCache();++ setupCaches(cache);++ const fetchMock = jest+ .fn()+ .mockResolvedValueOnce(createFakeJsonHandoffResponse(PRESIGNED_URL))+ .mockResolvedValueOnce(+ createFakeJsResponse('globalThis.leak = "SOURCE_MARKER";'),+ );++ globalThis.fetch = fetchMock as unknown as typeof fetch;++ const error = await fetchComponentSource({+ url: FINGERPRINTED_URL,+ headers: { Authorization: 'Bearer SECRET_TOKEN' },+ }).catch((thrown: unknown) => thrown);++ expect(error).toBeInstanceOf(Error);++ const { message } = error as Error;++ expect(message).toContain(CHECKSUM_MISMATCH_MESSAGE);+ expect(message).toContain(FINGERPRINTED_URL);++ for (const secret of [+ 'SECRET_TOKEN',+ 'SECRET_SIGNATURE',+ 'bucket.example.com',+ 'SOURCE_MARKER',+ ]) {+ expect(message).not.toContain(secret);+ }- expect(source).toBe(COMPONENT_SOURCE);expect(cache.put).not.toHaveBeenCalled();});@@ -346,6 +398,20 @@ describe('fetchComponentSource', () => {expect(fetchMock).toHaveBeenCalledTimes(1);});+ it('rejects a mismatched network response when CacheStorage is unavailable', async () => {+ (globalThis as unknown as { caches?: unknown }).caches = undefined;++ const fetchMock = jest.fn(async () =>+ createFakeJsResponse(COMPONENT_SOURCE),+ );++ globalThis.fetch = fetchMock as unknown as typeof fetch;++ await expect(fetchComponentSource({ url: STALE_URL })).rejects.toThrow(+ CHECKSUM_MISMATCH_MESSAGE,+ );+ });+it('does not trust the cache when WebCrypto is unavailable', async () => {const cache = new FakeCache();diff --git a/packages/twenty-front-component-renderer/src/host/component-source/utils/fetchComponentSource.ts b/packages/twenty-front-component-renderer/src/host/component-source/utils/fetchComponentSource.tsindex 2f819310dbf..945b53cb49c 100644--- a/packages/twenty-front-component-renderer/src/host/component-source/utils/fetchComponentSource.ts+++ b/packages/twenty-front-component-renderer/src/host/component-source/utils/fetchComponentSource.ts@@ -1,4 +1,4 @@-import { isDefined } from 'twenty-shared/utils';+import { CustomError, isDefined } from 'twenty-shared/utils';import { computeComponentSourceChecksum } from '@/host/component-source/utils/computeComponentSourceChecksum';import { deleteComponentSourceFromCache } from '@/host/component-source/utils/deleteComponentSourceFromCache';@@ -42,13 +42,26 @@ export const fetchComponentSource = async ({const source = await fetchComponentSourceFromNetwork({ url, headers });- if (isDefined(cache) && isDefined(expectedChecksum)) {- const sourceChecksum = await computeComponentSourceChecksum({ source });+ if (!isDefined(expectedChecksum)) {+ return source;+ }- if (sourceChecksum === expectedChecksum) {- writeComponentSourceToCache({ cache, url, source });- evictStaleComponentSourceCacheEntries({ cache, url });- }+ const sourceChecksum = await computeComponentSourceChecksum({ source });++ if (!isDefined(sourceChecksum)) {+ return source;+ }++ if (sourceChecksum !== expectedChecksum) {+ throw new CustomError(+ `Front component source checksum mismatch for ${url}: expected ${expectedChecksum}, received ${sourceChecksum}`,+ 'FRONT_COMPONENT_SOURCE_CHECKSUM_MISMATCH',+ );+ }++ if (isDefined(cache)) {+ writeComponentSourceToCache({ cache, url, source });+ evictStaleComponentSourceCacheEntries({ cache, url });}return source;