fix(v2): escape HTML entities in user tags attributes (#4894)

This commit is contained in:
Alexey Pyltsyn 2021-06-03 15:52:13 +03:00 committed by GitHub
parent 0587d6d6e7
commit 1349ece883
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 3 deletions

View file

@ -69,6 +69,7 @@
"cssnano": "^5.0.4", "cssnano": "^5.0.4",
"del": "^6.0.0", "del": "^6.0.0",
"detect-port": "^1.3.0", "detect-port": "^1.3.0",
"escape-html": "^1.0.3",
"eta": "^1.12.1", "eta": "^1.12.1",
"express": "^4.17.1", "express": "^4.17.1",
"file-loader": "^6.2.0", "file-loader": "^6.2.0",

View file

@ -8,7 +8,7 @@
import htmlTagObjectToString from '../htmlTags'; import htmlTagObjectToString from '../htmlTags';
describe('htmlTagObjectToString', () => { describe('htmlTagObjectToString', () => {
test('simple html tag', () => { test('valid html tag', () => {
expect( expect(
htmlTagObjectToString({ htmlTagObjectToString({
tagName: 'script', tagName: 'script',
@ -17,10 +17,11 @@ describe('htmlTagObjectToString', () => {
src: src:
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js', 'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
async: true, async: true,
'data-options': '{"prop":true}',
}, },
}), }),
).toMatchInlineSnapshot( ).toMatchInlineSnapshot(
`"<script type=\\"text/javascript\\" src=\\"https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js\\" async></script>"`, `"<script type=\\"text/javascript\\" src=\\"https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js\\" async data-options=\\"{&quot;prop&quot;:true}\\"></script>"`,
); );
expect( expect(

View file

@ -9,6 +9,7 @@ import {isPlainObject} from 'lodash';
import {HtmlTagObject} from '@docusaurus/types'; import {HtmlTagObject} from '@docusaurus/types';
import htmlTags from 'html-tags'; import htmlTags from 'html-tags';
import voidHtmlTags from 'html-tags/void'; import voidHtmlTags from 'html-tags/void';
import escapeHTML from 'escape-html';
function assertIsHtmlTagObject(val: unknown): asserts val is HtmlTagObject { function assertIsHtmlTagObject(val: unknown): asserts val is HtmlTagObject {
if (!isPlainObject(val)) { if (!isPlainObject(val)) {
@ -41,7 +42,7 @@ export default function htmlTagObjectToString(tagDefinition: unknown): string {
if (tagAttributes[attributeName] === true) { if (tagAttributes[attributeName] === true) {
return attributeName; return attributeName;
} }
return `${attributeName}="${tagAttributes[attributeName]}"`; return `${attributeName}="${escapeHTML(tagAttributes[attributeName])}"`;
}); });
return `<${[tagDefinition.tagName].concat(attributes).join(' ')}>${ return `<${[tagDefinition.tagName].concat(attributes).join(' ')}>${
(!isVoidTag && tagDefinition.innerHTML) || '' (!isVoidTag && tagDefinition.innerHTML) || ''