Opt-outs for the secure defaults; isPathVisibleIn in construction/logic
- setUnknownIpFactor (0 = old free pass), UNKNOWN_IP_FACTOR exported
- createContentHelpers({ settingsDepth }) and markdown/mcp isPathVisible overrides
- <RenderRichText safeLinks={false}>
- resolveRedirect({ draft: true }) for preview
- isPathVisibleIn(mode, path), pathOf, PathVisibility exported; page route uses it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4585e0fc69
commit
0fd57c1d6a
@@ -7,7 +7,7 @@ import path from 'node:path'
|
||||
import { buildConfig, getPayload } from 'payload'
|
||||
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
|
||||
|
||||
import { constructionNoticeHref, isPathAllowed, normalizePath } from '../src/modules/construction/logic.js'
|
||||
import { constructionNoticeHref, isPathAllowed, isPathVisibleIn, normalizePath } from '../src/modules/construction/logic.js'
|
||||
import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js'
|
||||
import { createPageRoute } from '../src/modules/frontend/pageRoute.js'
|
||||
import { ipalKit } from '../src/plugin.js'
|
||||
@@ -22,6 +22,12 @@ describe('under construction — path logic', () => {
|
||||
expect(isPathAllowed('/pl/o-nas', ['/pl', '/pl/brief'])).toBe(false)
|
||||
expect(constructionNoticeHref('/pl')).toBe('/pl?notice=construction')
|
||||
})
|
||||
|
||||
test('isPathVisibleIn: off → everything; on → allowed paths only', () => {
|
||||
expect(isPathVisibleIn({ allowedPaths: [], enabled: false }, '/pl/o-nas')).toBe(true)
|
||||
expect(isPathVisibleIn({ allowedPaths: ['/pl', '/pl/brief'], enabled: true }, '/pl/brief/')).toBe(true)
|
||||
expect(isPathVisibleIn({ allowedPaths: ['/pl', '/pl/brief'], enabled: true }, '/pl/o-nas')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('under construction — Site Settings → createPageRoute', () => {
|
||||
@@ -116,3 +122,27 @@ describe('under construction — Site Settings → createPageRoute', () => {
|
||||
expect(await helpers.isPathVisible('/pl/brief', 'pl')).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('under construction — opting out per route', () => {
|
||||
test('a project can pass its own isPathVisible to Markdown / MCP', async () => {
|
||||
const { createMcpRoute } = await import('../src/modules/mcp/createMcpRoute.js')
|
||||
const seen: string[] = []
|
||||
const mcp = createMcpRoute({
|
||||
config: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] },
|
||||
getCachedPayload: async () => ({}) as never,
|
||||
isPathVisible: (path) => {
|
||||
seen.push(path)
|
||||
return false
|
||||
},
|
||||
rateLimit: false,
|
||||
})
|
||||
const res = await mcp.POST(
|
||||
new Request('https://x/api/mcp', {
|
||||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'tools/call', params: { name: 'get_page', arguments: { path: '/pl/o-nas' } } }),
|
||||
method: 'POST',
|
||||
}),
|
||||
)
|
||||
expect(JSON.stringify(await res.json())).toContain('No page found')
|
||||
expect(seen).toEqual(['/pl/o-nas'])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
clientIp,
|
||||
consumeRateLimit,
|
||||
setRateLimitStore,
|
||||
setUnknownIpFactor,
|
||||
UNKNOWN_IP_FACTOR,
|
||||
} from '../src/modules/shared/rateLimit.js'
|
||||
|
||||
@@ -65,6 +66,13 @@ describe('checkRateLimit (in memory)', () => {
|
||||
expect(checkRateLimit({ cost: 4, key: `k-${Math.random()}`, max: 3 })).toBe(false)
|
||||
})
|
||||
|
||||
test('setUnknownIpFactor(0) restores the old free pass; the default comes back', () => {
|
||||
setUnknownIpFactor(0)
|
||||
expect(checkRateLimit({ key: '', max: 0, scope: `z-${Math.random()}` })).toBe(true)
|
||||
setUnknownIpFactor(UNKNOWN_IP_FACTOR)
|
||||
expect(checkRateLimit({ key: '', max: 0, scope: `z-${Math.random()}` })).toBe(false)
|
||||
})
|
||||
|
||||
test('no key → one shared bucket with a roomier limit, not a free pass', () => {
|
||||
const scope = `s-${Math.random()}`
|
||||
for (let i = 0; i < 2 * UNKNOWN_IP_FACTOR; i++) {expect(checkRateLimit({ key: '', max: 2, scope })).toBe(true)}
|
||||
|
||||
@@ -18,6 +18,11 @@ describe('old-slug redirects and drafts', () => {
|
||||
expect(await resolveRedirect({ config, locale: 'pl', payload, segments: ['oferta'] })).toBeNull()
|
||||
})
|
||||
|
||||
test('draft: true (preview) redirects drafts too', async () => {
|
||||
const payload = payloadWith({ slug: 'tajny-projekt', _status: 'draft' })
|
||||
expect(await resolveRedirect({ config, draft: true, locale: 'pl', payload, segments: ['oferta'] })).toBe('/tajny-projekt')
|
||||
})
|
||||
|
||||
test('collections without drafts still redirect', async () => {
|
||||
const payload = payloadWith({ slug: 'kontakt-2' })
|
||||
expect(await resolveRedirect({ config, locale: 'pl', payload, segments: ['kontakt'] })).toBe('/kontakt-2')
|
||||
|
||||
@@ -28,3 +28,24 @@ describe('rich text links', () => {
|
||||
expect(out.root.children[0].children[0]).toBe(d.root.children[0].children[0])
|
||||
})
|
||||
})
|
||||
|
||||
describe('RenderRichText', () => {
|
||||
const state = {
|
||||
root: {
|
||||
type: 'root', children: [{ type: 'paragraph', children: [{ type: 'link', children: [{ type: 'text', detail: 0, format: 0, mode: 'normal', style: '', text: 'klik', version: 1 }], fields: { linkType: 'custom', url: 'data:text/html,<script>alert(1)</script>' }, version: 3 }], version: 1 }],
|
||||
direction: null, format: '', indent: 0, version: 1,
|
||||
},
|
||||
}
|
||||
|
||||
// React 19 already blocks javascript: hrefs; data: URLs it renders as is.
|
||||
test('renders unsafe links as # by default; safeLinks={false} keeps them', async () => {
|
||||
const { renderToStaticMarkup } = await import('react-dom/server')
|
||||
const { createElement } = await import('react')
|
||||
const { RenderRichText } = await import('../src/modules/richtext/RenderRichText.js')
|
||||
const html = renderToStaticMarkup(createElement(RenderRichText, { data: state as never }))
|
||||
expect(html).toContain('href="#"')
|
||||
expect(html).not.toContain('data:')
|
||||
const raw = renderToStaticMarkup(createElement(RenderRichText, { data: state as never, safeLinks: false }))
|
||||
expect(raw).toContain('href="data:text/html')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -82,3 +82,11 @@ describe('Site Settings depth', () => {
|
||||
expect((route?.doc as { hero: { alt?: string } }).hero).toMatchObject({ alt: 'hero' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('settingsDepth option', () => {
|
||||
test('a project that needs deeper settings can raise the depth', async () => {
|
||||
const { getSettings } = createContentHelpers({ config: configPromise, settingsDepth: 2 })
|
||||
const settings = (await getSettings('pl')) as { featured: { hero: { alt?: string } } }
|
||||
expect(settings.featured.hero).toMatchObject({ alt: 'hero' })
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user