fix: resolve code quality and security issues in 5 oldest files#6258
fix: resolve code quality and security issues in 5 oldest files#6258JiayangLai wants to merge 1 commit intoavelino:mainfrom
Conversation
Bug fixes: - Fix nil map panic in IDGenerator (pkg/markdown/convert.go): The 'used' map was never initialized, causing a panic when Put() was called. Added NewIDGenerator() constructor. - Fix redundant mkdirAll logic (main.go): os.MkdirAll already handles existing directories, removed unnecessary os.Stat check. Security fixes: - Add io.LimitReader to prevent memory exhaustion (check-quality/main.go): Added 1MB limit to io.ReadAll calls to prevent DoS via large response bodies. - Add input validation for git refs (check-pr-diff/main.go): Added isValidRef() function to validate GITHUB_BASE_REF and PR_HEAD_SHA environment variables before using in git commands, preventing potential command injection. Code cleanup: - Fix misleading FIXME comment in slug/generator.go: Corrected grammatically incorrect comment and clarified the code intent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Automated Quality ChecksSkipped — this PR does not modify This is expected for maintenance, documentation, or workflow PRs. |
| func isValidRef(ref string) bool { | ||
| if ref == "" { | ||
| return false | ||
| } | ||
| // Only allow alphanumeric, hyphen, underscore, and dot (common in branch names) | ||
| for _, c := range ref { | ||
| if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.') { |
There was a problem hiding this comment.
Bug: The isValidRef() function incorrectly rejects branch names with slashes (/), causing diffs to silently fall back to the main branch as a base.
Severity: HIGH
Suggested Fix
Update the isValidRef() function to allow the forward slash (/) character. Git branch names commonly use slashes for organization (e.g., feature/add-x). Since the script uses exec.Command, which separates arguments and prevents shell injection, this change is safe and will allow the script to correctly handle a common and valid branch naming convention.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: .github/scripts/check-pr-diff/main.go#L206-L212
Potential issue: The `isValidRef()` function is overly restrictive and rejects valid Git
branch names that contain a forward slash (`/`), such as `release/1.0`. When a pull
request targets a branch with a slash, the validation fails, causing the script to
silently fall back to using `main` as the base for comparison. This results in the `git
diff` command running against an incorrect base branch, leading to inaccurate PR
validation checks. The check may approve changes that should be flagged or reject valid
changes because it is not comparing against the correct code history.
Did we get this right? 👍 / 👎 to inform future reviews.
Summary
This PR fixes code quality and security issues in the 5 oldest code files in the repository:
Bug Fixes
pkg/markdown/convert.go - Fix nil map panic in
IDGeneratorusedmap was never initialized, causing a panic whenPut()was calledNewIDGenerator()constructor to properly initialize the mapmain.go - Fix redundant
mkdirAlllogicos.MkdirAllalready handles the case where the directory existsos.StatcheckSecurity Fixes
.github/scripts/check-quality/main.go - Prevent memory exhaustion
io.LimitReader(1<<20)(1MB limit) toio.ReadAllcalls.github/scripts/check-pr-diff/main.go - Prevent command injection
isValidRef()function to validateGITHUB_BASE_REFandPR_HEAD_SHACode Cleanup
Test Plan
go build ./...to verify code compiles🤖 Generated with Claude Code