|
| 1 | +import type { Logger } from '../ports/logger-port'; |
| 2 | +import type { ForestServerActionFormLayoutElement } from '@forestadmin/forestadmin-client'; |
| 3 | + |
| 4 | +import sanitizeHtml from 'sanitize-html'; |
| 5 | + |
| 6 | +const MAX_HTML_CHARACTERS = 256 * 1024; |
| 7 | + |
| 8 | +const MAX_LAYOUT_DEPTH = 10; |
| 9 | + |
| 10 | +const SAFE_STYLE_VALUE = |
| 11 | + /^(?![^;{}\\]*(?:url|expression|image-set|element|-moz-binding)\s*\()[^;{}\\]*$/i; |
| 12 | + |
| 13 | +const ALLOWED_STYLE_PROPERTIES = [ |
| 14 | + 'background', |
| 15 | + 'background-color', |
| 16 | + 'border', |
| 17 | + 'border-bottom', |
| 18 | + 'border-collapse', |
| 19 | + 'border-left', |
| 20 | + 'border-radius', |
| 21 | + 'border-right', |
| 22 | + 'border-top', |
| 23 | + 'color', |
| 24 | + 'display', |
| 25 | + 'font-family', |
| 26 | + 'font-size', |
| 27 | + 'font-style', |
| 28 | + 'font-weight', |
| 29 | + 'height', |
| 30 | + 'letter-spacing', |
| 31 | + 'line-height', |
| 32 | + 'margin', |
| 33 | + 'margin-bottom', |
| 34 | + 'margin-left', |
| 35 | + 'margin-right', |
| 36 | + 'margin-top', |
| 37 | + 'max-width', |
| 38 | + 'min-width', |
| 39 | + 'opacity', |
| 40 | + 'overflow', |
| 41 | + 'padding', |
| 42 | + 'padding-bottom', |
| 43 | + 'padding-left', |
| 44 | + 'padding-right', |
| 45 | + 'padding-top', |
| 46 | + 'text-align', |
| 47 | + 'text-decoration', |
| 48 | + 'text-transform', |
| 49 | + 'vertical-align', |
| 50 | + 'white-space', |
| 51 | + 'width', |
| 52 | +]; |
| 53 | + |
| 54 | +const OPTIONS: sanitizeHtml.IOptions = { |
| 55 | + allowedAttributes: { ...sanitizeHtml.defaults.allowedAttributes, '*': ['style'] }, |
| 56 | + allowedClasses: { '*': ['c-*', 'l-*'] }, |
| 57 | + allowedStyles: { |
| 58 | + '*': Object.fromEntries(ALLOWED_STYLE_PROPERTIES.map(name => [name, [SAFE_STYLE_VALUE]])), |
| 59 | + }, |
| 60 | +}; |
| 61 | + |
| 62 | +function sanitize(html: unknown, logger: Logger): string | null { |
| 63 | + if (typeof html !== 'string') return null; |
| 64 | + |
| 65 | + let bounded = html; |
| 66 | + |
| 67 | + if (bounded.length > MAX_HTML_CHARACTERS) { |
| 68 | + logger('Warn', 'Action html truncated: longer than the sanitizable size', { |
| 69 | + characters: bounded.length, |
| 70 | + limit: MAX_HTML_CHARACTERS, |
| 71 | + }); |
| 72 | + |
| 73 | + bounded = bounded.slice(0, MAX_HTML_CHARACTERS); |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + return sanitizeHtml(bounded, OPTIONS) || null; |
| 78 | + } catch (error) { |
| 79 | + logger('Error', 'Action html dropped: sanitization failed', { cause: String(error) }); |
| 80 | + |
| 81 | + return null; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export default function sanitizeActionHtml(html: unknown, logger: Logger): string | null { |
| 86 | + return sanitize(html, logger); |
| 87 | +} |
| 88 | + |
| 89 | +export function sanitizeActionLayout( |
| 90 | + layout: ForestServerActionFormLayoutElement[], |
| 91 | + logger: Logger, |
| 92 | + depth = 0, |
| 93 | + budget: { remaining: number } = { remaining: MAX_HTML_CHARACTERS }, |
| 94 | +): ForestServerActionFormLayoutElement[] { |
| 95 | + return layout.map(element => { |
| 96 | + if (element?.component === 'htmlBlock') { |
| 97 | + const { content } = element; |
| 98 | + |
| 99 | + if (typeof content !== 'string') { |
| 100 | + return { ...element, content: sanitize(content, logger) ?? '' }; |
| 101 | + } |
| 102 | + |
| 103 | + if (content.length > budget.remaining) { |
| 104 | + logger( |
| 105 | + 'Warn', |
| 106 | + 'Action layout html truncated: total layout html longer than the sanitizable size', |
| 107 | + { |
| 108 | + characters: content.length, |
| 109 | + remaining: budget.remaining, |
| 110 | + limit: MAX_HTML_CHARACTERS, |
| 111 | + }, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + const bounded = content.slice(0, budget.remaining); |
| 116 | + |
| 117 | + budget.remaining -= bounded.length; |
| 118 | + |
| 119 | + return { ...element, content: sanitize(bounded, logger) ?? '' }; |
| 120 | + } |
| 121 | + |
| 122 | + if (element?.component === 'page' && Array.isArray(element.elements)) { |
| 123 | + if (depth >= MAX_LAYOUT_DEPTH) { |
| 124 | + logger('Warn', 'Action layout elements dropped: nested deeper than the sanitizable depth', { |
| 125 | + limit: MAX_LAYOUT_DEPTH, |
| 126 | + }); |
| 127 | + |
| 128 | + return { ...element, elements: [] }; |
| 129 | + } |
| 130 | + |
| 131 | + return { |
| 132 | + ...element, |
| 133 | + elements: sanitizeActionLayout(element.elements, logger, depth + 1, budget), |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + return element; |
| 138 | + }); |
| 139 | +} |
0 commit comments