Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Catch approval phrases that only render as one (entities, emphasis, odd spaces)

Nguyen Xuan Anf4d411394a59
diff --git a/scripts/triage-post-comment.mjs b/scripts/triage-post-comment.mjs
index f5bac792..13de56fc 100644
--- a/scripts/triage-post-comment.mjs
+++ b/scripts/triage-post-comment.mjs
@@ -39,8 +39,39 @@ export const APPROVAL_PHRASES = Object.freeze([
]);
const APPROVAL_PATTERN = new RegExp(`\\b(?:${APPROVAL_PHRASES.map(phrase => phrase.replace(/ /g, '\\s+')).join('|')})\\b`, 'i');
+const NAMED_ENTITIES = Object.freeze({
+ nbsp: ' ',
+ ensp: ' ',
+ emsp: ' ',
+ thinsp: ' ',
+ shy: '',
+ amp: '&',
+ lt: '<',
+ gt: '>',
+ quot: '"',
+ apos: "'",
+});
+const ENTITY_PATTERN = /&(?:#(\d{1,7})|#[xX]([0-9a-fA-F]{1,6})|([A-Za-z][A-Za-z0-9]{1,31}));?/g;
+
+function decodeEntity(whole, decimal, hexadecimal, name) {
+ if (name !== undefined) return NAMED_ENTITIES[name.toLowerCase()] ?? whole;
+ const codePoint = decimal !== undefined ? Number(decimal) : parseInt(hexadecimal, 16);
+ return codePoint > 0 && codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : whole;
+}
+
+// The text as a reader of the rendered comment sees it: entities decoded, emphasis and code marks gone, and every
+// kind of space a plain space.
+export function normalizeForApprovalCheck(text) {
+ return String(text ?? '')
+ .replace(ENTITY_PATTERN, decodeEntity)
+ .replace(/[­​-‏⁠]/g, '')
+ .replace(/[*_~`\\]/g, '')
+ .replace(/[\s   -    ]+/g, ' ');
+}
+
export function claimsApproval(text) {
- return APPROVAL_PATTERN.test(String(text ?? ''));
+ const original = String(text ?? '');
+ return APPROVAL_PATTERN.test(original) || APPROVAL_PATTERN.test(normalizeForApprovalCheck(original));
}
// Removes every match of the pattern and repeats until nothing is left to remove, so a removal cannot join the
diff --git a/tests/triage-bot-hardening.test.js b/tests/triage-bot-hardening.test.js
index be0f585e..5216f027 100644
--- a/tests/triage-bot-hardening.test.js
+++ b/tests/triage-bot-hardening.test.js
@@ -139,6 +139,17 @@ test('an approval phrase hidden by tags or a zero-width character is still caugh
assert.match(buildCommentBody('LG\u200bTM'), /A maintainer will review this item\./);
});
+test('an approval phrase that only renders as one is still caught', () => {
+ const bypasses = ['sa&#102;e', 'sa&#x66;e', 'Looks&nbsp;good', 'looks&NBSP;good', 'sa**f**e', 's*a*fe', 'look`s` good', 'sa\\fe', 'Looks good', 'Looks good'];
+ for (const bypass of bypasses) {
+ assert.equal(claimsApproval(bypass), true, bypass);
+ const body = buildCommentBody(`Notes: ${bypass} to me. Details follow.`);
+ assert.match(body, /A maintainer will review this item\./, bypass);
+ assert.doesNotMatch(body, /Details follow/, bypass);
+ }
+ assert.equal(claimsApproval('Use R&amp;D notes and &copy; marks; the snake_case name is fine.'), false);
+});
+
test('a harmless sentence is quoted and word boundaries keep similar words out', () => {
const body = buildCommentBody('The change touches the loader and lacks a change note. Please add changes/123.md. The unsafe-looking regex was not run.');
assert.match(body, /> The change touches the loader/);