File size: 1,045 Bytes
bc2711b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Refuse to ship artifacts that contain token-like strings (HF / OpenAI / generic bearer)."""
from __future__ import annotations

import re
import tarfile
import zipfile
from pathlib import Path

PATTERNS = re.compile(rb"\b(hf_[A-Za-z0-9]{30,}|sk-[A-Za-z0-9_-]{32,}|ghp_[A-Za-z0-9]{36})\b")
MAX_MEMBER = 2_000_000


def find_secrets(path: Path) -> list[str]:
    hits: list[str] = []
    try:
        if zipfile.is_zipfile(path):
            with zipfile.ZipFile(path) as z:
                for i in z.infolist():
                    if i.file_size <= MAX_MEMBER and PATTERNS.search(z.read(i)):
                        hits.append(i.filename)
        elif path.name.endswith((".tar.gz", ".tgz")):
            with tarfile.open(path) as t:
                for m in t.getmembers():
                    if m.isfile() and m.size <= MAX_MEMBER:
                        f = t.extractfile(m)
                        if f and PATTERNS.search(f.read()):
                            hits.append(m.name)
    except Exception:
        pass
    return hits