Close medium exposure on reports, registration, uploads, and exercise HTML.
Deploy Development / deploy (push) Successful in 59s
Test Suite / pytest-backend (push) Failing after 47s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 17s
Test Suite / k6 /health Baseline (push) Successful in 34s
Test Suite / playwright-tests (push) Failing after 7m48s
Deploy Development / deploy (push) Successful in 59s
Test Suite / pytest-backend (push) Failing after 47s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 17s
Test Suite / k6 /health Baseline (push) Successful in 34s
Test Suite / playwright-tests (push) Failing after 7m48s
A superadmin can also turn off another account''s two-factor when the authenticator is lost. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
5ea225ec6b
commit
f10c28e5cd
20 files changed
+480
-247
No files matched your search
@@ -1,111 +1,79 @@
|
||||
/**
|
||||
* Sanitizer für Übungs-Rich-HTML inkl. §11 Platzhalter (span data-shinkan-exercise-media).
|
||||
* Restriktiver als sanitizeTrainerHtml: Allowlist für XSS-Minimierung.
|
||||
* DOMPurify mit fester Allowlist.
|
||||
*/
|
||||
|
||||
import DOMPurify from 'dompurify'
|
||||
import { sanitizeInlineMediaCaption } from './inlineMediaCaption'
|
||||
|
||||
const ALLOWED_BLOCK = new Set(['p', 'div', 'br', 'ul', 'ol', 'li', 'h3'])
|
||||
const ALLOWED_INLINE = new Set(['b', 'strong', 'i', 'em', 'u', 'span', 'a'])
|
||||
const ALLOWED_TAGS = ['p', 'div', 'br', 'ul', 'ol', 'li', 'h3', 'b', 'strong', 'i', 'em', 'u', 'span', 'a']
|
||||
const ALLOWED_ATTR = [
|
||||
'href',
|
||||
'class',
|
||||
'data-shinkan-exercise-media',
|
||||
'data-shinkan-exercise-media-size',
|
||||
'data-shinkan-exercise-media-caption',
|
||||
]
|
||||
const SIZE_OK = new Set(['small', 'medium', 'full'])
|
||||
|
||||
function isHttpsUrl(val) {
|
||||
if (!val || typeof val !== 'string') return false
|
||||
const s = val.trim()
|
||||
return s.startsWith('http://') || s.startsWith('https://')
|
||||
function hookAttribute(_node, data) {
|
||||
const name = data.attrName
|
||||
if (name === 'href') {
|
||||
const href = String(data.attrValue || '').trim()
|
||||
if (!/^https?:\/\//i.test(href)) data.keepAttr = false
|
||||
else data.attrValue = href
|
||||
return
|
||||
}
|
||||
if (name === 'class') {
|
||||
const keep = String(data.attrValue || '')
|
||||
.split(/\s+/)
|
||||
.filter((c) => c === 'shinkan-inline-media')
|
||||
if (!keep.length) data.keepAttr = false
|
||||
else data.attrValue = keep.join(' ')
|
||||
return
|
||||
}
|
||||
if (name === 'data-shinkan-exercise-media') {
|
||||
if (!/^\d+$/.test(String(data.attrValue || '').trim())) data.keepAttr = false
|
||||
return
|
||||
}
|
||||
if (name === 'data-shinkan-exercise-media-size') {
|
||||
const size = String(data.attrValue || '').trim().toLowerCase()
|
||||
if (!SIZE_OK.has(size)) data.keepAttr = false
|
||||
else data.attrValue = size
|
||||
return
|
||||
}
|
||||
if (name === 'data-shinkan-exercise-media-caption') {
|
||||
const caption = sanitizeInlineMediaCaption(String(data.attrValue || ''))
|
||||
if (!caption) data.keepAttr = false
|
||||
else data.attrValue = caption
|
||||
}
|
||||
}
|
||||
|
||||
/** Nur für unsere Embed-Markierung: erlaubt data-attribut und optionale Marker-Klasse + Größe. */
|
||||
const _SIZE_OK = new Set(['small', 'medium', 'full'])
|
||||
function isInlineExerciseMediaPlaceholderSpan(el) {
|
||||
if (!el?.getAttribute || el.tagName.toLowerCase() !== 'span') return false
|
||||
const raw = el.getAttribute('data-shinkan-exercise-media')
|
||||
if (!raw || !String(raw).trim().match(/^\d+$/)) return false
|
||||
return true
|
||||
}
|
||||
const instances = new WeakMap()
|
||||
|
||||
function sanitizeAttributes(el, tagLower) {
|
||||
if (tagLower === 'a') {
|
||||
const href = el.getAttribute('href')
|
||||
if (href && isHttpsUrl(href)) {
|
||||
const out = document.createElement('a')
|
||||
out.setAttribute('href', href.trim())
|
||||
return out.attributes
|
||||
}
|
||||
return []
|
||||
}
|
||||
if (tagLower === 'span' && isInlineExerciseMediaPlaceholderSpan(el)) {
|
||||
const out = document.createElement('span')
|
||||
out.setAttribute('data-shinkan-exercise-media', el.getAttribute('data-shinkan-exercise-media').trim())
|
||||
const sz = (el.getAttribute('data-shinkan-exercise-media-size') || '').trim().toLowerCase()
|
||||
if (sz && _SIZE_OK.has(sz)) {
|
||||
out.setAttribute('data-shinkan-exercise-media-size', sz)
|
||||
}
|
||||
const capRaw = el.getAttribute('data-shinkan-exercise-media-caption')
|
||||
if (capRaw != null && String(capRaw).trim()) {
|
||||
const cap = sanitizeInlineMediaCaption(String(capRaw))
|
||||
if (cap) out.setAttribute('data-shinkan-exercise-media-caption', cap)
|
||||
}
|
||||
const cls = (el.getAttribute('class') || '').trim().split(/\s+/).filter(Boolean)
|
||||
const keep = cls.filter((c) => c === 'shinkan-inline-media')
|
||||
if (keep.length) out.setAttribute('class', keep.join(' '))
|
||||
return out.attributes
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
function cloneAsAllowed(tagLower, el) {
|
||||
const fresh = document.createElement(tagLower)
|
||||
const attrs = sanitizeAttributes(el, tagLower)
|
||||
for (const a of attrs) {
|
||||
fresh.setAttribute(a.name, a.value)
|
||||
}
|
||||
return fresh
|
||||
}
|
||||
|
||||
function cleanTree(parent) {
|
||||
const nodes = Array.from(parent.childNodes)
|
||||
for (const node of nodes) {
|
||||
if (node.nodeType === Node.TEXT_NODE) continue
|
||||
if (node.nodeType === Node.COMMENT_NODE) {
|
||||
parent.removeChild(node)
|
||||
continue
|
||||
}
|
||||
if (node.nodeType !== Node.ELEMENT_NODE) {
|
||||
parent.removeChild(node)
|
||||
continue
|
||||
}
|
||||
const tag = node.tagName.toLowerCase()
|
||||
if (tag === 'script' || tag === 'iframe' || tag === 'object' || tag === 'embed') {
|
||||
parent.removeChild(node)
|
||||
continue
|
||||
}
|
||||
if (ALLOWED_BLOCK.has(tag) || ALLOWED_INLINE.has(tag)) {
|
||||
const repl = cloneAsAllowed(tag, node)
|
||||
while (node.firstChild) {
|
||||
repl.appendChild(node.firstChild)
|
||||
}
|
||||
cleanTree(repl)
|
||||
parent.replaceChild(repl, node)
|
||||
continue
|
||||
}
|
||||
while (node.firstChild) {
|
||||
parent.insertBefore(node.firstChild, node)
|
||||
}
|
||||
parent.removeChild(node)
|
||||
}
|
||||
function purifyFor(win) {
|
||||
const existing = instances.get(win)
|
||||
if (existing) return existing
|
||||
const purify = DOMPurify(win)
|
||||
purify.addHook('uponSanitizeAttribute', hookAttribute)
|
||||
instances.set(win, purify)
|
||||
return purify
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string|null|undefined} html
|
||||
* @param {Window=} win
|
||||
* @returns {string}
|
||||
*/
|
||||
export function sanitizeExerciseRichDisplayHtml(html) {
|
||||
export function sanitizeExerciseRichDisplayHtml(html, win) {
|
||||
if (html == null || typeof html !== 'string') return ''
|
||||
const trimmed = html.trim()
|
||||
if (!trimmed) return ''
|
||||
|
||||
const tpl = document.createElement('template')
|
||||
tpl.innerHTML = trimmed
|
||||
cleanTree(tpl.content)
|
||||
return tpl.innerHTML
|
||||
const target = win || (typeof window !== 'undefined' ? window : null)
|
||||
if (!target) return ''
|
||||
return purifyFor(target).sanitize(trimmed, {
|
||||
ALLOWED_TAGS,
|
||||
ALLOWED_ATTR,
|
||||
ALLOW_DATA_ATTR: false,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user