mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-19 16:18:11 +00:00
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
40 lines
1.1 KiB
TypeScript
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;
|