From 421c145b26610e2e51a54c26be81bb2d2a2ebee5 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Wed, 28 Oct 2026 06:58:32 +0000 Subject: [PATCH 1/4] fix(ci): neutralize section markers inside PR report section content A section body containing a literal section start/end marker could truncate its own section or forge/overwrite another check's section the next time the shared PR report comment was split back apart. Rewrite those markers to an inert form before wrapping the content. Co-Authored-By: Christian Byrne Co-Authored-By: Claude Opus 5.5 --- .../upsert-comment-section/action.yaml | 8 +- .../upsert-comment-section-markers.test.ts | 134 ++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 scripts/cicd/upsert-comment-section-markers.test.ts diff --git a/.github/actions/upsert-comment-section/action.yaml b/.github/actions/upsert-comment-section/action.yaml index 8218d05d857..6feb8e68257 100644 --- a/.github/actions/upsert-comment-section/action.yaml +++ b/.github/actions/upsert-comment-section/action.yaml @@ -48,9 +48,15 @@ runs: 'Either section-content or section-content-file is required' ) } - const sectionContent = sectionContentFile + const rawSectionContent = sectionContentFile ? require('node:fs').readFileSync(sectionContentFile, 'utf8') : process.env.INPUT_SECTION_CONTENT + // A literal section marker inside the content would truncate this + // section or forge another one when the comment is split back apart. + const sectionContent = rawSectionContent.replaceAll( + '' + +const script = parseDocument( + readFileSync( + join(ROOT, '.github/actions/upsert-comment-section/action.yaml'), + 'utf8' + ) +).getIn(['runs', 'steps', 0, 'with', 'script']) + +type Comment = { id: number; body: string; user: { login: string } } + +const AsyncFunction: new ( + ...args: string[] +) => (...args: unknown[]) => Promise = Object.getPrototypeOf( + async () => {} +).constructor + +function fakeGitHub() { + const comments: Comment[] = [] + const byId = (id: number) => { + const comment = comments.find((c) => c.id === id) + if (!comment) throw new Error(`No comment ${id}`) + return comment + } + const rest = { + issues: { + listComments: () => {}, + createComment: async ({ body }: { body: string }) => { + comments.push({ + id: comments.length + 1, + body, + user: { login: 'github-actions[bot]' } + }) + }, + updateComment: async ({ + comment_id, + body + }: { + comment_id: number + body: string + }) => { + byId(comment_id).body = body + }, + getComment: async ({ comment_id }: { comment_id: number }) => ({ + data: byId(comment_id) + }), + deleteComment: async () => {} + } + } + return { + comments, + github: { rest, paginate: async () => comments.map((c) => ({ ...c })) } + } +} + +async function upsert( + github: ReturnType['github'], + sectionName: string, + sectionContent: string +) { + await new AsyncFunction( + 'github', + 'context', + 'core', + 'require', + 'process', + String(script) + )( + github, + { repo: { owner: 'owner', repo: 'repo' } }, + { warning: () => {} }, + () => {}, + { + env: { + INPUT_PR_NUMBER: '1', + INPUT_SECTION_NAME: sectionName, + INPUT_SECTION_CONTENT: sectionContent, + INPUT_SECTION_CONTENT_FILE: '', + INPUT_COMMENT_MARKER: COMMENT_MARKER + } + } + ) +} + +const sectionNamesIn = (body: string) => + Array.from(body.matchAll(//g), (m) => m[1]) + +const sectionContentsIn = (body: string) => + Object.fromEntries( + Array.from( + body.matchAll( + /\n([\s\S]*?)\n/g + ), + (m) => [m[1], m[2]] + ) + ) + +describe('upsert-comment-section content containing section markers', () => { + it('leaves only the upserted section and replaces it whole', async () => { + const { comments, github } = fakeGitHub() + + await upsert(github, 'a', 'x') + expect(sectionNamesIn(comments[0].body)).toEqual(['a']) + + await upsert(github, 'a', 'replaced') + expect(comments).toHaveLength(1) + expect(comments[0].body).toBe( + `${COMMENT_MARKER}\n\n\nreplaced\n` + ) + }) + + it('cannot truncate itself or overwrite another section', async () => { + const { comments, github } = fakeGitHub() + const forged = + 'kept fake kept' + + await upsert(github, 'b', 'real b') + await upsert(github, 'z', forged) + await upsert(github, 'c', 'other') + + expect(sectionContentsIn(comments[0].body)).toEqual({ + b: 'real b', + c: 'other', + z: forged.replaceAll('' + + await upsert(github, 'a', `${videoMarker} `) + await upsert(github, 'videos', 'video report', videoMarker) + + expect(comments).toHaveLength(2) + expect(comments[0].body).not.toContain(videoMarker) + expect(comments[0].body).not.toContain('') + expect(sectionNamesIn(comments[0].body)).toEqual(['a']) + }) }) From a2d598a8160500cc2027e747f24ccd28ae2eb8dc Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Wed, 28 Oct 2026 09:23:19 +0000 Subject: [PATCH 3/4] test: use a literal expected value for the escaped section content Co-Authored-By: Claude Opus 5.5 --- scripts/cicd/upsert-comment-section-markers.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/cicd/upsert-comment-section-markers.test.ts b/scripts/cicd/upsert-comment-section-markers.test.ts index bba1abdb058..7f4ad7d0a95 100644 --- a/scripts/cicd/upsert-comment-section-markers.test.ts +++ b/scripts/cicd/upsert-comment-section-markers.test.ts @@ -129,7 +129,7 @@ describe('upsert-comment-section content containing section markers', () => { expect(sectionContentsIn(comments[0].body)).toEqual({ b: 'real b', c: 'other', - z: forged.replaceAll(' fake kept' }) }) From 1a12155aa95c746bf212ab82782177785c7991e6 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Thu, 29 Oct 2026 18:07:43 +0000 Subject: [PATCH 4/4] test: cover payload preservation, unknown openers, and file-input escaping Co-Authored-By: Claude Opus 5.5 --- .../upsert-comment-section-markers.test.ts | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/scripts/cicd/upsert-comment-section-markers.test.ts b/scripts/cicd/upsert-comment-section-markers.test.ts index 7f4ad7d0a95..ff41b256f87 100644 --- a/scripts/cicd/upsert-comment-section-markers.test.ts +++ b/scripts/cicd/upsert-comment-section-markers.test.ts @@ -137,12 +137,49 @@ describe('upsert-comment-section content containing section markers', () => { const { comments, github } = fakeGitHub() const videoMarker = '' - await upsert(github, 'a', `${videoMarker} `) + await upsert( + github, + 'a', + `${videoMarker} ` + ) await upsert(github, 'videos', 'video report', videoMarker) expect(comments).toHaveLength(2) - expect(comments[0].body).not.toContain(videoMarker) - expect(comments[0].body).not.toContain('') - expect(sectionNamesIn(comments[0].body)).toEqual(['a']) + expect(comments[0].body).toBe( + `${COMMENT_MARKER}\n\n\n \n` + ) + }) + + it('escapes markers read through the file input', async () => { + const { comments, github } = fakeGitHub() + + await new AsyncFunction( + 'github', + 'context', + 'core', + 'require', + 'process', + String(script) + )( + github, + { repo: { owner: 'owner', repo: 'repo' } }, + { warning: () => {} }, + () => ({ + readFileSync: () => 'file' + }), + { + env: { + INPUT_PR_NUMBER: '1', + INPUT_SECTION_NAME: 'a', + INPUT_SECTION_CONTENT: '', + INPUT_SECTION_CONTENT_FILE: 'report.md', + INPUT_COMMENT_MARKER: COMMENT_MARKER + } + } + ) + + expect(comments[0].body).toBe( + `${COMMENT_MARKER}\n\n\nfile\n` + ) }) })