summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/lib/unreadable.py
blob: b36ad88088661be6eeded6f5f64f81096128845c (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""What a scan records and says of the directories that it could not read."""

from __future__ import annotations

import os
from pathlib import PurePath
from typing import Final

from .strictjson import JsonMap, is_list, is_map, is_str

_CAUSE: Final = "the scan could not read one or more directories (permission denied)"
UNREADABLE_NEXT: Final = (
    "; take all coverage fields from coverage.json, not from the completion notice: "
    f'completenessCheckOutcome is "not-checkable", because {_CAUSE}'
)
_NOT_READ: Final = "the scan could not read it (permission denied)"


def _at_or_below(path: object, roots: list[PurePath]) -> bool:
    """Whether `path` is a string that lies at or below one of `roots`."""
    return is_str(path) and any(map(PurePath(os.path.normpath(path)).is_relative_to, roots))


def _lies_in(entry: object, roots: list[PurePath]) -> bool:
    """Whether a skipped component has paths, and each lies at or below one of `roots`."""
    paths = entry.get("paths") if is_map(entry) else None
    return is_list(paths) and bool(paths) and all(_at_or_below(path, roots) for path in paths)


def _lowered(coverage: JsonMap) -> JsonMap:
    """The outcome "not-checkable", with the permission cause after any that the workflow gave."""
    earlier = coverage.get("topLevelRejected")
    cause = f"{earlier}; {_CAUSE}" if earlier else _CAUSE
    return {"completenessCheckOutcome": "not-checkable", "topLevelRejected": cause}


def corrected(coverage: JsonMap, meta: JsonMap) -> JsonMap:
    """`coverage`, corrected for the directories that write_scan_meta.py named as unreadable, and
    unchanged where it named none. Each one is a skipped component with a fixed reason, in place of
    the skipped components that lie in it, and no longer counts as unaccounted for. "checked", and a
    "partial" that then lists no directory, become "not-checkable"."""
    revision, scan_root = meta.get("revision"), meta.get("scan_root")
    recorded = revision.get("unreadable_dirs") if is_map(revision) else None
    named = [name for name in recorded if is_str(name)] if is_list(recorded) else []
    if not named:
        return coverage
    roots = [PurePath(start, name) for name in named for start in ("", scan_root) if is_str(start)]
    skipped = coverage.get("skippedComponents")
    kept = [entry for entry in skipped if not _lies_in(entry, roots)] if is_list(skipped) else []
    not_read = [{"name": name, "paths": [name], "reason": _NOT_READ} for name in named]
    listed = coverage.get("unaccountedTopLevelDirs")
    left = [path for path in listed if not _at_or_below(path, roots)] if is_list(listed) else []
    outcome = coverage.get("completenessCheckOutcome")
    lower = outcome == "checked" or (outcome == "partial" and not left)
    return {
        **coverage,
        "skippedComponents": [*not_read, *kept],
        "unaccountedTopLevelDirs": left,
        **(_lowered(coverage) if lower else {}),
    }


def is_lowered(coverage: JsonMap) -> bool:
    """Whether `coverage` has the cause that corrected() gives."""
    cause = coverage.get("topLevelRejected")
    return is_str(cause) and cause.endswith(_CAUSE)