-
Notifications
You must be signed in to change notification settings - Fork 230
Add max limit enforcement to add_comment handler (SEC-003) #15745
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,45 @@ const { getMessages } = require("./messages_core.cjs"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** @type {string} Safe output type handled by this module */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const HANDLER_TYPE = "add_comment"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * 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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const mentions = (body.match(/@\w+/g) || []).length; | |
| const mentions = (body.match(/@[a-zA-Z][a-zA-Z0-9-]*/g) || []).length; |
Copilot
AI
Feb 14, 2026
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 error codes used (E006, E007, E008) don't match their definitions in the Safe Outputs specification (docs/src/content/docs/reference/safe-outputs-specification.md:2885-2894). According to the spec:
- E006 is defined as INVALID_LABEL
- E007 is defined as API_ERROR
- E008 is defined as SANITIZATION_FAILED
However, they're being used here for resource limit violations (length exceeded, mentions exceeded, links exceeded). This creates confusion and could cause issues if these error codes are used for their documented purposes elsewhere. Consider either updating the specification to document these as resource limit error codes, or using different error codes (E011-E013) that don't conflict with existing definitions.
| 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}`); | |
| throw new Error(`E011: 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(`E012: 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(`E013: Comment contains ${links} links, maximum is ${MAX_LINKS}`); |
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.
@copilot use mentions parser from the mentions sanitizer