sensitivebox 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sensitivebox/__init__.py +10 -0
- sensitivebox/alerts/__init__.py +94 -0
- sensitivebox/alerts/config.py +85 -0
- sensitivebox/alerts/defaults.toml +126 -0
- sensitivebox/alerts/engine.py +624 -0
- sensitivebox/alerts/events.py +87 -0
- sensitivebox/alerts/globs.py +84 -0
- sensitivebox/alerts/model.py +215 -0
- sensitivebox/alerts/mute.py +55 -0
- sensitivebox/alerts/replay.py +99 -0
- sensitivebox/alerts/routing.py +247 -0
- sensitivebox/api/__init__.py +6 -0
- sensitivebox/api/app.py +225 -0
- sensitivebox/api/data.py +109 -0
- sensitivebox/api/host.py +27 -0
- sensitivebox/api/model.py +90 -0
- sensitivebox/api/security.py +89 -0
- sensitivebox/api/server.py +108 -0
- sensitivebox/api/state.py +220 -0
- sensitivebox/api/static/assets/archivo-latin-wdth-normal-DY7AcnAa.woff2 +0 -0
- sensitivebox/api/static/assets/ibm-plex-mono-medium-BpyVxOr_.woff2 +0 -0
- sensitivebox/api/static/assets/index-BH9EydfS.js +10 -0
- sensitivebox/api/static/assets/index-RMruxW54.css +1 -0
- sensitivebox/api/static/assets/mark-on-dark-CpSxozdM.png +0 -0
- sensitivebox/api/static/assets/mark-on-light-DgUAeHTp.png +0 -0
- sensitivebox/api/static/assets/mark-raised-Bg5z6PGs.png +0 -0
- sensitivebox/api/static/index.html +14 -0
- sensitivebox/browser.py +106 -0
- sensitivebox/cli.py +467 -0
- sensitivebox/collector.py +319 -0
- sensitivebox/completion.py +117 -0
- sensitivebox/daemon.py +181 -0
- sensitivebox/data/icons/128.png +0 -0
- sensitivebox/data/icons/256.png +0 -0
- sensitivebox/data/icons/512.png +0 -0
- sensitivebox/data/proart-p16.tar.gz +0 -0
- sensitivebox/data/sensitivebox.desktop.in +8 -0
- sensitivebox/data/sensitivebox.service.in +11 -0
- sensitivebox/doctor.py +322 -0
- sensitivebox/fixtures.py +177 -0
- sensitivebox/history.py +298 -0
- sensitivebox/hwmon/__init__.py +20 -0
- sensitivebox/hwmon/classify.py +74 -0
- sensitivebox/hwmon/discovery.py +139 -0
- sensitivebox/hwmon/ids.py +35 -0
- sensitivebox/hwmon/model.py +78 -0
- sensitivebox/hwmon/normalise.py +42 -0
- sensitivebox/hwmon/reader.py +65 -0
- sensitivebox/inspect.py +175 -0
- sensitivebox/live.py +56 -0
- sensitivebox/notify/__init__.py +7 -0
- sensitivebox/notify/base.py +60 -0
- sensitivebox/notify/desktop.py +84 -0
- sensitivebox/notify/log.py +30 -0
- sensitivebox/operations.py +44 -0
- sensitivebox/output.py +76 -0
- sensitivebox/process.py +25 -0
- sensitivebox/py.typed +0 -0
- sensitivebox/redact.py +85 -0
- sensitivebox/report.py +357 -0
- sensitivebox/runtime.py +313 -0
- sensitivebox/sample.py +84 -0
- sensitivebox/service.py +112 -0
- sensitivebox/simulate.py +220 -0
- sensitivebox/sources/__init__.py +83 -0
- sensitivebox/sources/apps.py +135 -0
- sensitivebox/sources/bluez.py +96 -0
- sensitivebox/sources/bluez_watch.py +214 -0
- sensitivebox/sources/changes.py +121 -0
- sensitivebox/sources/cpu.py +152 -0
- sensitivebox/sources/dbus.py +174 -0
- sensitivebox/sources/logind.py +88 -0
- sensitivebox/sources/memory.py +127 -0
- sensitivebox/sources/net.py +128 -0
- sensitivebox/sources/power_profiles.py +116 -0
- sensitivebox/sources/power_supply.py +70 -0
- sensitivebox/sources/psi.py +78 -0
- sensitivebox/sources/report.py +85 -0
- sensitivebox/sources/wifi.py +90 -0
- sensitivebox/sources/wifi_nm.py +269 -0
- sensitivebox/status.py +205 -0
- sensitivebox/store/__init__.py +5 -0
- sensitivebox/store/changes.py +58 -0
- sensitivebox/store/db.py +423 -0
- sensitivebox/store/events.py +337 -0
- sensitivebox/store/mutes.py +58 -0
- sensitivebox/store/quality.py +51 -0
- sensitivebox/store/read.py +40 -0
- sensitivebox/store/schema.py +194 -0
- sensitivebox/store/status.py +105 -0
- sensitivebox-0.1.0.dist-info/METADATA +424 -0
- sensitivebox-0.1.0.dist-info/RECORD +95 -0
- sensitivebox-0.1.0.dist-info/WHEEL +4 -0
- sensitivebox-0.1.0.dist-info/entry_points.txt +4 -0
- sensitivebox-0.1.0.dist-info/licenses/LICENSE +21 -0
sensitivebox/redact.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Redaction for everything Sensitive Box prints (docs/BIBLE.md, "Command line": private by
|
|
2
|
+
default). Doctor output goes into public bug reports, so it must never carry a hardware address.
|
|
3
|
+
|
|
4
|
+
``redact`` is applied as the last step of rendering, after all the text is put together. It
|
|
5
|
+
removes:
|
|
6
|
+
|
|
7
|
+
* MAC addresses written with ``:`` or ``-`` separators, wherever they appear (free text included,
|
|
8
|
+
such as an ``unavailable (not a number: '…')`` reason);
|
|
9
|
+
* systemd's MAC-based interface names, ``<prefix>x<12 hex digits>`` such as ``wlx001122334455``
|
|
10
|
+
(see systemd.net-naming-scheme(7)).
|
|
11
|
+
|
|
12
|
+
Bluetooth device names can't be recognised in free text, so they are never put into output in the
|
|
13
|
+
first place: ``device_label`` describes a device by its class icon and battery level only. Wi-Fi
|
|
14
|
+
network names are never read.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import re
|
|
18
|
+
from dataclasses import dataclass
|
|
19
|
+
from typing import Any, cast
|
|
20
|
+
|
|
21
|
+
import msgspec
|
|
22
|
+
|
|
23
|
+
from sensitivebox.sources.bluez import BtDevice
|
|
24
|
+
|
|
25
|
+
MAC_MASK = "XX:XX:XX:XX:XX:XX"
|
|
26
|
+
|
|
27
|
+
_MAC_RE = re.compile(
|
|
28
|
+
r"(?<![0-9A-Fa-f])[0-9A-Fa-f]{2}([:-])(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}(?![0-9A-Fa-f])"
|
|
29
|
+
)
|
|
30
|
+
_MAC_IFACE_RE = re.compile(r"\b(en|wl|ww|sl|ib)x[0-9A-Fa-f]{12}\b")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# A space can belong to a path. Unquoted free text is ambiguous, so redact conservatively
|
|
34
|
+
# through the rest of the field/line, stopping only at a structural delimiter.
|
|
35
|
+
_HOME_RE = re.compile(r"(?:/home/|/root/|(?<!\w)~/)[^\r\n\t\"'<>]+")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def redact(text: str) -> str:
|
|
39
|
+
text = _HOME_RE.sub("<home>", text)
|
|
40
|
+
text = _MAC_RE.sub(MAC_MASK, text)
|
|
41
|
+
return _MAC_IFACE_RE.sub(lambda m: f"{m[1]}x<MAC>", text)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def device_label(device: BtDevice) -> str:
|
|
45
|
+
"""A Bluetooth device as its class and battery, never its name or address."""
|
|
46
|
+
battery = f" (battery {device.battery} %)" if device.battery is not None else ""
|
|
47
|
+
return (device.icon or "device") + battery
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass(frozen=True, slots=True)
|
|
51
|
+
class Redactor:
|
|
52
|
+
"""Known private labels supplement pattern redaction for exports."""
|
|
53
|
+
|
|
54
|
+
private: tuple[str, ...] = ()
|
|
55
|
+
|
|
56
|
+
def __call__(self, text: str) -> str:
|
|
57
|
+
for name in self.private:
|
|
58
|
+
if name:
|
|
59
|
+
text = text.replace(name, "<redacted>")
|
|
60
|
+
return redact(text)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def redacted_json(
|
|
64
|
+
value: object, *, private: tuple[str, ...] = (), stable_ids: frozenset[str] = frozenset()
|
|
65
|
+
) -> str:
|
|
66
|
+
"""API/SSE boundary: redact text fields while retaining explicitly known signal IDs.
|
|
67
|
+
|
|
68
|
+
A hardware ID can itself contain a MAC-based interface name. Only typed ID fields whose
|
|
69
|
+
value is in the collector's registry are exempt; labels, reasons and errors never are.
|
|
70
|
+
"""
|
|
71
|
+
redactor = Redactor(private)
|
|
72
|
+
|
|
73
|
+
def clean(item: Any, key: str = "") -> Any:
|
|
74
|
+
if isinstance(item, str):
|
|
75
|
+
if key in {"id", "signal", "sensor_id"} and item in stable_ids:
|
|
76
|
+
return item
|
|
77
|
+
return redactor(item)
|
|
78
|
+
if isinstance(item, dict):
|
|
79
|
+
# API mapping keys are fixed schema/group names, never private labels.
|
|
80
|
+
return {k: clean(v, k) for k, v in cast(dict[str, Any], item).items()}
|
|
81
|
+
if isinstance(item, (list, tuple)):
|
|
82
|
+
return [clean(v) for v in cast(list[Any] | tuple[Any, ...], item)]
|
|
83
|
+
return item
|
|
84
|
+
|
|
85
|
+
return msgspec.json.encode(clean(msgspec.to_builtins(value))).decode()
|
sensitivebox/report.py
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
"""Allowlisted, private diagnostic exports and self-contained machine fixtures."""
|
|
2
|
+
|
|
3
|
+
import io
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
import tarfile
|
|
7
|
+
import time
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from pathlib import Path, PurePosixPath
|
|
10
|
+
from tempfile import TemporaryDirectory
|
|
11
|
+
|
|
12
|
+
from sensitivebox import __version__, sources
|
|
13
|
+
from sensitivebox.fixtures import MachineRoot, Properties
|
|
14
|
+
from sensitivebox.history import log_view
|
|
15
|
+
from sensitivebox.hwmon.ids import bus_id
|
|
16
|
+
from sensitivebox.inspect import doctor_view
|
|
17
|
+
from sensitivebox.output import Document, encode
|
|
18
|
+
from sensitivebox.redact import Redactor
|
|
19
|
+
from sensitivebox.sample import sample
|
|
20
|
+
from sensitivebox.simulate import fixture_opener
|
|
21
|
+
from sensitivebox.sources.apps import app_name, should_sample_apps
|
|
22
|
+
from sensitivebox.sources.bluez import ADAPTER, BATTERY, DEVICE, BluetoothStatus
|
|
23
|
+
from sensitivebox.sources.memory import read_meminfo
|
|
24
|
+
from sensitivebox.sources.psi import read_all_pressure
|
|
25
|
+
from sensitivebox.sources.wifi import WifiStatus
|
|
26
|
+
from sensitivebox.sources.wifi_nm import ACCESS_POINT, WIRELESS
|
|
27
|
+
from sensitivebox.sources.wifi_nm import DEVICE as NM_DEVICE
|
|
28
|
+
from sensitivebox.store.events import Events
|
|
29
|
+
from sensitivebox.store.read import reader
|
|
30
|
+
|
|
31
|
+
MAX_FILE = 65536
|
|
32
|
+
MAX_MEMBERS = 4096
|
|
33
|
+
MAX_BYTES = 16 * 1024 * 1024
|
|
34
|
+
ATTR = re.compile(
|
|
35
|
+
r"(?:(?:temp|fan|in|curr|power|energy|humidity|freq)\d+_(?:input|average|label|min|max|crit|alarm)|pwm\d+)$"
|
|
36
|
+
)
|
|
37
|
+
PROC_FILES = (
|
|
38
|
+
"meminfo",
|
|
39
|
+
"vmstat",
|
|
40
|
+
"stat",
|
|
41
|
+
"uptime",
|
|
42
|
+
"pressure/memory",
|
|
43
|
+
"pressure/cpu",
|
|
44
|
+
"pressure/io",
|
|
45
|
+
"net/wireless",
|
|
46
|
+
"sys/kernel/osrelease",
|
|
47
|
+
)
|
|
48
|
+
POWER_FILES = (
|
|
49
|
+
"type",
|
|
50
|
+
"status",
|
|
51
|
+
"capacity",
|
|
52
|
+
"online",
|
|
53
|
+
"voltage_now",
|
|
54
|
+
"current_now",
|
|
55
|
+
"energy_now",
|
|
56
|
+
"energy_full",
|
|
57
|
+
"energy_full_design",
|
|
58
|
+
"power_now",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(frozen=True, slots=True)
|
|
63
|
+
class Entry:
|
|
64
|
+
name: str
|
|
65
|
+
data: str = ""
|
|
66
|
+
link: str | None = None
|
|
67
|
+
directory: bool = False
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True, slots=True)
|
|
71
|
+
class ReportView:
|
|
72
|
+
output: str
|
|
73
|
+
fixture: bool
|
|
74
|
+
members: int
|
|
75
|
+
omissions: tuple[str, ...]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class Bundle:
|
|
79
|
+
def __init__(self) -> None:
|
|
80
|
+
self.entries: dict[str, Entry] = {}
|
|
81
|
+
self.omissions: list[str] = []
|
|
82
|
+
self.bytes = 0
|
|
83
|
+
|
|
84
|
+
def add(
|
|
85
|
+
self, name: str, data: str = "", *, link: str | None = None, directory: bool = False
|
|
86
|
+
) -> None:
|
|
87
|
+
parts = PurePosixPath(name).parts
|
|
88
|
+
if not parts or name.startswith("/") or ".." in parts:
|
|
89
|
+
raise ValueError("unsafe report member")
|
|
90
|
+
if link and (PurePosixPath(link).is_absolute() or not _safe_link(name, link)):
|
|
91
|
+
raise ValueError("unsafe report link")
|
|
92
|
+
if len(self.entries) >= MAX_MEMBERS or self.bytes + len(data.encode()) > MAX_BYTES:
|
|
93
|
+
raise OSError("report exceeded bounded collection budget")
|
|
94
|
+
self.entries[name] = Entry(name, data, link, directory)
|
|
95
|
+
self.bytes += len(data.encode())
|
|
96
|
+
for parent in PurePosixPath(name).parents:
|
|
97
|
+
if str(parent) != ".":
|
|
98
|
+
self.entries.setdefault(str(parent), Entry(str(parent), directory=True))
|
|
99
|
+
|
|
100
|
+
def copy(self, source: Path, name: str, *, root: Path) -> None:
|
|
101
|
+
try:
|
|
102
|
+
if not source.resolve().is_relative_to(root.resolve()):
|
|
103
|
+
self.omissions.append(f"{name}: link escapes selected root")
|
|
104
|
+
return
|
|
105
|
+
with source.open() as stream:
|
|
106
|
+
data = stream.read(MAX_FILE + 1)
|
|
107
|
+
if len(data) > MAX_FILE:
|
|
108
|
+
self.omissions.append(f"{name}: exceeds file size limit")
|
|
109
|
+
return
|
|
110
|
+
except OSError:
|
|
111
|
+
return # absent/unreadable files stay absent, never filled from the host
|
|
112
|
+
self.add(name, data)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _safe_link(name: str, target: str) -> bool:
|
|
116
|
+
depth = len(PurePosixPath(name).parent.parts)
|
|
117
|
+
for part in PurePosixPath(target).parts:
|
|
118
|
+
depth += -1 if part == ".." else 0 if part == "." else 1
|
|
119
|
+
if depth < 0:
|
|
120
|
+
return False
|
|
121
|
+
return True
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _children(path: Path) -> list[Path]:
|
|
125
|
+
try:
|
|
126
|
+
return sorted(path.iterdir())[:MAX_MEMBERS]
|
|
127
|
+
except OSError:
|
|
128
|
+
return []
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _files(bundle: Bundle, sysfs_root: Path, proc_root: Path, keep_apps: bool) -> list[str]:
|
|
132
|
+
private: list[str] = []
|
|
133
|
+
for part in ("sys", "proc", "dbus", "sys/class/hwmon"):
|
|
134
|
+
bundle.add(part, directory=True)
|
|
135
|
+
for index, chip in enumerate(_children(sysfs_root / "class/hwmon")):
|
|
136
|
+
prefix = f"sys/class/hwmon/hwmon{index}"
|
|
137
|
+
bundle.add(prefix, directory=True)
|
|
138
|
+
for attr in _children(chip):
|
|
139
|
+
if attr.name == "name" or ATTR.fullmatch(attr.name):
|
|
140
|
+
bundle.copy(attr, f"{prefix}/{attr.name}", root=sysfs_root)
|
|
141
|
+
if (chip / "device").is_symlink():
|
|
142
|
+
bus = bus_id(chip)
|
|
143
|
+
destination = f"sys/devices/{bus}"
|
|
144
|
+
bundle.add(destination, directory=True)
|
|
145
|
+
bundle.add(f"{prefix}/device", link=os.path.relpath(destination, prefix))
|
|
146
|
+
for relative in PROC_FILES:
|
|
147
|
+
bundle.copy(proc_root / relative, "proc/" + relative, root=proc_root)
|
|
148
|
+
for supply in _children(sysfs_root / "class/power_supply"):
|
|
149
|
+
for name in POWER_FILES:
|
|
150
|
+
bundle.copy(
|
|
151
|
+
supply / name, f"sys/class/power_supply/{supply.name}/{name}", root=sysfs_root
|
|
152
|
+
)
|
|
153
|
+
for device in _children(sysfs_root / "class/net"):
|
|
154
|
+
for name in ("statistics/rx_bytes", "statistics/tx_bytes", "operstate"):
|
|
155
|
+
bundle.copy(device / name, f"sys/class/net/{device.name}/{name}", root=sysfs_root)
|
|
156
|
+
for device in _children(sysfs_root / "block"):
|
|
157
|
+
if re.fullmatch(r"zram\d+", device.name):
|
|
158
|
+
bundle.copy(device / "mm_stat", f"sys/block/{device.name}/mm_stat", root=sysfs_root)
|
|
159
|
+
for cpu in _children(sysfs_root / "devices/system/cpu"):
|
|
160
|
+
if re.fullmatch(r"cpu\d+", cpu.name):
|
|
161
|
+
for name in ("scaling_cur_freq", "cpuinfo_min_freq", "cpuinfo_max_freq"):
|
|
162
|
+
relative = f"devices/system/cpu/{cpu.name}/cpufreq/{name}"
|
|
163
|
+
bundle.copy(sysfs_root / relative, "sys/" + relative, root=sysfs_root)
|
|
164
|
+
bundle.copy(
|
|
165
|
+
sysfs_root / "class/dmi/id/bios_version", "sys/class/dmi/id/bios_version", root=sysfs_root
|
|
166
|
+
)
|
|
167
|
+
# Only the selected user's app.slice. The exported UID is synthetic and needs no host UID.
|
|
168
|
+
try:
|
|
169
|
+
cgroup = (proc_root / "self/cgroup").read_text()[:MAX_FILE]
|
|
170
|
+
except OSError:
|
|
171
|
+
cgroup = ""
|
|
172
|
+
match = re.search(r"/user-(\d+)\.slice(?:/|$)", cgroup)
|
|
173
|
+
if match and should_sample_apps(
|
|
174
|
+
read_meminfo(proc_root, fixture_opener),
|
|
175
|
+
read_all_pressure(proc_root, fixture_opener).get("memory"),
|
|
176
|
+
):
|
|
177
|
+
uid = match[1]
|
|
178
|
+
relative = f"fs/cgroup/user.slice/user-{uid}.slice/user@{uid}.service/app.slice"
|
|
179
|
+
target = "sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/app.slice"
|
|
180
|
+
bundle.add(
|
|
181
|
+
"proc/self/cgroup", "0::/user.slice/user-1000.slice/user@1000.service/app.slice\n"
|
|
182
|
+
)
|
|
183
|
+
for index, app in enumerate(_children(sysfs_root / relative)):
|
|
184
|
+
name = app.name if keep_apps else f"app-report-app{index}-1.scope"
|
|
185
|
+
if not keep_apps:
|
|
186
|
+
private.extend((app.name, app_name(app.name)))
|
|
187
|
+
bundle.copy(app / "memory.current", f"{target}/{name}/memory.current", root=sysfs_root)
|
|
188
|
+
return private
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _dbus(bundle: Bundle, bt: BluetoothStatus, wifi: WifiStatus) -> list[str]:
|
|
192
|
+
objects: Properties = {}
|
|
193
|
+
private = [a.alias for a in bt.adapters] + [d.alias for d in bt.devices]
|
|
194
|
+
for index, adapter in enumerate(bt.adapters):
|
|
195
|
+
path = f"/org/bluez/hci{index}"
|
|
196
|
+
objects[path] = {ADAPTER: {"Powered": adapter.powered}}
|
|
197
|
+
for number, device in enumerate(d for d in bt.devices if d.adapter == adapter.name):
|
|
198
|
+
device_path = f"{path}/device{number}"
|
|
199
|
+
props: dict[str, str | bool | int] = {"Connected": True, "Adapter": path}
|
|
200
|
+
if device.icon:
|
|
201
|
+
props["Icon"] = device.icon
|
|
202
|
+
objects[device_path] = {DEVICE: props}
|
|
203
|
+
if device.battery is not None:
|
|
204
|
+
objects[device_path][BATTERY] = {"Percentage": device.battery}
|
|
205
|
+
if bt.available:
|
|
206
|
+
bundle.add("dbus/bluez-managed-objects.json", encode(objects))
|
|
207
|
+
if wifi.signal:
|
|
208
|
+
signal = wifi.signal
|
|
209
|
+
ap = "/org/freedesktop/NetworkManager/AccessPoint/1"
|
|
210
|
+
device = "/org/freedesktop/NetworkManager/Devices/1"
|
|
211
|
+
network: dict[str, Properties] = {
|
|
212
|
+
"devices": {
|
|
213
|
+
device: {
|
|
214
|
+
NM_DEVICE: {"DeviceType": 2, "Interface": signal.iface},
|
|
215
|
+
WIRELESS: {"ActiveAccessPoint": ap},
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
"access_points": {ap: {ACCESS_POINT: {"Strength": signal.percent}}},
|
|
219
|
+
}
|
|
220
|
+
bundle.add("dbus/nm-wifi.json", encode(network))
|
|
221
|
+
return private
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _sanitize(bundle: Bundle, private: list[str]) -> Bundle:
|
|
225
|
+
clean = Bundle()
|
|
226
|
+
policy = Redactor(tuple(sorted({p for p in private if p}, key=len, reverse=True)))
|
|
227
|
+
for entry in bundle.entries.values():
|
|
228
|
+
clean.add(
|
|
229
|
+
policy(entry.name),
|
|
230
|
+
policy(entry.data),
|
|
231
|
+
link=policy(entry.link) if entry.link else None,
|
|
232
|
+
directory=entry.directory,
|
|
233
|
+
)
|
|
234
|
+
clean.omissions = [policy(line) for line in bundle.omissions]
|
|
235
|
+
return clean
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _write_machine(bundle: Bundle, root: Path) -> None:
|
|
239
|
+
for entry in sorted(bundle.entries.values(), key=lambda e: (not e.directory, e.name)):
|
|
240
|
+
path = root / entry.name
|
|
241
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
242
|
+
if entry.directory:
|
|
243
|
+
path.mkdir(exist_ok=True)
|
|
244
|
+
elif entry.link is not None:
|
|
245
|
+
path.symlink_to(entry.link)
|
|
246
|
+
else:
|
|
247
|
+
path.write_text(entry.data)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
async def export(
|
|
251
|
+
output: Path,
|
|
252
|
+
sysfs_root: Path,
|
|
253
|
+
proc_root: Path,
|
|
254
|
+
*,
|
|
255
|
+
root: MachineRoot | None = None,
|
|
256
|
+
fixture: bool = False,
|
|
257
|
+
db: Path | None = None,
|
|
258
|
+
keep_app_names: bool = False,
|
|
259
|
+
) -> ReportView:
|
|
260
|
+
# Exclusive creation precedes collection, so an existing report is never touched.
|
|
261
|
+
fd = os.open(output, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
262
|
+
try:
|
|
263
|
+
os.fchmod(fd, 0o600)
|
|
264
|
+
bundle = Bundle()
|
|
265
|
+
private = _files(bundle, sysfs_root, proc_root, keep_app_names)
|
|
266
|
+
bt, wifi = await (root.snapshot() if root is not None else sources.system_bus_snapshot())
|
|
267
|
+
private.extend(_dbus(bundle, bt, wifi))
|
|
268
|
+
bundle = _sanitize(bundle, private)
|
|
269
|
+
with TemporaryDirectory(prefix="sensitivebox-report-") as temporary:
|
|
270
|
+
machine_path = Path(temporary)
|
|
271
|
+
_write_machine(bundle, machine_path)
|
|
272
|
+
machine = MachineRoot.load(machine_path)
|
|
273
|
+
current = await sample(machine.sys, machine.proc, machine)
|
|
274
|
+
view = doctor_view(current, machine.sys, machine.proc, False)
|
|
275
|
+
# Diagnostic output uses synthetic relative roots, never collector paths.
|
|
276
|
+
diagnostic = encode(
|
|
277
|
+
Document("doctor", view), redactor=Redactor(tuple(private))
|
|
278
|
+
).replace(str(machine_path), ".")
|
|
279
|
+
bundle.add("diagnostics.json", Redactor(tuple(private))(diagnostic))
|
|
280
|
+
if db is not None:
|
|
281
|
+
history = log_view(db, 0, 100)
|
|
282
|
+
with reader(db) as connection:
|
|
283
|
+
weekly = Events(connection, time.time, writer=False).weekly() if connection else []
|
|
284
|
+
markers = (
|
|
285
|
+
connection.execute(
|
|
286
|
+
"SELECT ts,kind,value FROM change_markers ORDER BY id DESC LIMIT 101"
|
|
287
|
+
).fetchall()
|
|
288
|
+
if connection
|
|
289
|
+
else []
|
|
290
|
+
)
|
|
291
|
+
content = encode(
|
|
292
|
+
Document(
|
|
293
|
+
"operations",
|
|
294
|
+
{
|
|
295
|
+
"history": history,
|
|
296
|
+
"weekly": weekly[:100],
|
|
297
|
+
"markers": [
|
|
298
|
+
(ts, kind, value if keep_app_names else "<omitted>")
|
|
299
|
+
for ts, kind, value in markers[:100]
|
|
300
|
+
],
|
|
301
|
+
"truncated": history.truncated
|
|
302
|
+
or len(weekly) > 100
|
|
303
|
+
or len(markers) > 100,
|
|
304
|
+
},
|
|
305
|
+
),
|
|
306
|
+
redactor=Redactor(tuple(private)),
|
|
307
|
+
)
|
|
308
|
+
bundle.add("operations.json", Redactor(tuple(private))(content))
|
|
309
|
+
bundle.add(
|
|
310
|
+
"manifest.json",
|
|
311
|
+
encode(
|
|
312
|
+
Document(
|
|
313
|
+
"report",
|
|
314
|
+
{
|
|
315
|
+
"package_version": __version__,
|
|
316
|
+
"fixture": fixture,
|
|
317
|
+
"app_names_kept": keep_app_names,
|
|
318
|
+
"omissions": bundle.omissions,
|
|
319
|
+
"privacy": "allowlisted machine inputs; no database or journal",
|
|
320
|
+
},
|
|
321
|
+
)
|
|
322
|
+
),
|
|
323
|
+
)
|
|
324
|
+
selected = bundle if fixture else Bundle()
|
|
325
|
+
if not fixture:
|
|
326
|
+
for name in ("diagnostics.json", "manifest.json", "operations.json"):
|
|
327
|
+
if name not in bundle.entries:
|
|
328
|
+
continue
|
|
329
|
+
entry = bundle.entries[name]
|
|
330
|
+
selected.add(entry.name, entry.data)
|
|
331
|
+
# TarInfo is constructed from sanitized data only: no host metadata or hard links.
|
|
332
|
+
with os.fdopen(fd, "wb") as stream:
|
|
333
|
+
fd = -1
|
|
334
|
+
with tarfile.open(fileobj=stream, mode="w:gz") as archive:
|
|
335
|
+
policy = Redactor(tuple(private))
|
|
336
|
+
for entry in sorted(selected.entries.values(), key=lambda e: e.name):
|
|
337
|
+
member = tarfile.TarInfo(policy(entry.name))
|
|
338
|
+
member.uid = member.gid = member.mtime = 0
|
|
339
|
+
member.uname = member.gname = ""
|
|
340
|
+
member.mode = 0o700 if entry.directory else 0o600
|
|
341
|
+
if entry.directory:
|
|
342
|
+
member.type = tarfile.DIRTYPE
|
|
343
|
+
archive.addfile(member)
|
|
344
|
+
elif entry.link:
|
|
345
|
+
member.type = tarfile.SYMTYPE
|
|
346
|
+
member.linkname = policy(entry.link)
|
|
347
|
+
archive.addfile(member)
|
|
348
|
+
else:
|
|
349
|
+
data = policy(entry.data).encode()
|
|
350
|
+
member.size = len(data)
|
|
351
|
+
archive.addfile(member, io.BytesIO(data))
|
|
352
|
+
return ReportView(str(output), fixture, len(selected.entries), tuple(bundle.omissions))
|
|
353
|
+
except BaseException:
|
|
354
|
+
if fd >= 0:
|
|
355
|
+
os.close(fd)
|
|
356
|
+
os.unlink(output)
|
|
357
|
+
raise
|