Files
Peter EvansandJeremie Pardou 5a62c876e4 fix: align custom code in builder Preview with the editor on shared hostnames (#6881)
* fix: restrict editor-written code in the builder editor and on the editor hostname

* fix: simplify builder author code restrictions by hostname

* refactor: remove redundant backend custom code hostname checks

* refactor: unify builder HTML element rendering restrictions

* refactor: clarify builder web frontend hostname terminology

* fix: distinguish untrusted builder content in preview notices

* refactor: remove unused backend preview security setting

* docs: shorten builder code restriction changelog

* fix: restrict iframe cookie access across frontend ports

* refactor: share web frontend hostname matching

* fix: preserve protected HTML interactions and bound iframe height

---------

Co-authored-by: Jeremie Pardou <571533+jrmi@users.noreply.github.com>
2026-11-26 17:20:44 +00:00

88 lines
2.6 KiB
JavaScript

import { describe, expect, test } from 'vitest'
import {
getBuilderPreviewApiPath,
getBuilderPreviewCookieName,
getBuilderPreviewCookiePath,
getBuilderPreviewIdFromApiUrl,
getBuilderPreviewSsrCookieName,
getBuilderPreviewUserSourceAuthConfig,
getBuilderPreviewUserSourceCookieName,
previewSharesWebFrontendHostname,
} from '@baserow/modules/builder/utils/preview'
const config = { public: { baserowFrontendCookiePrefix: 'test_' } }
describe('builder preview cookies', () => {
test('uses fixed names and builder-specific paths', () => {
expect(getBuilderPreviewCookieName(config)).toBe(
'test_baserow_builder_preview'
)
expect(getBuilderPreviewSsrCookieName(config)).toBe(
'test_baserow_builder_preview_ssr'
)
expect(getBuilderPreviewUserSourceCookieName()).toBe(
'baserow_builder_preview_user_source'
)
expect(getBuilderPreviewCookiePath(42)).toBe('/builder/preview/42')
})
test('uses and recognizes builder-scoped preview API paths', () => {
expect(getBuilderPreviewApiPath(42, 'pages/7/elements/')).toBe(
'builder/preview/42/pages/7/elements/'
)
expect(
getBuilderPreviewIdFromApiUrl(
'https://api.example.com/api/builder/preview/42/pages/7/elements/'
)
).toBe(42)
expect(
getBuilderPreviewIdFromApiUrl(
'builder/domains/published/page/7/elements/'
)
).toBeNull()
})
test('provides core with generic user source authentication settings', () => {
const builder = {
id: 42,
user_sources: [{ id: 10 }, { id: 11 }],
}
expect(
getBuilderPreviewUserSourceAuthConfig(
builder,
'https://preview.example.com'
)
).toEqual({
authenticationUrls: {
10: 'builder/preview/42/user-sources/10/token-auth/',
11: 'builder/preview/42/user-sources/11/token-auth/',
},
refreshUrl: 'builder/preview/42/user-source-auth-refresh/',
cookieName: 'baserow_builder_preview_user_source',
cookieOptions: {
cookieUrl: 'https://preview.example.com',
path: '/builder/preview/42',
sameSite: 'Lax',
},
})
})
})
describe('previewSharesWebFrontendHostname', () => {
test.each([
['http://localhost:3000', 'http://localhost:3000', true],
['http://localhost:3001', 'http://localhost:3000', true],
['https://preview.example.com', 'https://example.com', false],
['', 'https://example.com', true],
])('%s vs %s', (builderPreviewUrl, publicWebFrontendUrl, expected) => {
expect(
previewSharesWebFrontendHostname({
builderPreviewUrl,
publicWebFrontendUrl,
})
).toBe(expected)
})
})