# Domain rules: what may be stored, and how project scope is resolved. $script:FkeKnowledgeTypes = @( 'technical-fact', 'lesson-learned', 'decision', 'procedure', 'error-solution', 'project-memory', 'preference', 'episode' ) # Heuristic guard only. It catches common credential shapes; it cannot prove content is safe. $script:FkeSecretPatterns = @( @{ Name = 'private key block'; Pattern = '-----BEGIN [A-Z ]*PRIVATE KEY-----' } @{ Name = 'AWS access key id'; Pattern = '\bAKIA[0-9A-Z]{16}\b' } @{ Name = 'GitHub token'; Pattern = '\bgh[pousr]_[A-Za-z0-9]{30,}\b' } @{ Name = 'API key (sk-...)'; Pattern = '\bsk-[A-Za-z0-9_-]{20,}\b' } @{ Name = 'credentials in a URL'; Pattern = '://[^/\s:@]+:[^/\s@]+@' } @{ Name = 'password/secret/token assignment'; Pattern = '(?i)\b(password|passwd|pwd|secret|api[_-]?key|access[_-]?token|auth[_-]?token)\b\s*[:=]\s*["'']?[^\s"'']{6,}' } ) function Test-FkeSecret([string]$Text) { foreach ($p in $script:FkeSecretPatterns) { if ($Text -match $p.Pattern) { return $p.Name } } return $null } function Assert-FkeEntryRules { param( [string]$Title, [string]$Content, [string]$Type, [string]$Status, [string]$Source, [switch]$Partial # Update: only validate what was supplied ) if (-not $Partial -or $PSBoundParameters.ContainsKey('Title')) { if ([string]::IsNullOrWhiteSpace($Title)) { throw 'Title must not be blank.' } if ($Title.Length -gt 300) { throw 'Title is longer than 300 characters; state the point in a short title and put detail in Content.' } } if (-not $Partial -or $PSBoundParameters.ContainsKey('Content')) { if ([string]::IsNullOrWhiteSpace($Content)) { throw 'Content must not be blank.' } } if ($Type -and $Type -notin $script:FkeKnowledgeTypes) { throw "Unknown knowledge type '$Type'. Use one of: $($script:FkeKnowledgeTypes -join ', ')." } foreach ($field in @(@('Title', $Title), @('Content', $Content), @('Source', $Source))) { $hit = if ($field[1]) { Test-FkeSecret $field[1] } if ($hit) { throw "Refusing to store: $($field[0]) looks like it contains a credential ($hit). Remove it; secrets must never be stored." } } if ($Status -eq 'verified' -and [string]::IsNullOrWhiteSpace($Source)) { throw "A 'verified' entry needs a Source (the evidence: a test, a command and its result, a spec section, or documentation). Use status 'unverified' otherwise." } } function Resolve-FkeProject { # Explicit value, then FOUNDRY_PROJECT_KEY, then Set-FoundryProjectContext. $null = global. param([string]$Project, [switch]$Global) if ($Global) { return $null } $p = if ($Project) { $Project } elseif ($env:FOUNDRY_PROJECT_KEY) { $env:FOUNDRY_PROJECT_KEY } else { $script:DefaultProject } if ($p -and $p -notmatch '^[a-z0-9][a-z0-9._-]{0,62}$') { throw "Invalid project key '$p'. Use lowercase letters, digits, '.', '_' or '-' (for example the project slug)." } $p }