-
Notifications
You must be signed in to change notification settings - Fork 212
Enforce add_comment constraints at MCP server invocation #16011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
16072aa
Initial plan
Copilot bfd4ba3
Add MCP server constraint enforcement for add_comment tool
Copilot eafac4b
Update Safe Outputs Specification to v1.12.0
Copilot 7a311a7
Refactor comment limit validation into separate helper module
Copilot 7073da5
Add changeset [skip-ci]
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // @ts-check | ||
|
|
||
| /** | ||
| * Comment Limit Helpers | ||
| * | ||
| * This module provides validation functions for enforcing comment constraints | ||
| * across both MCP server (Phase 4 - tool invocation) and safe output processor | ||
| * (Phase 6 - API execution) per Safe Outputs Specification MCE1 and MCE4. | ||
| * | ||
| * The limits defined here must match the constraints documented in: | ||
| * - pkg/workflow/js/safe_outputs_tools.json (add_comment tool description) | ||
| * - docs/src/content/docs/reference/safe-outputs-specification.md | ||
| * | ||
| * This ensures constraint consistency per Requirement MCE5. | ||
| */ | ||
|
|
||
| /** | ||
| * Maximum limits for comment parameters to prevent resource exhaustion. | ||
| * These limits align with GitHub's API constraints and security best practices. | ||
| */ | ||
|
|
||
| /** @type {number} Maximum comment body length (GitHub's limit) */ | ||
| const MAX_COMMENT_LENGTH = 65536; | ||
|
|
||
| /** @type {number} Maximum number of mentions allowed per comment */ | ||
| const MAX_MENTIONS = 10; | ||
|
|
||
| /** @type {number} Maximum number of links allowed per comment */ | ||
| const MAX_LINKS = 50; | ||
|
|
||
| /** | ||
| * Enforces maximum limits on comment parameters to prevent resource exhaustion attacks. | ||
| * Per Safe Outputs specification requirement MR3, limits must be enforced before API calls. | ||
| * | ||
| * @param {string} body - Comment body to validate | ||
| * @throws {Error} When any limit is exceeded, with error code and details | ||
| */ | ||
| function enforceCommentLimits(body) { | ||
| // Check body length - max limit exceeded check | ||
| if (body.length > MAX_COMMENT_LENGTH) { | ||
| throw new Error(`E006: Comment body exceeds maximum length of ${MAX_COMMENT_LENGTH} characters (got ${body.length})`); | ||
| } | ||
|
|
||
| // Count mentions (@username pattern) - max limit exceeded check | ||
| const mentions = (body.match(/@\w+/g) || []).length; | ||
| if (mentions > MAX_MENTIONS) { | ||
| throw new Error(`E007: Comment contains ${mentions} mentions, maximum is ${MAX_MENTIONS}`); | ||
| } | ||
|
|
||
| // Count links (http:// and https:// URLs) - max limit exceeded check | ||
| const links = (body.match(/https?:\/\/[^\s]+/g) || []).length; | ||
| if (links > MAX_LINKS) { | ||
| throw new Error(`E008: Comment contains ${links} links, maximum is ${MAX_LINKS}`); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| MAX_COMMENT_LENGTH, | ||
| MAX_MENTIONS, | ||
| MAX_LINKS, | ||
| enforceCommentLimits, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // @ts-check | ||
| import { describe, it, expect } from "vitest"; | ||
|
|
||
| describe("comment_limit_helpers", () => { | ||
| let MAX_COMMENT_LENGTH, MAX_MENTIONS, MAX_LINKS, enforceCommentLimits; | ||
|
|
||
| // Import the module | ||
| beforeEach(async () => { | ||
| const module = await import("./comment_limit_helpers.cjs"); | ||
| MAX_COMMENT_LENGTH = module.MAX_COMMENT_LENGTH; | ||
| MAX_MENTIONS = module.MAX_MENTIONS; | ||
| MAX_LINKS = module.MAX_LINKS; | ||
| enforceCommentLimits = module.enforceCommentLimits; | ||
| }); | ||
|
|
||
| describe("constants", () => { | ||
| it("should export MAX_COMMENT_LENGTH constant", () => { | ||
| expect(MAX_COMMENT_LENGTH).toBe(65536); | ||
| }); | ||
|
|
||
| it("should export MAX_MENTIONS constant", () => { | ||
| expect(MAX_MENTIONS).toBe(10); | ||
| }); | ||
|
|
||
| it("should export MAX_LINKS constant", () => { | ||
| expect(MAX_LINKS).toBe(50); | ||
| }); | ||
| }); | ||
|
|
||
| describe("enforceCommentLimits", () => { | ||
| it("should accept valid comment body", () => { | ||
| const validBody = "This is a valid comment with reasonable length"; | ||
| expect(() => enforceCommentLimits(validBody)).not.toThrow(); | ||
| }); | ||
|
|
||
| it("should accept comment at exactly maximum length", () => { | ||
| const exactBody = "a".repeat(MAX_COMMENT_LENGTH); | ||
| expect(() => enforceCommentLimits(exactBody)).not.toThrow(); | ||
| }); | ||
|
|
||
| it("should reject comment exceeding maximum length", () => { | ||
| const longBody = "a".repeat(MAX_COMMENT_LENGTH + 1); | ||
| expect(() => enforceCommentLimits(longBody)).toThrow(/E006.*maximum length/i); | ||
| }); | ||
|
|
||
| it("should accept comment with exactly maximum mentions", () => { | ||
| const mentions = Array.from({ length: MAX_MENTIONS }, (_, i) => `@user${i}`).join(" "); | ||
| expect(() => enforceCommentLimits(mentions)).not.toThrow(); | ||
| }); | ||
|
|
||
| it("should reject comment exceeding maximum mentions", () => { | ||
| const mentions = Array.from({ length: MAX_MENTIONS + 1 }, (_, i) => `@user${i}`).join(" "); | ||
| expect(() => enforceCommentLimits(mentions)).toThrow(/E007.*mentions/i); | ||
| }); | ||
|
|
||
| it("should accept comment with exactly maximum links", () => { | ||
| const links = Array.from({ length: MAX_LINKS }, (_, i) => `https://example.com/${i}`).join(" "); | ||
| expect(() => enforceCommentLimits(links)).not.toThrow(); | ||
| }); | ||
|
|
||
| it("should reject comment exceeding maximum links", () => { | ||
| const links = Array.from({ length: MAX_LINKS + 1 }, (_, i) => `https://example.com/${i}`).join(" "); | ||
| expect(() => enforceCommentLimits(links)).toThrow(/E008.*links/i); | ||
| }); | ||
|
|
||
| it("should count both http and https links", () => { | ||
| const httpsLinks = Array.from({ length: 30 }, (_, i) => `https://example.com/${i}`).join(" "); | ||
| const httpLinks = Array.from({ length: 21 }, (_, i) => `http://example.org/${i}`).join(" "); | ||
| const mixed = `${httpsLinks} ${httpLinks}`; | ||
| expect(() => enforceCommentLimits(mixed)).toThrow(/E008.*51.*links/i); | ||
| }); | ||
|
|
||
| it("should include actual values in error messages", () => { | ||
| const longBody = "a".repeat(70000); | ||
| try { | ||
| enforceCommentLimits(longBody); | ||
| expect.fail("Should have thrown error"); | ||
| } catch (error) { | ||
| expect(error.message).toContain("E006"); | ||
| expect(error.message).toContain("65536"); // Max length | ||
| expect(error.message).toContain("70000"); // Actual length | ||
| } | ||
| }); | ||
|
|
||
| it("should accept empty string", () => { | ||
| expect(() => enforceCommentLimits("")).not.toThrow(); | ||
| }); | ||
|
|
||
| it("should handle comment with no mentions", () => { | ||
| const body = "Comment without any user mentions"; | ||
| expect(() => enforceCommentLimits(body)).not.toThrow(); | ||
| }); | ||
|
|
||
| it("should handle comment with no links", () => { | ||
| const body = "Comment without any links or URLs"; | ||
| expect(() => enforceCommentLimits(body)).not.toThrow(); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test file uses
beforeEachon line 8 but it's not imported from vitest on line 2. This will cause a runtime error when the tests are executed. AddbeforeEachto the import statement.