|
| 1 | +// @ts-check |
| 2 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 3 | +import { createRequire } from "module"; |
| 4 | + |
| 5 | +const require = createRequire(import.meta.url); |
| 6 | +const { main: createDiscussionMain } = require("./create_discussion.cjs"); |
| 7 | + |
| 8 | +describe("create_discussion body sanitization", () => { |
| 9 | + let mockGithub; |
| 10 | + let mockCore; |
| 11 | + let mockContext; |
| 12 | + let mockExec; |
| 13 | + let originalEnv; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + // Save original environment |
| 17 | + originalEnv = { ...process.env }; |
| 18 | + |
| 19 | + // Mock GitHub API with discussion categories |
| 20 | + mockGithub = { |
| 21 | + rest: {}, |
| 22 | + graphql: vi.fn().mockImplementation((query, variables) => { |
| 23 | + // Handle repository query (fetch categories) |
| 24 | + if (query.includes("discussionCategories")) { |
| 25 | + return Promise.resolve({ |
| 26 | + repository: { |
| 27 | + id: "R_test123", |
| 28 | + discussionCategories: { |
| 29 | + nodes: [ |
| 30 | + { |
| 31 | + id: "DIC_kwDOGFsHUM4BsUn1", |
| 32 | + name: "General", |
| 33 | + slug: "general", |
| 34 | + description: "General discussions", |
| 35 | + }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + }, |
| 39 | + }); |
| 40 | + } |
| 41 | + // Handle create discussion mutation — echo back the body for assertion |
| 42 | + if (query.includes("createDiscussion")) { |
| 43 | + return Promise.resolve({ |
| 44 | + createDiscussion: { |
| 45 | + discussion: { |
| 46 | + id: "D_test456", |
| 47 | + number: 42, |
| 48 | + title: variables.title, |
| 49 | + url: "https://github.com/test-owner/test-repo/discussions/42", |
| 50 | + }, |
| 51 | + }, |
| 52 | + }); |
| 53 | + } |
| 54 | + return Promise.reject(new Error("Unknown GraphQL query")); |
| 55 | + }), |
| 56 | + }; |
| 57 | + |
| 58 | + // Mock Core |
| 59 | + mockCore = { |
| 60 | + info: vi.fn(), |
| 61 | + warning: vi.fn(), |
| 62 | + error: vi.fn(), |
| 63 | + setOutput: vi.fn(), |
| 64 | + }; |
| 65 | + |
| 66 | + // Mock Context |
| 67 | + mockContext = { |
| 68 | + repo: { owner: "test-owner", repo: "test-repo" }, |
| 69 | + runId: 12345, |
| 70 | + payload: { |
| 71 | + repository: { |
| 72 | + html_url: "https://github.com/test-owner/test-repo", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }; |
| 76 | + |
| 77 | + // Mock Exec |
| 78 | + mockExec = { |
| 79 | + exec: vi.fn().mockResolvedValue(0), |
| 80 | + }; |
| 81 | + |
| 82 | + // Set globals |
| 83 | + global.github = mockGithub; |
| 84 | + global.core = mockCore; |
| 85 | + global.context = mockContext; |
| 86 | + global.exec = mockExec; |
| 87 | + |
| 88 | + // Set required environment variables |
| 89 | + process.env.GH_AW_WORKFLOW_NAME = "Test Workflow"; |
| 90 | + process.env.GH_AW_WORKFLOW_ID = "test-workflow"; |
| 91 | + process.env.GH_AW_WORKFLOW_SOURCE_URL = "https://github.com/owner/repo/blob/main/workflow.md"; |
| 92 | + process.env.GITHUB_SERVER_URL = "https://github.com"; |
| 93 | + }); |
| 94 | + |
| 95 | + afterEach(() => { |
| 96 | + // Restore environment by mutating process.env in place |
| 97 | + for (const key of Object.keys(process.env)) { |
| 98 | + if (!(key in originalEnv)) { |
| 99 | + delete process.env[key]; |
| 100 | + } |
| 101 | + } |
| 102 | + Object.assign(process.env, originalEnv); |
| 103 | + vi.clearAllMocks(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should neutralize @mentions in discussion body", async () => { |
| 107 | + const handler = await createDiscussionMain({ max: 5, category: "general" }); |
| 108 | + const result = await handler( |
| 109 | + { |
| 110 | + title: "Test Discussion", |
| 111 | + body: "This was reported by @malicious-user and CC @another-user.", |
| 112 | + }, |
| 113 | + {} |
| 114 | + ); |
| 115 | + |
| 116 | + expect(result.success).toBe(true); |
| 117 | + |
| 118 | + const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); |
| 119 | + expect(createMutationCall).toBeDefined(); |
| 120 | + const body = createMutationCall[1].body; |
| 121 | + // @mentions must be neutralized to backtick form |
| 122 | + expect(body).toContain("`@malicious-user`"); |
| 123 | + expect(body).toContain("`@another-user`"); |
| 124 | + // Raw unescaped @mentions must not appear |
| 125 | + expect(body).not.toMatch(/(?<![`])@malicious-user(?![`])/); |
| 126 | + expect(body).not.toMatch(/(?<![`])@another-user(?![`])/); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should expose hidden markdown link title XPIA payloads in discussion body (closing the XPIA channel)", async () => { |
| 130 | + const handler = await createDiscussionMain({ max: 5, category: "general" }); |
| 131 | + const result = await handler( |
| 132 | + { |
| 133 | + title: "Security Test", |
| 134 | + body: 'Click [here](https://example.com "XPIA hidden payload") for details.', |
| 135 | + }, |
| 136 | + {} |
| 137 | + ); |
| 138 | + |
| 139 | + expect(result.success).toBe(true); |
| 140 | + |
| 141 | + const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); |
| 142 | + expect(createMutationCall).toBeDefined(); |
| 143 | + const body = createMutationCall[1].body; |
| 144 | + // The hidden title must be moved to visible text (closing the XPIA channel) |
| 145 | + // sanitizeContent converts [text](url "hidden") → [text (hidden)](url) |
| 146 | + expect(body).toContain("XPIA hidden payload"); |
| 147 | + // The payload must no longer be in a hidden markdown link title attribute |
| 148 | + expect(body).not.toMatch(/\[.*\]\([^)]*"XPIA hidden payload"[^)]*\)/); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should sanitize body but preserve the footer workflow marker", async () => { |
| 152 | + const handler = await createDiscussionMain({ max: 5, category: "general" }); |
| 153 | + const result = await handler( |
| 154 | + { |
| 155 | + title: "Test Discussion", |
| 156 | + body: "Please notify @someone about this finding.", |
| 157 | + }, |
| 158 | + {} |
| 159 | + ); |
| 160 | + |
| 161 | + expect(result.success).toBe(true); |
| 162 | + |
| 163 | + const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); |
| 164 | + expect(createMutationCall).toBeDefined(); |
| 165 | + const body = createMutationCall[1].body; |
| 166 | + // @mention must be neutralized |
| 167 | + expect(body).toContain("`@someone`"); |
| 168 | + // System-generated footer marker must still be present |
| 169 | + expect(body).toContain("gh-aw-workflow-id"); |
| 170 | + }); |
| 171 | +}); |
0 commit comments