| 1 | // Package diffscan applies the pinned Preview scanner's embedded default rules |
| 2 | // to complete bounded text. It never reads repository config, ignore files or |
| 3 | // baselines, and never returns findings or writes scanner output. |
| 4 | package diffscan |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | "github.com/rs/zerolog" |
| 15 | "github.com/spf13/viper" |
| 16 | "github.com/zricethezav/gitleaks/v8/config" |
| 17 | "github.com/zricethezav/gitleaks/v8/detect" |
| 18 | "github.com/zricethezav/gitleaks/v8/detect/codec" |
| 19 | "github.com/zricethezav/gitleaks/v8/logging" |
| 20 | "github.com/zricethezav/gitleaks/v8/sources" |
| 21 | ) |
| 22 | |
| 23 | // Gitleaks' trace logger can contain matches. Disable this library's logger |
| 24 | // permanently, not process logging, before any scan. Findings stay in memory. |
| 25 | func init() { logging.Logger = zerolog.Nop() } |
| 26 | |
| 27 | // Cheap whole-file refusal for URL userinfo containing a nonempty password. |
| 28 | var passwordURL = regexp.MustCompile(`[A-Za-z][A-Za-z0-9+.-]*://[^\s/:@]*:[^\s/@]+@[^\s/]+`) |
| 29 | |
| 30 | var privateHeader = regexp.MustCompile(`-----BEGIN [A-Z ]*PRIVATE KEY-----`) |
| 31 | |
| 32 | var rules = sync.OnceValues(func() (config.Config, error) { |
| 33 | v := viper.New() |
| 34 | v.SetConfigType("toml") |
| 35 | if err := v.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil { |
| 36 | return config.Config{}, err |
| 37 | } |
| 38 | var raw config.ViperConfig |
| 39 | if err := v.Unmarshal(&raw); err != nil { |
| 40 | return config.Config{}, err |
| 41 | } |
| 42 | return raw.Translate() |
| 43 | }) |
| 44 | |
| 45 | func Check(ctx context.Context, text string, known []string) (reason string) { |
| 46 | // A scanner panic is a failed scan, never a clean result. |
| 47 | defer func() { |
| 48 | if recover() != nil { |
| 49 | reason = wire.DiffUnavailable |
| 50 | } |
| 51 | }() |
| 52 | if ctx.Err() != nil { |
| 53 | return wire.DiffUnavailable |
| 54 | } |
| 55 | if len(text) > wire.MaxDiffBlobBytes { |
| 56 | return wire.DiffTooLarge |
| 57 | } |
| 58 | decoded := text |
| 59 | var segments []*codec.EncodedSegment |
| 60 | decoder := codec.NewDecoder() |
| 61 | for depth := 0; depth <= 5; depth++ { |
| 62 | if ctx.Err() != nil { |
| 63 | return wire.DiffUnavailable |
| 64 | } |
| 65 | if passwordURL.MatchString(decoded) || privateHeader.MatchString(decoded) || runnerproto.RedactText(decoded, known) != decoded { |
| 66 | return wire.DiffWithheld |
| 67 | } |
| 68 | decoded, segments = decoder.Decode(decoded, segments) |
| 69 | if len(segments) == 0 { |
| 70 | break |
| 71 | } |
| 72 | } |
| 73 | cfg, err := rules() |
| 74 | if err != nil { |
| 75 | return wire.DiffUnavailable |
| 76 | } |
| 77 | found, err := scanText(ctx, cfg, text) |
| 78 | if err != nil || ctx.Err() != nil { |
| 79 | return wire.DiffUnavailable |
| 80 | } |
| 81 | if found { |
| 82 | return wire.DiffWithheld |
| 83 | } |
| 84 | return "" |
| 85 | } |
| 86 | |
| 87 | // Separate the detector call so failures and panics are exercised at the |
| 88 | // scanner boundary, in addition to configuration failures. |
| 89 | var scanText = func(ctx context.Context, cfg config.Config, text string) (bool, error) { |
| 90 | d := detect.NewDetectorContext(ctx, cfg) |
| 91 | d.IgnoreGitleaksAllow = true |
| 92 | d.MaxDecodeDepth = 5 |
| 93 | d.Redact = 100 |
| 94 | found, err := d.DetectSource(ctx, boundedText(text)) |
| 95 | return len(found) > 0, err |
| 96 | } |
| 97 | |
| 98 | // Yield one complete in-memory fragment, with no path-based exemptions or |
| 99 | // filesystem/archive reader. The caller has already enforced the input bound. |
| 100 | type boundedText string |
| 101 | |
| 102 | func (text boundedText) Fragments(ctx context.Context, yield sources.FragmentsFunc) error { |
| 103 | if err := ctx.Err(); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | return yield(sources.Fragment{Raw: string(text)}, nil) |
| 107 | } |
| 108 | |