RenderRichText neutralises unsafe link URLs (safeHref)
Payload's link converter rendered fields.url as is; javascript:/data: URLs stored via import or API reached the href. They now become '#'. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
7967453f57
commit
1beb0edb7f
@@ -5,6 +5,7 @@ import { RichText as PayloadRichText } from '@payloadcms/richtext-lexical/react'
|
||||
import { cloneElement, isValidElement, type ReactElement, type ReactNode } from 'react'
|
||||
|
||||
import { headingIds as computeHeadingIds, type HeadingLevel } from './headings.js'
|
||||
import { sanitizeRichTextLinks } from './sanitizeLinks.js'
|
||||
|
||||
export type RenderRichTextProps = {
|
||||
/** Wrapper class (e.g. Tailwind prose classes). */
|
||||
@@ -96,8 +97,10 @@ function withHeadingIds(
|
||||
* <TableOfContents items={extractHeadings(page.content)} label={toc.label} /> // /client
|
||||
* <RenderRichText data={page.content} headingIds={{ className: 'scroll-mt-24' }} />
|
||||
*/
|
||||
export function RenderRichText({ className, converters, data, headingIds }: RenderRichTextProps) {
|
||||
if (!data) {return null}
|
||||
export function RenderRichText({ className, converters, data: raw, headingIds }: RenderRichTextProps) {
|
||||
if (!raw) {return null}
|
||||
// javascript:/data: link URLs from stored content never reach an href
|
||||
const data = sanitizeRichTextLinks(raw)
|
||||
if (headingIds) {
|
||||
const opts = headingIds === true ? {} : headingIds
|
||||
const ids = computeHeadingIds(data, opts.levels ? { levels: opts.levels } : {})
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { safeHref } from '../links/resolveLinkHref.js'
|
||||
|
||||
type LexicalNode = { [key: string]: unknown; children?: unknown[]; fields?: { [key: string]: unknown; url?: unknown }; type?: string }
|
||||
|
||||
/**
|
||||
* Rich text with custom links whose URL isn't a safe target (`javascript:`,
|
||||
* `data:`, "java\tscript:") gets them neutralised to '#' — the same rule as
|
||||
* <CMSLink> (safeHref). Payload's link converter renders `fields.url` as is,
|
||||
* and stored content can come from imports, seeds or the API, not only the
|
||||
* editor's link drawer.
|
||||
*
|
||||
* Returns the SAME object when nothing needed changing (no copy per render);
|
||||
* otherwise only the path to each unsafe link is copied.
|
||||
*/
|
||||
export function sanitizeRichTextLinks<T>(data: T): T {
|
||||
return walk(data) as T
|
||||
}
|
||||
|
||||
function walk(value: unknown): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
let changed = false
|
||||
const next = value.map((item) => {
|
||||
const out = walk(item)
|
||||
if (out !== item) {changed = true}
|
||||
return out
|
||||
})
|
||||
return changed ? next : value
|
||||
}
|
||||
if (!value || typeof value !== 'object') {return value}
|
||||
const node = value as LexicalNode
|
||||
let result: LexicalNode = node
|
||||
|
||||
if ((node.type === 'link' || node.type === 'autolink') && node.fields && node.fields.linkType !== 'internal') {
|
||||
const url = typeof node.fields.url === 'string' ? node.fields.url : ''
|
||||
if (url && safeHref(url) === null) {result = { ...node, fields: { ...node.fields, url: '#' } }}
|
||||
}
|
||||
for (const key of ['root', 'children'] as const) {
|
||||
if (!(key in result)) {continue}
|
||||
const out = walk(result[key])
|
||||
if (out !== result[key]) {result = { ...result, [key]: out }}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user