syncova-ticket/server/content.js
Weapie 9c561cb574
All checks were successful
Tests und Docker-Image / Tests und Frontend-Build (push) Successful in 2m8s
Tests und Docker-Image / Leeres Produktionsimage bauen und veröffentlichen (push) Successful in 4m28s
Add Syncova Ticket with tested Gitea image publishing and persistent Compose deployment
2026-11-03 17:04:00 +01:00

131 lines
4.0 KiB
JavaScript

import sanitizeHtml from 'sanitize-html'
import { convert } from 'html-to-text'
import { HttpError } from './service.js'
import { expandSignature } from './signature-placeholders.js'
export const escapeHtml = (value) =>
String(value).replace(
/[&<>"']/g,
(c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c],
)
export function safeHtml(html) {
return sanitizeHtml(html, {
allowedTags: [
'p',
'br',
'b',
'strong',
'i',
'em',
'u',
's',
'div',
'span',
'a',
'ul',
'ol',
'li',
'table',
'thead',
'tbody',
'tr',
'td',
'th',
'img',
'hr',
],
allowedAttributes: {
'*': ['style'],
a: ['href', 'title'],
img: ['src', 'alt', 'width', 'height'],
td: ['colspan', 'rowspan'],
th: ['colspan', 'rowspan'],
},
allowedSchemes: ['https', 'http', 'mailto', 'tel'],
allowedSchemesByTag: { img: ['https', 'data'] },
allowProtocolRelative: false,
allowedStyles: {
'*': {
color: [/^#[0-9a-f]{3,8}$/i, /^[a-z]+$/i, /^rgb\([\d\s,]+\)$/i],
'background-color': [/^#[0-9a-f]{3,8}$/i, /^[a-z]+$/i],
'font-family': [/^[a-z\s,\-']+$/i],
'font-size': [/^\d{1,2}(px|pt)$/],
'font-weight': [/^(normal|bold|[1-9]00)$/],
'text-align': [/^(left|right|center|justify)$/],
'text-decoration': [/^(none|underline|line-through)$/],
'line-height': [/^[\d.]+(px|em|%)?$/],
padding: [/^[\d\s.]+(px|pt)$/],
'border-collapse': [/^(collapse|separate)$/],
},
},
transformTags: {
img: (tag, attribs) => {
if (
attribs.src?.startsWith('data:') &&
!/^data:image\/(png|jpeg|gif|webp);base64,[a-z\d+/=]+$/i.test(attribs.src)
)
delete attribs.src
return { tagName: tag, attribs }
},
},
})
}
export function plainHtml(html) {
return convert(html, {
wordwrap: false,
selectors: [
{ selector: 'img', format: 'skip' },
{ selector: 'a', options: { hideLinkHrefIfSameAsText: true } },
],
}).trim()
}
export function signatureValue(s, context) {
return s?.format === 'html'
? (() => {
const html = safeHtml(expandSignature(safeHtml(s.body), context, escapeHtml))
return { html, text: plainHtml(html) }
})()
: { html: null, text: expandSignature(s?.body || '', context) }
}
export function composeContent(body, signature, context) {
const sig = signatureValue(signature, context)
let base = body.trimEnd()
if (sig.text && base.endsWith(sig.text)) base = base.slice(0, -sig.text.length).trimEnd()
return {
text: base + (sig.text ? '\n\n' + sig.text : ''),
html: sig.html
? `<div style="white-space:pre-wrap">${escapeHtml(base).replace(/\n/g, '<br>')}</div><br>${sig.html}`
: null,
}
}
export function normaliseAttachments(values = []) {
if (!Array.isArray(values) || values.length > 20)
throw new HttpError(400, 'Höchstens 20 Anhänge pro Nachricht.')
let total = 0
return values.map((a) => {
if (
!a ||
typeof a.content !== 'string' ||
a.content.length > 13981016 ||
a.content.length % 4 !== 0 ||
/[^a-z\d+/=]/i.test(a.content)
)
throw new HttpError(400, 'Ungültiger Anhang (maximal 10 MB pro Datei).')
const content = Buffer.from(a.content, 'base64')
if (content.length > 10 * 1024 * 1024 || content.toString('base64') !== a.content)
throw new HttpError(413, 'Anhang ist zu groß oder ungültig.')
total += content.length
if (total > 20 * 1024 * 1024)
throw new HttpError(413, 'Anhänge dürfen zusammen höchstens 20 MB groß sein.')
return {
name: String(a.name || 'Anhang')
.replace(/[\x00-\x1f\x7f/\\]/g, '_')
.slice(0, 200),
mime:
typeof a.mime === 'string' && /^[a-z\d!#$&^_.+-]+\/[a-z\d!#$&^_.+-]+$/i.test(a.mime)
? a.mime
: 'application/octet-stream',
content,
}
})
}