Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

57 lines
2.9KB

  1. # Domain rules: what may be stored, and how project scope is resolved.
  2. $script:FkeKnowledgeTypes = @(
  3. 'technical-fact', 'lesson-learned', 'decision', 'procedure',
  4. 'error-solution', 'project-memory', 'preference', 'episode'
  5. )
  6. # Heuristic guard only. It catches common credential shapes; it cannot prove content is safe.
  7. $script:FkeSecretPatterns = @(
  8. @{ Name = 'private key block'; Pattern = '-----BEGIN [A-Z ]*PRIVATE KEY-----' }
  9. @{ Name = 'AWS access key id'; Pattern = '\bAKIA[0-9A-Z]{16}\b' }
  10. @{ Name = 'GitHub token'; Pattern = '\bgh[pousr]_[A-Za-z0-9]{30,}\b' }
  11. @{ Name = 'API key (sk-...)'; Pattern = '\bsk-[A-Za-z0-9_-]{20,}\b' }
  12. @{ Name = 'credentials in a URL'; Pattern = '://[^/\s:@]+:[^/\s@]+@' }
  13. @{ Name = 'password/secret/token assignment'; Pattern = '(?i)\b(password|passwd|pwd|secret|api[_-]?key|access[_-]?token|auth[_-]?token)\b\s*[:=]\s*["'']?[^\s"'']{6,}' }
  14. )
  15. function Test-FkeSecret([string]$Text) {
  16. foreach ($p in $script:FkeSecretPatterns) { if ($Text -match $p.Pattern) { return $p.Name } }
  17. return $null
  18. }
  19. function Assert-FkeEntryRules {
  20. param(
  21. [string]$Title, [string]$Content, [string]$Type, [string]$Status, [string]$Source,
  22. [switch]$Partial # Update: only validate what was supplied
  23. )
  24. if (-not $Partial -or $PSBoundParameters.ContainsKey('Title')) {
  25. if ([string]::IsNullOrWhiteSpace($Title)) { throw 'Title must not be blank.' }
  26. if ($Title.Length -gt 300) { throw 'Title is longer than 300 characters; state the point in a short title and put detail in Content.' }
  27. }
  28. if (-not $Partial -or $PSBoundParameters.ContainsKey('Content')) {
  29. if ([string]::IsNullOrWhiteSpace($Content)) { throw 'Content must not be blank.' }
  30. }
  31. if ($Type -and $Type -notin $script:FkeKnowledgeTypes) {
  32. throw "Unknown knowledge type '$Type'. Use one of: $($script:FkeKnowledgeTypes -join ', ')."
  33. }
  34. foreach ($field in @(@('Title', $Title), @('Content', $Content), @('Source', $Source))) {
  35. $hit = if ($field[1]) { Test-FkeSecret $field[1] }
  36. if ($hit) { throw "Refusing to store: $($field[0]) looks like it contains a credential ($hit). Remove it; secrets must never be stored." }
  37. }
  38. if ($Status -eq 'verified' -and [string]::IsNullOrWhiteSpace($Source)) {
  39. 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."
  40. }
  41. }
  42. function Resolve-FkeProject {
  43. # Explicit value, then FOUNDRY_PROJECT_KEY, then Set-FoundryProjectContext. $null = global.
  44. param([string]$Project, [switch]$Global)
  45. if ($Global) { return $null }
  46. $p = if ($Project) { $Project } elseif ($env:FOUNDRY_PROJECT_KEY) { $env:FOUNDRY_PROJECT_KEY } else { $script:DefaultProject }
  47. if ($p -and $p -notmatch '^[a-z0-9][a-z0-9._-]{0,62}$') {
  48. throw "Invalid project key '$p'. Use lowercase letters, digits, '.', '_' or '-' (for example the project slug)."
  49. }
  50. $p
  51. }

Powered by TurnKey Linux.