-
Notifications
You must be signed in to change notification settings - Fork 0
fix(security): resolve open CodeQL code-scanning alerts #19
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 |
|---|---|---|
|
|
@@ -6,6 +6,9 @@ on: | |
| pull_request: | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| lint: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package collections | ||
|
|
||
| import "math" | ||
|
|
||
| // SafeAdd returns a + b, saturating at math.MaxInt if the sum would overflow. | ||
| // | ||
| // It is intended for computing make() size or capacity hints from independent | ||
| // lengths (e.g. len(a)+len(b)) without risking an integer-overflow wraparound, | ||
| // which could otherwise yield a tiny allocation followed by out-of-bounds | ||
| // writes. Negative operands are treated as an overflow guard as well. | ||
| func SafeAdd(a, b int) int { | ||
| if a < 0 || b < 0 || a > math.MaxInt-b { | ||
| return math.MaxInt | ||
| } | ||
| return a + b | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,9 +39,9 @@ func Levenshtein(s1, s2 string) int { | |
| return len(s1) | ||
| } | ||
|
|
||
| matrix := make([][]int, len(s1)+1) | ||
| matrix := make([][]int, SafeAdd(len(s1), 1)) | ||
| for i := range matrix { | ||
| matrix[i] = make([]int, len(s2)+1) | ||
| matrix[i] = make([]int, SafeAdd(len(s2), 1)) | ||
|
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: set -euo pipefail
printf '\n== fuzzy.go outline ==\n'
ast-grep outline pkg/collections/fuzzy.go --view expanded || true
printf '\n== fuzzy.go relevant lines ==\n'
cat -n pkg/collections/fuzzy.go | sed -n '1,140p'
printf '\n== SafeAdd definitions/usages ==\n'
rg -n "func SafeAdd|SafeAdd\(" -S .
printf '\n== integer type context ==\n'
sed -n '1,120p' pkg/collections/fuzzy.go | cat -nRepository: flanksource/captain Length of output: 4496 🏁 Script executed: set -euo pipefail
python3 - <<'PY'
from pathlib import Path
p = Path('pkg/collections/fuzzy.go')
text = p.read_text()
print(text)
PY
printf '\n== SafeAdd definition(s) ==\n'
rg -n "func SafeAdd|type .*SafeAdd|SafeAdd =" -S pkg . || trueRepository: flanksource/captain Length of output: 1542 🏁 Script executed: set -euo pipefail
printf '--- fuzzy.go ---\n'
nl -ba pkg/collections/fuzzy.go | sed -n '1,120p'
printf '\n--- SafeAdd references ---\n'
rg -n "SafeAdd" pkg -S
printf '\n--- go.mod ---\n'
cat -n go.mod | sed -n '1,120p'Repository: flanksource/captain Length of output: 248 🏁 Script executed: set -euo pipefail
nl -ba pkg/collections/alloc.go | sed -n '1,120p'
printf '\n== SafeAdd callers in collections ==\n'
rg -n "SafeAdd\(" pkg/collections -S
printf '\n== Any tests for fuzzy/alloc ==\n'
rg -n "Levenshtein|SafeAdd" -S . --glob '*_test.go'Repository: flanksource/captain Length of output: 197 🏁 Script executed: set -euo pipefail
python3 - <<'PY'
from pathlib import Path
p = Path('pkg/collections/alloc.go')
print(p.read_text())
PYRepository: flanksource/captain Length of output: 692 🏁 Script executed: set -euo pipefail
cat -n pkg/collections/alloc.go | sed -n '1,120p'Repository: flanksource/captain Length of output: 803 Use Proposed fix- for j := range len(s2) + 1 {
+ for j := range SafeAdd(len(s2), 1) {🤖 Prompt for AI Agents |
||
| matrix[i][0] = i | ||
| } | ||
| for j := range len(s2) + 1 { | ||
|
|
||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Fix path validation when
baseCwdis a filesystem root.If
baseis a root directory (e.g.,/on Unix orC:\on Windows),filepath.Clean(base)retains the trailing slash. Appendingstring(os.PathSeparator)results in a double slash (e.g.,//orC:\\), which causesstrings.HasPrefixto falsely reject valid subdirectories (e.g.,/foo).Ensure the separator is only appended if
basedoes not already end with one.🛠️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents