mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
Credential-access rules match whole lines, so a Python docstring that named ~/.aws/credentials in a `cat` example counted as a CRITICAL read and blocked the mcp-sentinel skill at the default threshold (#817). In .py files, a built-in CRITICAL credential-access match on a comment-only line, or inside a string that is a whole statement such as a docstring, is now HIGH: it still shows, and still blocks under a HIGH threshold. A small tokenizer decides this and keeps CRITICAL wherever it cannot be sure the line never runs: argument, assigned and bracketed strings, f-strings and their replacement fields, code before a trailing comment, files that read __doc__ or use bare CR line breaks, and explicit severity overrides. A lowered finding carries severityContext "python-inert" (JSON, SARIF, `audit rules --format json`), and the dashboard, CLI text output, both audit TUIs, install/update warnings and the markdown report say it was lowered from CRITICAL, so it is not mistaken for a changed rule. Closes #817
174 lines
5.2 KiB
Go
174 lines
5.2 KiB
Go
package audit
|
|
|
|
import (
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// ScanContent scans raw content for security issues and returns findings.
|
|
// filename is used for reporting (e.g. "SKILL.md").
|
|
func ScanContent(content []byte, filename string) []Finding {
|
|
return ScanContentWithRules(content, filename, nil)
|
|
}
|
|
|
|
// ScanContentWithRules scans content using the given rules.
|
|
// If rules is nil, the default global rules are used.
|
|
func ScanContentWithRules(content []byte, filename string, activeRules []rule) []Finding {
|
|
if activeRules == nil {
|
|
var err error
|
|
activeRules, err = Rules()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
activeRules = rulesForFile(activeRules, filename)
|
|
findings := scanWhitespacePadding(content, filename, activeRules)
|
|
// A BOM at the very start is the file's encoding mark, not a hidden character.
|
|
text := utils.TrimBOM(string(content))
|
|
// Admitted scripts need not have an extension. A first-line shebang is
|
|
// code evidence; extensionless notes without one retain prose severity.
|
|
scriptContext := strings.HasPrefix(text, "#!")
|
|
var inert []bool
|
|
var blocks []markdownCodeBlock
|
|
if strings.EqualFold(filepath.Ext(filename), ".md") {
|
|
for _, r := range activeRules {
|
|
if r.usesMetadataContext() {
|
|
blocks = markdownCodeBlocks([]byte(text))
|
|
break
|
|
}
|
|
}
|
|
}
|
|
blockIndex := 0
|
|
lineNum := 0
|
|
for start := 0; start <= len(text); {
|
|
for blockIndex < len(blocks) && lineNum >= blocks[blockIndex].end {
|
|
blockIndex++
|
|
}
|
|
codeContext := scriptContext || (blockIndex < len(blocks) && lineNum >= blocks[blockIndex].start && metadataCodeFence(blocks[blockIndex].language))
|
|
lineNum++
|
|
end := strings.IndexByte(text[start:], '\n')
|
|
var line string
|
|
if end == -1 {
|
|
line = text[start:]
|
|
start = len(text) + 1
|
|
} else {
|
|
line = text[start : start+end]
|
|
start = start + end + 1
|
|
}
|
|
lineLower := ""
|
|
lineLowerReady := false
|
|
for _, r := range activeRules {
|
|
if !rulePrefilterAllows(r, line, &lineLower, &lineLowerReady) {
|
|
continue
|
|
}
|
|
if r.matchesLine(line) {
|
|
sev, sevContext := r.inertSeverity(r.findingSeverity(line, filename, codeContext), &inert, filename, text, lineNum)
|
|
findings = append(findings, Finding{
|
|
Severity: sev,
|
|
SeverityContext: sevContext,
|
|
Pattern: r.Pattern,
|
|
Message: r.Message,
|
|
File: filename,
|
|
Line: lineNum,
|
|
Snippet: strings.TrimSpace(line),
|
|
RuleID: r.ID,
|
|
Analyzer: AnalyzerStatic,
|
|
Category: categoryForPattern(r.Pattern),
|
|
Confidence: r.Confidence,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
return findings
|
|
}
|
|
|
|
// ScanMarkdownContentWithRules scans markdown content, suppresses selected
|
|
// tutorial patterns, and downgrades SDK-style system parameters in code fences.
|
|
func ScanMarkdownContentWithRules(content []byte, filename string, activeRules []rule) []Finding {
|
|
if activeRules == nil {
|
|
var err error
|
|
activeRules, err = Rules()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
findings, _ := scanFileUnified(content, filename, true, activeRules, nil, true, false, false)
|
|
return findings
|
|
}
|
|
|
|
// Match parameter values, not prose after a role label. Other injection rules
|
|
// still scan the entire line and retain their configured severity.
|
|
var mdSystemValueRe = regexp.MustCompile("^(?:[\"'`\\[]|[A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*\\(|[|>][+-]?$|[A-Za-z_][A-Za-z0-9_]*,[ \\t]*(?://.*)?$)")
|
|
|
|
func markdownFindingSeverity(r rule, line string, inCodeFence bool, nextLine string) string {
|
|
if r.Pattern != "prompt-injection" || r.Severity != SeverityCritical || r.severityOverridden {
|
|
return r.Severity
|
|
}
|
|
if !inCodeFence || r.ID != "prompt-injection-1" {
|
|
return r.Severity
|
|
}
|
|
if isMarkdownSystemParameter(line, nextLine) {
|
|
return SeverityHigh
|
|
}
|
|
return r.Severity
|
|
}
|
|
|
|
func isMarkdownSystemParameter(line, nextLine string) bool {
|
|
key, value, ok := strings.Cut(strings.TrimSpace(line), ":")
|
|
if !ok || (key != "system" && key != "System") {
|
|
return false
|
|
}
|
|
value = strings.TrimSpace(value)
|
|
if value == "" {
|
|
// A split argument must continue with code, not a bare role directive.
|
|
value = strings.TrimSpace(nextLine)
|
|
return strings.HasPrefix(value, "\"") || strings.HasPrefix(value, "'") || strings.HasPrefix(value, "[")
|
|
}
|
|
return mdSystemValueRe.MatchString(value)
|
|
}
|
|
|
|
var tutorialSuppressedPatterns = map[string]bool{
|
|
"dynamic-code-exec": true,
|
|
"shell-execution": true,
|
|
"destructive-commands": true,
|
|
"suspicious-fetch": true,
|
|
|
|
"insecure-http": true,
|
|
"escape-obfuscation": true,
|
|
"hidden-unicode": true,
|
|
"fetch-with-pipe": true,
|
|
"untrusted-install": true,
|
|
}
|
|
|
|
func shouldSuppressTutorialExample(pattern, line string, inCodeFence, tutorialPath bool) bool {
|
|
if !tutorialSuppressedPatterns[pattern] {
|
|
return false
|
|
}
|
|
trimmed := strings.TrimSpace(line)
|
|
if trimmed == "" {
|
|
return false
|
|
}
|
|
if inCodeFence || tutorialPath {
|
|
return true
|
|
}
|
|
return mdTutorialMarkerRe.MatchString(trimmed)
|
|
}
|
|
|
|
func isLikelyTutorialPath(path string) bool {
|
|
lower := strings.ToLower(filepath.ToSlash(path))
|
|
parts := strings.Split(lower, "/")
|
|
for _, p := range parts {
|
|
switch p {
|
|
case "reference", "references", "resource", "resources", "template", "templates", "example", "examples":
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|