| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/API/Element/setHTML | [Back] [Original] |
Get to know MDN better
Want more browser support for this feature? Tell us why.
setHTML() Element HTML DocumentFragment DOM XSS
setHTML(input)
setHTML(input, options)
inputHTML
options sanitizerSanitizer SanitizerConfig "default"
SanitizerConfigSanitizer
(undefined)
TypeErroroptions.sanitizer
SanitizerConfig"allowed" "removed" "default" SanitizerSanitizerConfigsetHTML() HTML DocumentFragment DOM XSS
setHTML() <table> <col> HTML
HTML XSS
options.sanitizer setHTML() Sanitizer
XSS
Sanitizer.removeUnsafe()
HTML Element.innerHTML setHTML()
Element.setHTMLUnsafe()
XSS API
setHTML() HTML
// HTML
const unsanitizedString = "abc <script>alert(1)<" + "/script> def";
// ID "target"
const target = document.getElementById("target");
// setHTML()
target.setHTML(unsanitizedString);
// setHTML()
// div, p, button script
const sanitizer1 = new Sanitizer({
elements: ["div", "p", "button", "script"],
});
target.setHTML(unsanitizedString, { sanitizer: sanitizer1 });
// SanitizerConfig setHTML()
// divpbuttonscript/
target.setHTML(unsanitizedString, {
sanitizer: { removeElements: ["div", "p", "button", "script"] },
});
setHTML() HTML HTML
<button id="buttonDefault" type="button"></button>
<button id="buttonAllowScript" type="button">allowScript</button>
<button id="reload" type="button"></button>
<div id="target"></div>
<pre id="log"></pre>
#log {
height: 220px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
margin: 5px;
}
const logElement = document.querySelector("#log");
function log(text) {
logElement.textContent += text;
}
if ("Sanitizer" in window) {
<script> onclick XSS
// HTML
const unsanitizedString = `
<div>
<p>This is a paragraph. <button >Click me</button></p>
<script src="path/to/a/module.js" type="module"><script>
</div>
`;
const reload = document.querySelector("#reload");
reload.addEventListener("click", () => document.location.reload());
HTML
HTML
Sanitizer()
const defaultSanitizerButton = document.querySelector("#buttonDefault");
defaultSanitizerButton.addEventListener("click", () => {
//
target.setHTML(unsanitizedString);
// HTML
logElement.textContent =
"Default sanitizer: remove script element and onclick attribute\n\n";
log(`\nunsanitized: ${unsanitizedString}`);
log(`\nsanitized: ${target.innerHTML}`);
});
HTML <div><p><script>
setHTML <script>
const allowScriptButton = document.querySelector("#buttonAllowScript");
allowScriptButton.addEventListener("click", () => {
//
const sanitizer1 = new Sanitizer({
elements: ["div", "p", "script"],
});
target.setHTML(unsanitizedString, { sanitizer: sanitizer1 });
// HTML
logElement.textContent =
"Sanitizer: {elements: ['div', 'p', 'script']}\n Script removed even though allowed\n";
log(`\nunsanitized: ${unsanitizedString}`);
log(`\nsanitized: ${target.innerHTML}`);
});
} else {
log("HTML API ");
//
}
allowScript
<script> onclick
| HTML # dom-element-sethtml |
ElementactiveViewTransitionariaActiveDescendantElementariaAtomicariaAutoCompleteariaBrailleLabelariaBrailleRoleDescriptionariaBusyariaCheckedariaColCountariaColIndexariaColIndexTextariaColSpanariaControlsElementsariaCurrentariaDescribedByElementsariaDescriptionariaDetailsElementsariaDisabledariaErrorMessageElementsariaExpandedariaFlowToElementsariaHasPopupariaHiddenariaInvalidariaKeyShortcutsariaLabelariaLabelledByElementsariaLevelariaLiveariaModalariaMultiLineariaMultiSelectableariaOrientationariaOwnsElementsariaPlaceholderariaPosInSetariaPressedariaReadOnlyariaRelevantariaRequiredariaRoleDescriptionariaRowCountariaRowIndexariaRowIndexTextariaRowSpanariaSelectedariaSetSizeariaSortariaValueMaxariaValueMinariaValueNowariaValueTextassignedSlotattributeschildElementCountchildrenclassListclassNameclientHeightclientLeftclientTopclientWidthcurrentCSSZoomcustomElementRegistryelementTimingfirstElementChildidinnerHTMLlastElementChildlocalNamenamespaceURInextElementSiblingouterHTMLpartprefixpreviousElementSiblingrolescrollHeightscrollLeftscrollLeftMaxscrollTopscrollTopMaxscrollWidthshadowRootslottagNameafter()animate()append()ariaNotify()attachShadow()before()checkVisibility()closest()computedStyleMap()getAnimations()getAttribute()getAttributeNames()getAttributeNode()getAttributeNodeNS()getAttributeNS()getBoundingClientRect()getClientRects()getElementsByClassName()getElementsByTagName()getElementsByTagNameNS()getHTML()hasAttribute()hasAttributeNS()hasAttributes()hasPointerCapture()insertAdjacentElement()insertAdjacentHTML()insertAdjacentText()matches()moveBefore()prepend()pseudo()querySelector()querySelectorAll()releasePointerCapture()remove()removeAttribute()removeAttributeNode()removeAttributeNS()replaceChildren()replaceWith()requestFullscreen()requestPointerLock()scroll()scrollBy()scrollIntoView()scrollIntoViewIfNeeded()scrollTo()setAttribute()setAttributeNode()setAttributeNodeNS()setAttributeNS()setCapture()setHTML()setHTMLUnsafe()setPointerCapture()startViewTransition()toggleAttribute()afterscriptexecuteanimationcancelanimationendanimationiterationanimationstartauxclickbeforeinputbeforematchbeforescriptexecutebeforexrselectblurclickcompositionendcompositionstartcompositionupdatecontentvisibilityautostatechangecontextmenucopycutdblclickDOMActivateDOMMouseScrollfocusfocusinfocusoutfullscreenchangefullscreenerrorgesturechangegestureendgesturestartgotpointercaptureinputkeydownkeypresskeyuplostpointercapturemousedownmouseentermouseleavemousemovemouseoutmouseovermouseupmousewheelMozMousePixelScrollpastepointercancelpointerdownpointerenterpointerleavepointermovepointeroutpointeroverpointerrawupdatepointerupscrollscrollendscrollsnapchangescrollsnapchangingsecuritypolicyviolationtouchcanceltouchendtouchmovetouchstarttransitioncanceltransitionendtransitionruntransitionstartwebkitmouseforcechangedwebkitmouseforcedownwebkitmouseforceupwebkitmouseforcewillbeginwheel| Web Proxy Viewer | New URL | Original Page |