Files
gitea/tools/eslint-rules/unescaped-html-literal.ts
T
silverwind 428d72d26f chore: re-enable and extend unescaped-html-literal eslint rule (#39832)
https://github.com/go-gitea/gitea/pull/38046 accidentally dropped
`gitea/unescaped-html-literal` from the eslint config, so it has been
off since. Raised in
https://github.com/go-gitea/gitea/pull/39829#issuecomment-6246720235.

- Re-enable the rule, exempting e2e tests like unit tests
- Detect tags anywhere in strings and template chunks, not only at the
start
- Allow `htmlRaw` templates, they escape like `html`
- Fix the existing violations, which escapes the heatmap tooltip and
shortcut keys
2026-10-18 07:37:17 +00:00

40 lines
1.1 KiB
TypeScript

// MIT license, Copyright (c) GitHub, Inc.
// https://github.com/github/eslint-plugin-github/blob/main/lib/rules/unescaped-html-literal.js
import type {JSRuleDefinition, JSRuleDefinitionTypeOptions} from 'eslint';
const htmlTag = /<\/?[a-z][\w-]*[\s/>]/i;
const escapingTags = new Set(['html', 'htmlRaw']);
const rule: JSRuleDefinition<JSRuleDefinitionTypeOptions> = {
meta: {
type: 'problem',
messages: {
unescapedHtmlLiteral: 'Unescaped HTML literal. Use html`` tag template literal for secure escaping.',
},
},
create: (context) => ({
Literal(node) {
if (typeof node.value !== 'string' || !htmlTag.test(node.value)) return;
context.report({
node,
messageId: 'unescapedHtmlLiteral',
});
},
TemplateLiteral(node) {
if (node.quasis.every((quasi) => !htmlTag.test(quasi.value.raw))) return;
const parent = node.parent;
if (parent?.type === 'TaggedTemplateExpression' && parent.tag.type === 'Identifier' && escapingTags.has(parent.tag.name)) return;
context.report({
node,
messageId: 'unescapedHtmlLiteral',
});
},
}),
};
export default rule;