Skip to content

Commit ff28995

Browse files
nsyseanedemaine
andauthored
Merge commit from fork
* fix: escape \htmlData attribute name * simplify escape lookup, add escape characters * Add escape list source * Fix escape list source * fix: handling invalid HTML attribute names * fix: change comments position * fix: change HTML attribute name validator * Factor out regex * Improve tests to apply to check individual characters * Rename regex --------- Co-authored-by: Erik Demaine <edemaine@mit.edu>
1 parent 28a0bf5 commit ff28995

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"no-array-constructor": 2,
4040
"no-console": 2,
4141
"no-const-assign": 2,
42+
"no-control-regex": 0,
4243
"no-debugger": 2,
4344
"no-dupe-class-members": 2,
4445
"no-dupe-keys": 2,

src/domTree.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {path} from "./svgGeometry";
1717
import type Options from "./Options";
1818
import {DocumentFragment} from "./tree";
1919
import {makeEm} from "./units";
20+
import ParseError from "./ParseError";
2021

2122
import type {VirtualNode} from "./tree";
2223

@@ -83,6 +84,16 @@ const toNode = function(tagName: string): HTMLElement {
8384
return node;
8485
};
8586

87+
/**
88+
* https://w3c.github.io/html-reference/syntax.html#syntax-attributes
89+
*
90+
* > Attribute Names must consist of one or more characters
91+
* other than the space characters, U+0000 NULL,
92+
* '"', "'", ">", "/", "=", the control characters,
93+
* and any characters that are not defined by Unicode.
94+
*/
95+
const invalidAttributeNameRegex = /[\s"'>/=\x00-\x1f]/;
96+
8697
/**
8798
* Convert into an HTML markup string
8899
*/
@@ -110,6 +121,9 @@ const toMarkup = function(tagName: string): string {
110121
// Add the attributes
111122
for (const attr in this.attributes) {
112123
if (this.attributes.hasOwnProperty(attr)) {
124+
if (invalidAttributeNameRegex.test(attr)) {
125+
throw new ParseError(`Invalid attribute name '${attr}'`);
126+
}
113127
markup += ` ${attr}="${utils.escape(this.attributes[attr])}"`;
114128
}
115129
}

test/katex-spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,24 @@ describe("An HTML extension builder", function() {
21582158
const built = getBuilt(html, trustNonStrictSettings);
21592159
expect(built).toMatchSnapshot();
21602160
});
2161+
2162+
it("should throw Error when HTML attribute name is invalid", function() {
2163+
for (const char of [">", " ", "\t", "\n", "\r", "\"", "'", "/"]) {
2164+
try {
2165+
katex.renderToString(
2166+
`\\htmlData{a${char}b=foo}{bar}`, trustNonStrictSettings);
2167+
2168+
// Render is expected to throw, so this should not be called.
2169+
expect(true).toBe(false);
2170+
} catch (error) {
2171+
expect(error).toBeInstanceOf(ParseError);
2172+
const message =
2173+
`Invalid attribute name 'data-a${char.replace(/\s/, ' ')}b'`;
2174+
expect(error.message).toBe(`KaTeX parse error: ${message}`);
2175+
expect(error.rawMessage).toBe(message);
2176+
}
2177+
}
2178+
});
21612179
});
21622180

21632181
describe("A bin builder", function() {

0 commit comments

Comments
 (0)