fix: ensure unique command patterns and trim whitespace in command extraction

This commit is contained in:
Daniel Riccio 2025-07-25 11:51:54 -05:00
parent c4f5c5f97f
commit 83627716b4
No known key found for this signature in database
GPG key ID: FFD5FD825F8E8209
2 changed files with 17 additions and 19 deletions

View file

@ -34,7 +34,21 @@ export const CommandPatternSelector: React.FC<CommandPatternSelectorProps> = ({
// Create a combined list with full command first, then patterns
const allPatterns = useMemo(() => {
const fullCommandPattern: CommandPattern = { pattern: command }
return [fullCommandPattern, ...patterns]
// Create a set to track unique patterns we've already seen
const seenPatterns = new Set<string>()
seenPatterns.add(command) // Add the full command first
// Filter out any patterns that are duplicates or are the same as the full command
const uniquePatterns = patterns.filter((p) => {
if (seenPatterns.has(p.pattern)) {
return false
}
seenPatterns.add(p.pattern)
return true
})
return [fullCommandPattern, ...uniquePatterns]
}, [command, patterns])
const getPatternStatus = (pattern: string): "allowed" | "denied" | "none" => {

View file

@ -51,27 +51,11 @@ function extractFromTokens(tokens: string[], patterns: Set<string>): void {
// Build patterns progressively up to 3 levels
let pattern = mainCmd
patterns.add(pattern)
patterns.add(pattern.trim())
for (let i = 1; i < Math.min(tokens.length, 3); i++) {
const token = tokens[i]
// Stop at flags (starting with -)
if (token.startsWith("-")) break
// Stop at paths (starting with / or ~)
if (token.startsWith("/") || token.startsWith("~")) break
// Stop at file extensions
if (token.includes(".") && /\.\w+$/.test(token)) break
// Stop at colons (like image:tag)
if (token.includes(":")) break
// Stop at dots (like . for current directory)
if (token === ".") break
pattern += ` ${token}`
patterns.add(pattern)
patterns.add(pattern.trim()) // Ensure no trailing whitespace
}
}