Skip to content

Commit 9cf497b

Browse files
SecAI-Hubclaude
andcommitted
Reduce evaluateTool complexity and use gofmt -s in CI
Extract checkPathConstraints() to lower cyclomatic complexity from 18 to under 15. Update CI format check to use gofmt -s (simplify). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d102119 commit 9cf497b

2 files changed

Lines changed: 33 additions & 29 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
run: go vet ./...
4141

4242
- name: Format check
43-
run: test -z "$(gofmt -l .)"
43+
run: test -z "$(gofmt -s -l .)"
4444

4545
container:
4646
name: Container Build

main.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,36 @@ func validateArgs(params map[string]string, entry ToolEntry) (bool, string) {
316316
// Core evaluation
317317
// ---------------------------------------------------------------------------
318318

319+
// checkPathConstraints validates path parameters against allowlist/denylist rules.
320+
func checkPathConstraints(params map[string]string, entry ToolEntry) (bool, string) {
321+
path, ok := params["path"]
322+
if !ok || path == "" {
323+
return true, ""
324+
}
325+
326+
resolved, err := cleanAndResolvePath(path)
327+
if err != nil {
328+
return false, "invalid path: " + err.Error()
329+
}
330+
331+
for _, denied := range entry.PathsDenylist {
332+
if matchesGlob(resolved, denied) {
333+
return false, "path matches denylist"
334+
}
335+
}
336+
337+
if len(entry.PathsAllowlist) > 0 {
338+
for _, pattern := range entry.PathsAllowlist {
339+
if matchesGlob(resolved, pattern) {
340+
return true, ""
341+
}
342+
}
343+
return false, "path not in allowlist"
344+
}
345+
346+
return true, ""
347+
}
348+
319349
func evaluateTool(req ToolCallRequest) ToolCallResponse {
320350
pol := getPolicy()
321351

@@ -344,38 +374,12 @@ func evaluateTool(req ToolCallRequest) ToolCallResponse {
344374
return ToolCallResponse{Allowed: false, Reason: "tool not in allowlist"}
345375
}
346376

347-
// Validate arguments
348377
if ok, reason := validateArgs(req.Params, *matched); !ok {
349378
return ToolCallResponse{Allowed: false, Reason: reason}
350379
}
351380

352-
// Check path constraints
353-
if path, ok := req.Params["path"]; ok && path != "" {
354-
resolved, err := cleanAndResolvePath(path)
355-
if err != nil {
356-
return ToolCallResponse{Allowed: false, Reason: "invalid path: " + err.Error()}
357-
}
358-
359-
// Check denylist first
360-
for _, denied := range matched.PathsDenylist {
361-
if matchesGlob(resolved, denied) {
362-
return ToolCallResponse{Allowed: false, Reason: "path matches denylist"}
363-
}
364-
}
365-
366-
// Check allowlist
367-
if len(matched.PathsAllowlist) > 0 {
368-
pathAllowed := false
369-
for _, pattern := range matched.PathsAllowlist {
370-
if matchesGlob(resolved, pattern) {
371-
pathAllowed = true
372-
break
373-
}
374-
}
375-
if !pathAllowed {
376-
return ToolCallResponse{Allowed: false, Reason: "path not in allowlist"}
377-
}
378-
}
381+
if ok, reason := checkPathConstraints(req.Params, *matched); !ok {
382+
return ToolCallResponse{Allowed: false, Reason: reason}
379383
}
380384

381385
return ToolCallResponse{Allowed: true}

0 commit comments

Comments
 (0)