Fix macOS table cells hiding their text; Bug report button and report relay #26

Merged
noah merged 5 commits from macos-table-and-bug-report into main 2026-11-04 10:20:16 -07:00
18 changed files with 1538 additions and 2 deletions
+1
View File
@@ -159,6 +159,7 @@ jobs:
run: |
cargo test -p alkane-gui --offline
cargo test --workspace --exclude alkane-gui --exclude wx-rs --offline
python3 tools/report-relay/test_relay.py
- name: Microbenchmarks
if: steps.surface.outputs.surface == 'full'
+437
View File
@@ -0,0 +1,437 @@
//! Bug reports and feature requests from the toolbar's Bug report button.
//!
//! The app posts the report to the report relay (see tools/report-relay),
//! which holds the Gitea token and files the issue in blueflare/alkane-toolkit.
//! The app itself holds no secret. A report carries its type, the user's text,
//! what the app knows about itself (version, OS, appearance, language, window
//! size) and, if the user leaves it ticked, the recent activity log with
//! credentials scrubbed. Nothing is stored locally.
use std::io::Write;
use std::process::Stdio;
use std::sync::mpsc::{channel, Receiver};
/// Where reports go. `ALKANE_REPORT_URL` overrides it (tests, staging).
pub const DEFAULT_URL: &str = "https://gitea.bluefla.re/alkane-report/v1/report";
/// Longest report text the relay accepts.
pub const MAX_TEXT_CHARS: usize = 8000;
/// Log sent with a report: at most this many of the newest lines...
const LOG_LINES: usize = 80;
/// ...cut to this many bytes on a line boundary.
const LOG_BYTES: usize = 16 * 1024;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kind {
Bug,
Ui,
Feature,
}
impl Kind {
/// In the order the dialog's type choice lists them.
pub const ALL: [Kind; 3] = [Kind::Bug, Kind::Ui, Kind::Feature];
pub fn label(self) -> &'static str {
match self {
Kind::Bug => "Bug",
Kind::Ui => "UI change",
Kind::Feature => "Feature request",
}
}
/// The wire value, which is also the issue label.
pub fn as_str(self) -> &'static str {
match self {
Kind::Bug => "bug",
Kind::Ui => "ui",
Kind::Feature => "feature",
}
}
}
/// What the app knows about itself, sent with every report.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Context {
pub app_version: String,
pub platform: String,
pub os: String,
pub appearance: String,
pub language: String,
pub window: String,
}
impl Context {
/// The running app's context. `window` is the main window's size.
pub fn current(dark: bool, language: &str, window: (i32, i32)) -> Context {
Context {
app_version: env!("CARGO_PKG_VERSION").to_string(),
platform: format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH),
os: os_version(),
appearance: if dark { "dark" } else { "light" }.to_string(),
language: language.to_string(),
window: format!("{}x{}", window.0, window.1),
}
}
}
/// A human-readable OS name and version, best effort.
fn os_version() -> String {
#[cfg(target_os = "linux")]
{
if let Ok(s) = std::fs::read_to_string("/etc/os-release") {
for line in s.lines() {
if let Some(v) = line.strip_prefix("PRETTY_NAME=") {
return v.trim_matches('"').to_string();
}
}
}
"Linux".to_string()
}
#[cfg(target_os = "macos")]
{
let out = alkane_core::platform::quiet_command("sw_vers")
.arg("-productVersion")
.output();
match out {
Ok(o) if o.status.success() => {
format!("macOS {}", String::from_utf8_lossy(&o.stdout).trim())
}
_ => "macOS".to_string(),
}
}
#[cfg(windows)]
{
let out = alkane_core::platform::quiet_command("cmd")
.args(["/C", "ver"])
.output();
match out {
Ok(o) if o.status.success() => {
let v = String::from_utf8_lossy(&o.stdout).trim().to_string();
if v.is_empty() {
"Windows".to_string()
} else {
v
}
}
_ => "Windows".to_string(),
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
{
std::env::consts::OS.to_string()
}
}
/// Names whose values are secrets: `name=value`, `name: value` and
/// `"name": "value"` have the value replaced.
const SECRET_NAMES: [&str; 12] = [
"password",
"passwd",
"pass",
"pwd",
"secret",
"token",
"apikey",
"api_key",
"authorization",
"cookie",
"psk",
"credential",
];
/// Remove credentials from log text before it leaves the machine: values of
/// secret-looking names, `user:password@` in URLs, and PEM private keys.
/// What is removed becomes `[redacted]`.
pub fn scrub(text: &str) -> String {
let mut out = Vec::new();
let mut in_pem = false;
for line in text.lines() {
if line.contains("-----BEGIN") && line.contains("PRIVATE KEY") {
in_pem = true;
out.push("[redacted]".to_string());
continue;
}
if in_pem {
if line.contains("-----END") {
in_pem = false;
}
continue;
}
out.push(scrub_line(line));
}
out.join("\n")
}
fn scrub_line(line: &str) -> String {
let mut s = scrub_url_userinfo(line);
let lower = s.to_ascii_lowercase();
// Walk every occurrence of every secret name; redact the value after the
// next `=` or `:` (quotes and spaces skipped) up to whitespace, a quote, a
// comma, `&` or `;`.
let mut cuts: Vec<(usize, usize)> = Vec::new();
for name in SECRET_NAMES {
let mut from = 0;
while let Some(pos) = lower[from..].find(name) {
let start = from + pos;
from = start + name.len();
let bytes = s.as_bytes();
let mut i = start + name.len();
// allow a closing quote and spaces between the name and the separator
while i < bytes.len() && (bytes[i] == b'"' || bytes[i] == b'\'' || bytes[i] == b' ') {
i += 1;
}
if i >= bytes.len() || (bytes[i] != b'=' && bytes[i] != b':') {
continue;
}
i += 1;
while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'"' || bytes[i] == b'\'') {
i += 1;
}
let vstart = i;
if name == "authorization" || name == "cookie" {
// "Authorization: Bearer <token>", "Cookie: a=b; c=d": the whole
// rest of the line is the secret.
i = bytes.len();
}
while i < bytes.len()
&& !matches!(bytes[i], b' ' | b'"' | b'\'' | b',' | b'&' | b';' | b'\t')
{
i += 1;
}
if i > vstart {
cuts.push((vstart, i));
}
}
}
if cuts.is_empty() {
return s;
}
cuts.sort();
let mut merged: Vec<(usize, usize)> = Vec::new();
for (a, b) in cuts {
match merged.last_mut() {
Some(last) if a <= last.1 => last.1 = last.1.max(b),
_ => merged.push((a, b)),
}
}
for (a, b) in merged.into_iter().rev() {
if s.is_char_boundary(a) && s.is_char_boundary(b) {
s.replace_range(a..b, "[redacted]");
}
}
s
}
/// `scheme://user:password@host` -> `scheme://user:[redacted]@host`.
fn scrub_url_userinfo(line: &str) -> String {
let mut s = line.to_string();
let mut from = 0;
while let Some(p) = s[from..].find("://") {
let auth_start = from + p + 3;
let rest = &s[auth_start..];
let end = rest
.find(|c: char| c == '/' || c.is_whitespace())
.unwrap_or(rest.len());
let authority = &rest[..end];
if let Some(at) = authority.rfind('@') {
if let Some(colon) = authority[..at].find(':') {
let a = auth_start + colon + 1;
let b = auth_start + at;
s.replace_range(a..b, "[redacted]");
}
}
from = auth_start;
if from >= s.len() {
break;
}
}
s
}
/// The newest activity-log lines, scrubbed and cut to the size limits.
pub fn recent_log(full_log: &str) -> String {
let lines: Vec<&str> = full_log.lines().collect();
let tail = &lines[lines.len().saturating_sub(LOG_LINES)..];
let mut text = scrub(&tail.join("\n"));
if text.len() > LOG_BYTES {
let mut cut = text.len() - LOG_BYTES;
while !text.is_char_boundary(cut) {
cut += 1;
}
text = text[cut..].to_string();
if let Some(nl) = text.find('\n') {
text = text[nl + 1..].to_string();
}
}
text
}
/// The JSON body the relay takes.
pub fn payload(kind: Kind, text: &str, ctx: &Context, log: Option<&str>) -> String {
serde_json::json!({
"kind": kind.as_str(),
"text": text,
"app_version": ctx.app_version,
"platform": ctx.platform,
"os": ctx.os,
"appearance": ctx.appearance,
"language": ctx.language,
"window": ctx.window,
"log": log.unwrap_or(""),
})
.to_string()
}
/// A filed issue: its number and web link.
#[derive(Clone, Debug, PartialEq)]
pub struct Filed {
pub number: u64,
pub url: String,
}
/// Read the relay's answer: curl's body, then the HTTP status on the last line.
pub fn parse_response(curl_stdout: &str) -> Result<Filed, String> {
let trimmed = curl_stdout.trim_end();
let (body, code) = match trimmed.rfind('\n') {
Some(i) => (&trimmed[..i], trimmed[i + 1..].trim()),
None => ("", trimmed),
};
let json: Option<serde_json::Value> = serde_json::from_str(body).ok();
if code == "201" || code == "200" {
let number = json.as_ref().and_then(|j| j["number"].as_u64());
let url = json.as_ref().and_then(|j| j["url"].as_str()).unwrap_or("");
return match number {
Some(number) => Ok(Filed {
number,
url: url.to_string(),
}),
None => Err("the report service answered without an issue number; \
check the issue list before sending again"
.into()),
};
}
let reason = json
.as_ref()
.and_then(|j| j["error"].as_str())
.map(str::to_string);
Err(match (code, reason) {
(_, Some(r)) => r,
("000", None) | ("", None) => "could not reach the report service".into(),
(c, None) => format!("the report service answered HTTP {c}"),
})
}
/// Send a report on a worker thread; the receiver gets one result.
pub fn send(body: String) -> Receiver<Result<Filed, String>> {
let url = std::env::var("ALKANE_REPORT_URL").unwrap_or_else(|_| DEFAULT_URL.to_string());
let (tx, rx) = channel();
std::thread::spawn(move || {
let result = (|| {
let mut child = alkane_core::platform::quiet_command("curl")
.args([
"-sS",
"--max-time",
"40",
"-X",
"POST",
"-H",
"Content-Type: application/json",
"--data-binary",
"@-",
"-w",
"\n%{http_code}",
&url,
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| format!("could not run curl: {e}"))?;
if let Some(mut si) = child.stdin.take() {
let _ = si.write_all(body.as_bytes());
}
let out = child
.wait_with_output()
.map_err(|e| format!("curl failed: {e}"))?;
parse_response(&String::from_utf8_lossy(&out.stdout))
})();
let _ = tx.send(result);
});
rx
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scrub_removes_credentials_and_keeps_the_rest() {
let log = "12:00:01 set pools on 192.168.1.20: url=stratum+tcp://u:hunter2@pool:3333 password=hunter2\n\
12:00:02 login {\"user\": \"root\", \"pass\": \"s3cret\"}\n\
12:00:03 Authorization: Bearer abc.def\n\
-----BEGIN OPENSSH PRIVATE KEY-----\nAAAA\n-----END OPENSSH PRIVATE KEY-----\n\
12:00:04 scan finished: 9 miners";
let s = scrub(log);
assert!(!s.contains("hunter2"), "{s}");
assert!(!s.contains("s3cret"), "{s}");
assert!(!s.contains("abc.def"), "{s}");
assert!(!s.contains("AAAA"), "{s}");
assert!(s.contains("192.168.1.20"));
assert!(s.contains("\"user\": \"root\""));
assert!(s.contains("scan finished: 9 miners"));
assert_eq!(s.matches("[redacted]").count(), 5, "{s}");
}
#[test]
fn recent_log_keeps_the_newest_lines_within_limits() {
let log: Vec<String> = (0..500)
.map(|i| format!("line {i} {}", "x".repeat(300)))
.collect();
let tail = recent_log(&log.join("\n"));
assert!(tail.len() <= LOG_BYTES);
assert!(tail.ends_with(&log[499]));
assert!(tail.starts_with("line "), "cut on a line boundary");
assert!(!tail.contains("line 400 "), "only the newest lines");
}
#[test]
fn payload_carries_kind_text_and_context() {
let ctx = Context {
app_version: "1.2.3".into(),
platform: "macos-aarch64".into(),
os: "macOS 15.1".into(),
appearance: "dark".into(),
language: "en".into(),
window: "1280x800".into(),
};
let v: serde_json::Value =
serde_json::from_str(&payload(Kind::Ui, "Text \"quoted\"", &ctx, None)).unwrap();
assert_eq!(v["kind"], "ui");
assert_eq!(v["text"], "Text \"quoted\"");
assert_eq!(v["app_version"], "1.2.3");
assert_eq!(v["log"], "");
}
#[test]
fn response_parsing() {
assert_eq!(
parse_response("{\"number\":42,\"url\":\"https://x/42\"}\n201"),
Ok(Filed {
number: 42,
url: "https://x/42".into()
})
);
assert_eq!(
parse_response("{\"error\":\"Too many reports; try again later\"}\n429"),
Err("Too many reports; try again later".into())
);
assert_eq!(
parse_response("\n000"),
Err("could not reach the report service".into())
);
assert_eq!(
parse_response("<html>\n502"),
Err("the report service answered HTTP 502".into())
);
}
}
+8 -1
View File
@@ -34,7 +34,7 @@ impl Lang {
Lang::Pt => "Portugu\u{ea}s",
}
}
fn code(self) -> &'static str {
pub fn code(self) -> &'static str {
match self {
Lang::En => "en",
Lang::Zh => "zh",
@@ -137,6 +137,13 @@ const TABLE: &[(&str, [&str; 6])] = &[
("Credentials", ["Credentials", "\u{51ed}\u{636e}", "\u{0423}\u{0447}\u{0451}\u{0442}\u{043d}\u{044b}\u{0435} \u{0434}\u{0430}\u{043d}\u{043d}\u{044b}\u{0435}", "Identifiants", "Credenciales", "Credenciais"]),
("Language", ["Language", "\u{8bed}\u{8a00}", "\u{042f}\u{0437}\u{044b}\u{043a}", "Langue", "Idioma", "Idioma"]),
("Logs", ["Logs", "\u{65e5}\u{5fd7}", "\u{0416}\u{0443}\u{0440}\u{043d}\u{0430}\u{043b}", "Journaux", "Registros", "Registos"]),
("Bug report", ["Bug report", "\u{95ee}\u{9898}\u{53cd}\u{9988}", "\u{421}\u{43e}\u{43e}\u{431}\u{449}\u{438}\u{442}\u{44c} \u{43e}\u{431} \u{43e}\u{448}\u{438}\u{431}\u{43a}\u{435}", "Signaler un bug", "Informar de un error", "Relatar um erro"]),
("Type", ["Type", "\u{7c7b}\u{578b}", "\u{422}\u{438}\u{43f}", "Type", "Tipo", "Tipo"]),
("Bug", ["Bug", "\u{7f3a}\u{9677}", "\u{41e}\u{448}\u{438}\u{431}\u{43a}\u{430}", "Bug", "Error", "Erro"]),
("UI change", ["UI change", "\u{754c}\u{9762}\u{4fee}\u{6539}", "\u{418}\u{437}\u{43c}\u{435}\u{43d}\u{435}\u{43d}\u{438}\u{435} \u{438}\u{43d}\u{442}\u{435}\u{440}\u{444}\u{435}\u{439}\u{441}\u{430}", "Modification de l'interface", "Cambio de interfaz", "Altera\u{e7}\u{e3}o da interface"]),
("Feature request", ["Feature request", "\u{529f}\u{80fd}\u{5efa}\u{8bae}", "\u{41f}\u{440}\u{435}\u{434}\u{43b}\u{43e}\u{436}\u{435}\u{43d}\u{438}\u{435} \u{444}\u{443}\u{43d}\u{43a}\u{446}\u{438}\u{438}", "Demande de fonctionnalit\u{e9}", "Solicitud de funci\u{f3}n", "Pedido de recurso"]),
("Send", ["Send", "\u{53d1}\u{9001}", "\u{41e}\u{442}\u{43f}\u{440}\u{430}\u{432}\u{438}\u{442}\u{44c}", "Envoyer", "Enviar", "Enviar"]),
("Open issue", ["Open issue", "\u{6253}\u{5f00}\u{95ee}\u{9898}", "\u{41e}\u{442}\u{43a}\u{440}\u{44b}\u{442}\u{44c} \u{437}\u{430}\u{434}\u{430}\u{447}\u{443}", "Ouvrir le ticket", "Abrir incidencia", "Abrir issue"]),
// KPI captions
("Total", ["Total", "\u{603b}\u{6570}", "\u{0412}\u{0441}\u{0435}\u{0433}\u{043e}", "Total", "Total", "Total"]),
("Hashing", ["Hashing", "\u{6b63}\u{5728}\u{6316}\u{77ff}", "\u{0425}\u{0435}\u{0448}\u{0438}\u{0440}\u{043e}\u{0432}\u{0430}\u{043d}\u{0438}\u{0435}", "Minage", "Minando", "Minerando"]),
+12
View File
@@ -7,6 +7,7 @@
mod access_probe;
mod askpass;
mod bug_report;
mod credentials;
mod export;
mod fixtures;
@@ -99,6 +100,17 @@ fn run_selftest() -> i32 {
let frame = wx_rs::Widget::main_frame();
let mut app = ui::build(frame);
let ok = app.run_fixture_selftest();
// ALKANE_SNAPSHOT=path (macOS): also write a PNG of the window as the app
// draws it, to check rendering without screen-recording permission.
if let Some(path) = std::env::var_os("ALKANE_SNAPSHOT") {
let path = path.to_string_lossy();
let wrote = wx_rs::snapshot_png(&frame, &path);
println!(
"SNAPSHOT {} {}",
if wrote { "wrote" } else { "unavailable" },
path
);
}
println!("SELFTEST {}", if ok { "PASS" } else { "FAIL" });
if ok {
0
+197
View File
@@ -22,6 +22,12 @@ pub const ID_PRESETS: i32 = 108;
pub const ID_EXPORT: i32 = 109;
// Settings menu (round 15 #13): the toolbar button + its menu items.
pub const ID_SETTINGS: i32 = 110;
// Bug report / feature request dialog (toolbar, between Export and Settings).
pub const ID_BUG_REPORT: i32 = 111;
const ID_REPORT_SEND: i32 = 240;
const ID_REPORT_CLOSE: i32 = 241;
const ID_REPORT_OPEN: i32 = 242;
const ID_REPORT_KIND: i32 = 243;
const ID_LANG_BASE: i32 = 790; // one id per language (790 + Lang index); reserves 790..790+Lang::all().len()
const ID_LOGS_OPEN: i32 = 796; // open the Logs page
const ID_LOGS_REFRESH: i32 = 797;
@@ -791,6 +797,15 @@ pub struct App {
// Settings > Logs page (round 15 #13).
logs_dialog: Widget,
logs_text: Widget,
// Bug report dialog (bug_report.rs): its controls, the send in flight and
// the last filed issue's link.
report_dialog: Widget,
report_kind: Widget,
report_text: Widget,
report_log: Widget,
report_status: Widget,
report_rx: Option<std::sync::mpsc::Receiver<Result<crate::bug_report::Filed, String>>>,
report_url: Option<String>,
// Restore-to-stock (round 12 #8a): background job + its typed-confirm dialog.
restore: Option<restore::RestoreHandle>,
restore_dialog: Widget,
@@ -1024,6 +1039,7 @@ pub fn build(frame: Widget) -> App {
let _filter_btn = tbtn("Filter", ID_FILTER);
let _columns_btn = tbtn("Columns", ID_COLUMNS);
let _export_btn = tbtn("Export", ID_EXPORT);
let _report_btn = tbtn("Bug report", ID_BUG_REPORT);
// The old Credentials button is now a Settings menu (round 15 #13).
let _creds_btn = tbtn("Settings", ID_SETTINGS);
root.add_sizer(ctl, 0, sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT, 4);
@@ -1149,6 +1165,13 @@ pub fn build(frame: Widget) -> App {
logview_dialog: frame,
logs_dialog: frame,
logs_text: frame,
report_dialog: frame,
report_kind: frame,
report_text: frame,
report_log: frame,
report_status: frame,
report_rx: None,
report_url: None,
restore: None,
restore_dialog: frame,
pending_restore: Vec::new(),
@@ -3628,6 +3651,7 @@ impl App {
self.drain_refresh();
self.drain_access_probe();
self.drain_restore();
self.drain_report();
// A completed background log fetch yields a viewer dialog to show; return
// it so `dispatch` presents it modally after the App borrow is released
// (round 18 #12).
@@ -4348,6 +4372,168 @@ impl App {
/// Build (but do not show) the Logs page (round 15 #13): the in-app activity
/// log with Refresh / Clear / Close.
/// The Bug report dialog: a type (Bug, UI change, Feature request), one text
/// box whose first line becomes the issue title, and whether to include the
/// recent activity log (scrubbed). Send files it through the report relay
/// (bug_report.rs) and shows the issue number, or the reason, in the dialog.
fn build_report_dialog(&mut self) -> Widget {
let frame = Widget::main_frame();
let t = crate::i18n::t;
let dlg = Widget::dialog(frame, &t("Bug report"));
let root = Widget::box_sizer(true);
let kindrow = Widget::box_sizer(false);
kindrow.add(
Widget::static_text(dlg, &t("Type")),
0,
sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_RIGHT,
8,
);
let kind = Widget::choice(dlg, ID_REPORT_KIND);
for k in crate::bug_report::Kind::ALL {
kind.choice_append(&t(k.label()));
}
kind.choice_set_selection(0);
kindrow.add(kind, 0, sf::ALIGN_CENTRE_VERTICAL, 0);
root.add_sizer(kindrow, 0, sf::BORDER_ALL, 8);
root.add(
Widget::static_text(
dlg,
&t("Describe the problem or the idea. The first line becomes the title."),
),
0,
sf::BORDER_LEFT | sf::BORDER_RIGHT,
8,
);
let text = Widget::text_ctrl(dlg, true);
text.set_min_size(560, 220);
root.add(text, 1, sf::EXPAND | sf::BORDER_ALL, 8);
let log = Widget::check_box(
dlg,
&t("Include the recent activity log (passwords removed)"),
);
log.set_checked(true);
root.add(log, 0, sf::BORDER_LEFT | sf::BORDER_RIGHT, 8);
root.add(
Widget::static_text(
dlg,
&t("Reports are public issues on gitea.bluefla.re. The app version and OS are included."),
),
0,
sf::BORDER_ALL,
8,
);
let status = Widget::static_text(dlg, "");
root.add(
status,
0,
sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT,
8,
);
let btns = Widget::box_sizer(false);
let open = Widget::button(dlg, &t("Open issue"), ID_REPORT_OPEN);
btns.add(open, 0, sf::BORDER_ALL, 6);
btns.add_stretch(1);
btns.add(
Widget::button(dlg, &t("Close"), ID_REPORT_CLOSE),
0,
sf::BORDER_ALL,
6,
);
btns.add(
Widget::button(dlg, &t("Send"), ID_REPORT_SEND),
0,
sf::BORDER_ALL,
6,
);
root.add_sizer(btns, 0, sf::EXPAND | sf::BORDER_ALL, 6);
root.set_sizer_on(dlg);
dlg.set_min_size(600, 440);
dlg.fit(dlg);
self.report_dialog = dlg;
self.report_kind = kind;
self.report_text = text;
self.report_log = log;
self.report_status = status;
self.report_url = None;
dlg
}
/// Validate the Bug report dialog and send it on a worker thread.
fn send_report(&mut self) {
let t = crate::i18n::t;
if self.report_rx.is_some() {
return; // one send at a time
}
let text = self.report_text.get_value();
let text = text.trim();
if text.is_empty() {
self.report_status
.set_label(&t("Write what happened or what you would like first."));
return;
}
if text.chars().count() > crate::bug_report::MAX_TEXT_CHARS {
self.report_status.set_label(&format!(
"{} ({} / {})",
t("The text is too long"),
text.chars().count(),
crate::bug_report::MAX_TEXT_CHARS
));
return;
}
let kinds = crate::bug_report::Kind::ALL;
let kind = usize::try_from(self.report_kind.choice_get_selection())
.ok()
.and_then(|i| kinds.get(i).copied())
.unwrap_or(crate::bug_report::Kind::Bug);
let size = wx_rs::get_client_size();
let ctx = crate::bug_report::Context::current(
wx_rs::is_dark(),
crate::i18n::current().code(),
(size.width as i32, size.height as i32),
);
let log = self
.report_log
.is_checked()
.then(|| crate::bug_report::recent_log(&crate::logs::dump()));
let body = crate::bug_report::payload(kind, text, &ctx, log.as_deref());
self.report_status.set_label(&t("Sending..."));
self.report_rx = Some(crate::bug_report::send(body));
}
/// Show the Bug report result in the dialog once the send finishes.
fn drain_report(&mut self) {
let Some(rx) = &self.report_rx else { return };
let result = match rx.try_recv() {
Ok(r) => r,
Err(std::sync::mpsc::TryRecvError::Empty) => return,
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
Err("the report was not sent".to_string())
}
};
self.report_rx = None;
let t = crate::i18n::t;
match result {
Ok(filed) => {
crate::logs::log(format!("bug report filed as issue #{}", filed.number));
self.report_status.set_label(&format!(
"{} #{}",
t("Thank you. Filed as issue"),
filed.number
));
self.report_text.set_value("");
self.report_kind.choice_set_selection(0);
if !filed.url.is_empty() {
self.report_url = Some(filed.url);
}
}
Err(e) => {
crate::logs::log(format!("bug report not sent: {e}"));
self.report_status
.set_label(&format!("{} {e}", t("Not sent:")));
}
}
}
fn build_logs_dialog(&mut self) -> Widget {
let frame = Widget::main_frame();
let dlg = Widget::dialog(frame, &crate::i18n::t("Logs"));
@@ -4933,6 +5119,17 @@ impl App {
ID_EXPORT_ALL => self.export_check_all(true),
ID_EXPORT_NONE => self.export_check_all(false),
ID_SETTINGS => self.open_settings_menu(),
ID_BUG_REPORT => return Some(self.build_report_dialog()),
ID_REPORT_SEND => self.send_report(),
ID_REPORT_CLOSE => self.report_dialog.close(0),
ID_REPORT_OPEN => match &self.report_url {
Some(url) => {
wx_rs::launch_browser(url);
}
None => self.report_status.set_label(&crate::i18n::t(
"Send a report first; its issue opens here.",
)),
},
ID_LOGS_REFRESH => self.logs_text.set_value(&crate::logs::dump()),
ID_LOGS_CLEAR => {
crate::logs::clear();
+18
View File
@@ -26,3 +26,21 @@ report them. Try **Check access** and the Credentials dialog.
**The window is too small or too large.** It opens at most as large as your
screen's work area. Use the zoom slider in the status bar to scale the table.
## Reporting a problem
**Bug report** in the toolbar, between Export and Settings, files a bug or a
feature request with the developers. Choose the type (Bug, UI change or
Feature request) and describe it; the first line becomes the title. **Send**
shows the issue number when it is filed, and **Open issue** opens it in the
browser. If it was not sent, the reason is shown and the text stays in the
box.
A report includes the app version, the operating system, the light or dark
appearance, the language and the window size. **Include the recent activity
log** adds the newest lines of the Settings > Logs page, with passwords,
tokens and similar values removed first; untick it to leave the log out.
Reports become public issues on
[gitea.bluefla.re](https://gitea.bluefla.re/blueflare/alkane-toolkit/issues),
so do not put passwords or customer details in the text. A computer can send
five reports an hour.
+1 -1
View File
@@ -17,7 +17,7 @@ PREFIX="${WX_MACOS_PREFIX:-$HOME/.cache/wx-3.3.3-macos}"
export WX_RS_WXCONFIG="$PREFIX/bin/wx-config"
export MACOSX_DEPLOYMENT_TARGET="${MACOSX_MIN:-11.0}"
for target in aarch64-apple-darwin x86_64-apple-darwin; do
cargo build --release --offline --locked -p alkane-gui --target "$target" "$@"
cargo build --release --offline -p alkane-gui --target "$target" "$@"
done
out=target/universal-apple-darwin/release
mkdir -p "$out"
+162
View File
@@ -0,0 +1,162 @@
Subject: [PATCH 0010] macOS: native alternating rows, theme text colour, self-test snapshot
--- a/build.rs
+++ b/build.rs
@@ -278,6 +278,12 @@
build.cpp(true)
.file("cpp_src/wxbridge.cpp")
.file("cpp_src/widgets_bridge.cpp");
+ if is_osx() {
+ // Cocoa calls wx does not wrap (native alternating rows, self-test
+ // snapshot); clang compiles .mm as Objective-C++.
+ println!("cargo:rerun-if-changed=cpp_src/osx_native.mm");
+ build.file("cpp_src/osx_native.mm");
+ }
let mut flags = cxxflags.split_whitespace();
while let Some(f) = flags.next() {
// A universal wx prefix reports `-arch arm64 -arch x86_64`; cargo builds
--- a/cpp_src/widgets_bridge.cpp
+++ b/cpp_src/widgets_bridge.cpp
@@ -597,7 +597,11 @@
} else if (GetAttr().HasBackgroundColour()) {
cellbg = GetAttr().GetBackgroundColour();
} else {
+#ifdef __WXOSX__
+ cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
+#else
cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
+#endif
}
x += DrawShape(dc, shape, x, cell, seg.has_colour ? seg.colour : plain, cellbg);
continue;
@@ -688,6 +692,11 @@
(unsigned char)(c.blue * 255.0 + 0.5));
}
}
+#elif defined(__WXOSX__)
+ // The DC arrives with the control's foreground colour, which is not the
+ // theme-aware label colour native cells use; take the list text colour so
+ // uncoloured segments follow light/dark like the native columns.
+ return wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
#endif
return wxColour();
}
@@ -1463,8 +1472,21 @@
// Set (or clear, when r<0) the alternating-row (zebra) background colour. The
// app passes a theme-aware colour; odd rows get it (see AttrListStore).
+#ifdef __WXOSX__
+extern "C" void wxbr_osx_set_alternating_rows(void* win, int on);
+#endif
void wxbr_dvlc_set_zebra(void* win, int r, int g, int b) {
+#ifdef __WXOSX__
+ // wxOSX draws custom cells without clipping them to the cell, so the
+ // renderer's over-sized background fill (which GTK/MSW clip) painted over
+ // neighbouring cells and hid their text until a row was selected. Use the
+ // table's native alternating rows instead: AppKit paints them under every
+ // cell, in the theme's colours, and no row carries a background attribute.
+ dvlc_store(win)->zebra = wxColour();
+ wxbr_osx_set_alternating_rows(win, r >= 0 ? 1 : 0);
+#else
dvlc_store(win)->zebra = (r < 0) ? wxColour() : wxColour(r, g, b);
+#endif
}
// The model-row index of the first (top) visible row, or -1 if none. Used to
--- a/cpp_src/osx_native.mm
+++ b/cpp_src/osx_native.mm
@@ -0,0 +1,60 @@
+// osx_native.mm -- alkane-toolkit extension: the few wxOSX/Cocoa calls the
+// bridge needs that wx does not wrap. Compiled on macOS only (see build.rs).
+
+#include <wx/wx.h>
+#include <wx/dataview.h>
+
+#import <Cocoa/Cocoa.h>
+
+// The NSTableView (NSOutlineView) behind a wxDataViewCtrl. wx's handle is the
+// enclosing NSScrollView.
+static NSTableView* TableOf(wxWindow* win) {
+ if (!win) return nil;
+ id h = (id)win->GetHandle();
+ if ([h isKindOfClass:[NSTableView class]]) return (NSTableView*)h;
+ if ([h isKindOfClass:[NSScrollView class]]) {
+ id doc = [(NSScrollView*)h documentView];
+ if ([doc isKindOfClass:[NSTableView class]]) return (NSTableView*)doc;
+ }
+ return nil;
+}
+
+extern "C" {
+
+// Native alternating-row backgrounds. AppKit paints them under every cell,
+// custom-drawn ones included, in the theme's own colours (light and dark), so
+// custom renderers never paint row backgrounds on macOS.
+void wxbr_osx_set_alternating_rows(void* win, int on) {
+ NSTableView* tv = TableOf((wxWindow*)win);
+ if (tv) [tv setUsesAlternatingRowBackgroundColors:(on ? YES : NO)];
+}
+
+// Write a PNG of a top-level window's content, rendered by the app itself
+// (no screen-recording permission needed). Used by the self-test with
+// ALKANE_SNAPSHOT=path to check what the table really draws. Returns 1 on
+// success.
+int wxbr_osx_snapshot(void* win, const char* path) {
+ wxWindow* w = (wxWindow*)win;
+ if (!w || !path) return 0;
+ wxTopLevelWindow* tlw = wxDynamicCast(wxGetTopLevelParent(w), wxTopLevelWindow);
+ if (!tlw) return 0;
+ tlw->Show();
+ tlw->Raise();
+ // Let the window lay out and paint before capturing it.
+ for (int i = 0; i < 30; ++i) {
+ wxYield();
+ wxMilliSleep(30);
+ }
+ NSWindow* nsw = (NSWindow*)tlw->GetWXWindow();
+ NSView* view = [nsw contentView];
+ if (!view) return 0;
+ NSRect rect = [view bounds];
+ NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:rect];
+ if (!rep) return 0;
+ [view cacheDisplayInRect:rect toBitmapImageRep:rep];
+ NSData* png = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
+ if (!png) return 0;
+ return [png writeToFile:[NSString stringWithUTF8String:path] atomically:YES] ? 1 : 0;
+}
+
+}
--- a/src/widgets.rs
+++ b/src/widgets.rs
@@ -107,6 +107,8 @@
pub fn wxbr_launch_browser(url: *const c_char) -> c_int;
pub fn wxbr_set_appearance_system();
pub fn wxbr_is_dark() -> c_int;
+ #[cfg(target_os = "macos")]
+ pub fn wxbr_osx_snapshot(win: *mut c_void, path: *const c_char) -> c_int;
pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8);
pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
@@ -271,6 +273,22 @@
unsafe { notsafe::wxbr_is_dark() != 0 }
}
+/// Write a PNG of `win`'s top-level window as the app itself renders it (shows
+/// the window and lets it paint first). macOS only, for the self-test's
+/// `ALKANE_SNAPSHOT`; returns false elsewhere or on failure.
+pub fn snapshot_png(win: &Widget, path: &str) -> bool {
+ #[cfg(target_os = "macos")]
+ {
+ let Ok(p) = CString::new(path) else { return false };
+ unsafe { notsafe::wxbr_osx_snapshot(win.0, p.as_ptr()) != 0 }
+ }
+ #[cfg(not(target_os = "macos"))]
+ {
+ let _ = (win, path);
+ false
+ }
+}
+
/// A theme colour. `which`: 0=window bg, 1=window text, 2=highlight,
/// 3=button face, 4=grey text. Returns (r, g, b).
pub fn system_color(which: i32) -> (u8, u8, u8) {
+1
View File
@@ -28,6 +28,7 @@ later changes go in new patches.
| 0007 | `wxbridge.cpp` on wxMSW: no input-swallowing overlay, appearance before the first window, work-area sizing, executable icon, UTF-8 menu fix |
| 0008 | Widget bridge on wxMSW: generic data-view input and tooltips, DIP boundary, anti-aliased shapes, themed selection colours |
| 0009 | macOS: wxOSX (universal static) through `wx-config`, logical sizes on Apple, clang compiler runtime for `@available` |
| 0010 | macOS table: native alternating rows instead of the unclipped custom-cell background fill (it hid neighbouring cells' text), theme-aware text colour, `osx_native.mm`, self-test window snapshot |
History: the round 19 health-tooltip fix (GTK tip area converted from
bin-window to widget coordinates) used to be a separate patch on top of an
+6
View File
@@ -278,6 +278,12 @@ fn build_with_wx_config() -> Result<(), Box<dyn Error>> {
build.cpp(true)
.file("cpp_src/wxbridge.cpp")
.file("cpp_src/widgets_bridge.cpp");
if is_osx() {
// Cocoa calls wx does not wrap (native alternating rows, self-test
// snapshot); clang compiles .mm as Objective-C++.
println!("cargo:rerun-if-changed=cpp_src/osx_native.mm");
build.file("cpp_src/osx_native.mm");
}
let mut flags = cxxflags.split_whitespace();
while let Some(f) = flags.next() {
// A universal wx prefix reports `-arch arm64 -arch x86_64`; cargo builds
@@ -0,0 +1,60 @@
// osx_native.mm -- alkane-toolkit extension: the few wxOSX/Cocoa calls the
// bridge needs that wx does not wrap. Compiled on macOS only (see build.rs).
#include <wx/wx.h>
#include <wx/dataview.h>
#import <Cocoa/Cocoa.h>
// The NSTableView (NSOutlineView) behind a wxDataViewCtrl. wx's handle is the
// enclosing NSScrollView.
static NSTableView* TableOf(wxWindow* win) {
if (!win) return nil;
id h = (id)win->GetHandle();
if ([h isKindOfClass:[NSTableView class]]) return (NSTableView*)h;
if ([h isKindOfClass:[NSScrollView class]]) {
id doc = [(NSScrollView*)h documentView];
if ([doc isKindOfClass:[NSTableView class]]) return (NSTableView*)doc;
}
return nil;
}
extern "C" {
// Native alternating-row backgrounds. AppKit paints them under every cell,
// custom-drawn ones included, in the theme's own colours (light and dark), so
// custom renderers never paint row backgrounds on macOS.
void wxbr_osx_set_alternating_rows(void* win, int on) {
NSTableView* tv = TableOf((wxWindow*)win);
if (tv) [tv setUsesAlternatingRowBackgroundColors:(on ? YES : NO)];
}
// Write a PNG of a top-level window's content, rendered by the app itself
// (no screen-recording permission needed). Used by the self-test with
// ALKANE_SNAPSHOT=path to check what the table really draws. Returns 1 on
// success.
int wxbr_osx_snapshot(void* win, const char* path) {
wxWindow* w = (wxWindow*)win;
if (!w || !path) return 0;
wxTopLevelWindow* tlw = wxDynamicCast(wxGetTopLevelParent(w), wxTopLevelWindow);
if (!tlw) return 0;
tlw->Show();
tlw->Raise();
// Let the window lay out and paint before capturing it.
for (int i = 0; i < 30; ++i) {
wxYield();
wxMilliSleep(30);
}
NSWindow* nsw = (NSWindow*)tlw->GetWXWindow();
NSView* view = [nsw contentView];
if (!view) return 0;
NSRect rect = [view bounds];
NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:rect];
if (!rep) return 0;
[view cacheDisplayInRect:rect toBitmapImageRep:rep];
NSData* png = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
if (!png) return 0;
return [png writeToFile:[NSString stringWithUTF8String:path] atomically:YES] ? 1 : 0;
}
}
@@ -597,7 +597,11 @@ class SegmentRenderer : public wxDataViewCustomRenderer {
} else if (GetAttr().HasBackgroundColour()) {
cellbg = GetAttr().GetBackgroundColour();
} else {
#ifdef __WXOSX__
cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
#else
cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
#endif
}
x += DrawShape(dc, shape, x, cell, seg.has_colour ? seg.colour : plain, cellbg);
continue;
@@ -688,6 +692,11 @@ class SegmentRenderer : public wxDataViewCustomRenderer {
(unsigned char)(c.blue * 255.0 + 0.5));
}
}
#elif defined(__WXOSX__)
// The DC arrives with the control's foreground colour, which is not the
// theme-aware label colour native cells use; take the list text colour so
// uncoloured segments follow light/dark like the native columns.
return wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
#endif
return wxColour();
}
@@ -1463,8 +1472,21 @@ void wxbr_dvlc_set_row_color(void* win, unsigned row, int r, int g, int b) {
// Set (or clear, when r<0) the alternating-row (zebra) background colour. The
// app passes a theme-aware colour; odd rows get it (see AttrListStore).
#ifdef __WXOSX__
extern "C" void wxbr_osx_set_alternating_rows(void* win, int on);
#endif
void wxbr_dvlc_set_zebra(void* win, int r, int g, int b) {
#ifdef __WXOSX__
// wxOSX draws custom cells without clipping them to the cell, so the
// renderer's over-sized background fill (which GTK/MSW clip) painted over
// neighbouring cells and hid their text until a row was selected. Use the
// table's native alternating rows instead: AppKit paints them under every
// cell, in the theme's colours, and no row carries a background attribute.
dvlc_store(win)->zebra = wxColour();
wxbr_osx_set_alternating_rows(win, r >= 0 ? 1 : 0);
#else
dvlc_store(win)->zebra = (r < 0) ? wxColour() : wxColour(r, g, b);
#endif
}
// The model-row index of the first (top) visible row, or -1 if none. Used to
+18
View File
@@ -107,6 +107,8 @@ mod notsafe {
pub fn wxbr_launch_browser(url: *const c_char) -> c_int;
pub fn wxbr_set_appearance_system();
pub fn wxbr_is_dark() -> c_int;
#[cfg(target_os = "macos")]
pub fn wxbr_osx_snapshot(win: *mut c_void, path: *const c_char) -> c_int;
pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8);
pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
@@ -271,6 +273,22 @@ pub fn is_dark() -> bool {
unsafe { notsafe::wxbr_is_dark() != 0 }
}
/// Write a PNG of `win`'s top-level window as the app itself renders it (shows
/// the window and lets it paint first). macOS only, for the self-test's
/// `ALKANE_SNAPSHOT`; returns false elsewhere or on failure.
pub fn snapshot_png(win: &Widget, path: &str) -> bool {
#[cfg(target_os = "macos")]
{
let Ok(p) = CString::new(path) else { return false };
unsafe { notsafe::wxbr_osx_snapshot(win.0, p.as_ptr()) != 0 }
}
#[cfg(not(target_os = "macos"))]
{
let _ = (win, path);
false
}
}
/// A theme colour. `which`: 0=window bg, 1=window text, 2=highlight,
/// 3=button face, 4=grey text. Returns (r, g, b).
pub fn system_color(which: i32) -> (u8, u8, u8) {
+7
View File
@@ -0,0 +1,7 @@
# Report relay for the Alkane Toolkit's Bug report button (see README.md).
FROM python:3.13-alpine
RUN adduser -D -H -u 10001 relay
COPY relay.py /app/relay.py
USER relay
EXPOSE 8080
ENTRYPOINT ["python3", "-u", "/app/relay.py"]
+65
View File
@@ -0,0 +1,65 @@
# Report relay
Files the app's bug reports and feature requests as issues in
`blueflare/alkane-toolkit`. The app's **Bug report** button posts the report
here; the relay holds the only token, so the public app download carries no
secret.
```
app --HTTPS--> gitea.bluefla.re/alkane-report/ (nginx) --> relay --> Gitea API
```
## What it does
- `POST /v1/report` takes the JSON the app sends (`kind`, `text`, version and
OS context, optional `log`) and answers `201 {"number", "url"}` or an error
`{"error": "..."}` the app shows as is. `GET /healthz` answers `200`.
- Checks: type is `bug`, `ui` or `feature`; text 1 to 8,000 characters;
each context value at most 120 printable characters; log at most 16 KiB;
body at most 64 KiB (nginx enforces the same).
- Rate limits, in memory: 5 reports per client IP per hour and 60 in all.
Over the limit: `429` with `Retry-After`. At most 4 reports in flight.
The client IP is `X-Real-IP` from nginx. Office LAN clients reach nginx
through the router's hairpin NAT, so they share one IP and one limit.
- Scrubs the log a second time (the app scrubs first): password, token,
secret and similar values, `Authorization`/`Cookie` values, URL passwords
and PEM private keys become `[redacted]`.
- `@name` in a report cannot notify anyone (a zero-width space follows `@`).
- The issue title is the text's first line (up to 100 characters). The body
has the type, a context table, the text, and the log in a collapsed block.
Labels: the type (`bug`, `ui`, `feature`) and `from-app`.
- Nothing is stored. Each report leaves one log line: the client IP and the
outcome (`filed #N`, `refused <status>`, `rate limited`, `tracker error`),
never the report's text.
## Gitea account
Issues are filed as `alkane-reporter`, a local non-admin user with a random
password that is never used. Its only access is the org team
`alkane-reporters` (units `repo.code: read`, `repo.issues: write`) on
`alkane-toolkit`. Its token `alkane-toolkit-bug-reports` has the scopes
`write:issue` and `read:repository`. This mirrors FLARE's `flare-reporter`.
## Deployment
On the Gitea host, recorded in the `flare-deployment` repository
(`runbooks/alkane-report-relay.md`):
```sh
# in /opt/flare/deploy/alkane-report-relay, holding relay.py, Dockerfile, compose.yml
PROXY_NETWORK=flare_default docker compose -p alkane-report-relay up -d --build
```
The token file is `secrets/gitea_token` next to the compose file (directory
`0700`, never committed). nginx routes `location /alkane-report/` in the
`gitea.bluefla.re` server block to `alkane-report-relay:8080`.
## Tests
```sh
python3 tools/report-relay/test_relay.py
```
They run the real HTTP handler against a fake Gitea: filing, scrubbing,
mention handling, validation, size and rate limits, and tracker errors. CI
runs them in the `verify` workflow's unit tests.
+26
View File
@@ -0,0 +1,26 @@
# Deployed on the Gitea host next to the shared nginx (see README.md).
# The token file is created on the host and never committed.
services:
report-relay:
build: .
image: alkane-report-relay:local
container_name: alkane-report-relay
restart: unless-stopped
read_only: true
cap_drop: [ALL]
security_opt: ["no-new-privileges:true"]
mem_limit: 128m
pids_limit: 64
environment:
GITEA_URL: http://gitea:3000
GITEA_REPO: blueflare/alkane-toolkit
GITEA_TOKEN_FILE: /run/secrets/gitea_token
RATE_PER_IP: "5"
RATE_GLOBAL: "60"
volumes:
- ./secrets/gitea_token:/run/secrets/gitea_token:ro
networks: [proxy]
networks:
proxy:
external: true
name: ${PROXY_NETWORK:?set PROXY_NETWORK to the shared nginx network}
+321
View File
@@ -0,0 +1,321 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Report relay: files Alkane Toolkit bug reports as Gitea issues.
The app's Bug report button posts JSON here; the relay checks it, scrubs the
log again, rate-limits, and files one issue in one repository with its own
token. The app holds no secret. Nothing about a report is stored.
POST /v1/report {"kind": "bug"|"ui"|"feature", "text": "...",
"app_version", "platform", "os", "appearance",
"language", "window": "...", "log": "..."}
-> 201 {"number": N, "url": "..."}
-> 4xx/5xx {"error": "reason the app shows"}
GET /healthz -> 200 ok
Configuration (environment):
GITEA_URL API base, e.g. http://gitea:3000 (default https://gitea.bluefla.re)
GITEA_REPO owner/name the issues go to (default blueflare/alkane-toolkit)
GITEA_TOKEN_FILE file holding the token (scopes write:issue, read:repository)
LISTEN host:port (default 0.0.0.0:8080)
RATE_PER_IP reports per client IP per hour (default 5)
RATE_GLOBAL reports per hour in all (default 60)
The client IP is the X-Real-IP header the reverse proxy sets; the relay must
only be reachable through that proxy.
"""
from collections import deque
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import os
import re
import sys
import threading
import time
import urllib.error
import urllib.request
KINDS = {"bug": "Bug", "ui": "UI change", "feature": "Feature request"}
CONTEXT_FIELDS = (
("app_version", "App version"),
("platform", "Platform"),
("os", "OS"),
("appearance", "Appearance"),
("language", "Language"),
("window", "Window"),
)
FROM_APP_LABEL = "from-app"
MAX_BODY = 64 * 1024
MAX_TEXT_CHARS = 8000
MAX_CONTEXT_CHARS = 120
MAX_LOG_BYTES = 16 * 1024
MAX_TITLE_CHARS = 100
MAX_IN_FLIGHT = 4
HOUR = 3600.0
SECRET_NAME = re.compile(
r"(password|passwd|pass|pwd|secret|token|apikey|api_key|psk|credential)"
r"(['\"]?\s*[:=]\s*['\"]?)([^\s'\",&;]+)",
re.IGNORECASE,
)
HEADER_SECRET = re.compile(r"((?:authorization|cookie)\s*:\s*).+", re.IGNORECASE)
URL_USERINFO = re.compile(r"(://[^/\s:@]+:)[^/\s@]+(@)")
PEM_BLOCK = re.compile(
r"-----BEGIN [A-Z ]*PRIVATE KEY-----.*?(-----END [A-Z ]*PRIVATE KEY-----|\Z)",
re.DOTALL,
)
def scrub(text: str) -> str:
"""Remove credentials (the app scrubs too; this is the second pass)."""
text = PEM_BLOCK.sub("[redacted]", text)
text = HEADER_SECRET.sub(r"\1[redacted]", text)
text = URL_USERINFO.sub(r"\1[redacted]\2", text)
return SECRET_NAME.sub(r"\1\2[redacted]", text)
def no_mentions(text: str) -> str:
"""Stop @name in a report from notifying Gitea users."""
return re.sub(r"@(?=[A-Za-z0-9_])", "@\u200b", text)
def fence_for(text: str) -> str:
"""A backtick fence longer than any backtick run in `text`."""
longest = max((len(m) for m in re.findall(r"`+", text)), default=0)
return "`" * max(3, longest + 1)
def cell(value: str) -> str:
return value.replace("|", "\\|").replace("\n", " ")
class BadReport(Exception):
def __init__(self, status: int, message: str):
super().__init__(message)
self.status = status
self.message = message
def validate(raw: bytes) -> dict:
try:
data = json.loads(raw)
except (ValueError, UnicodeDecodeError):
raise BadReport(400, "The report was not valid JSON")
if not isinstance(data, dict):
raise BadReport(400, "The report was not a JSON object")
kind = data.get("kind", "bug")
if kind not in KINDS:
raise BadReport(422, "Unknown report type")
text = data.get("text")
if not isinstance(text, str) or not text.strip():
raise BadReport(422, "The report is empty")
text = text.strip()
if len(text) > MAX_TEXT_CHARS:
raise BadReport(413, f"The text is longer than {MAX_TEXT_CHARS} characters")
context = {}
for key, _ in CONTEXT_FIELDS:
value = data.get(key, "")
if not isinstance(value, str) or len(value) > MAX_CONTEXT_CHARS:
raise BadReport(422, f"Bad value for {key}")
context[key] = "".join(c for c in value if c.isprintable())
log = data.get("log", "")
if not isinstance(log, str) or len(log.encode()) > MAX_LOG_BYTES:
raise BadReport(413, "The log is too large")
return {"kind": kind, "text": text, "context": context, "log": log}
def issue_title(report: dict) -> str:
first = next(line.strip() for line in report["text"].splitlines() if line.strip())
if len(first) > MAX_TITLE_CHARS:
first = first[: MAX_TITLE_CHARS - 3].rstrip() + "..."
return no_mentions(first)
def issue_body(report: dict) -> str:
ctx = report["context"]
lines = [
f"**Type:** {KINDS[report['kind']]}",
"",
"Sent from the Alkane Toolkit app's Bug report button.",
"",
"| | |",
"|---|---|",
]
for key, label in CONTEXT_FIELDS:
if ctx.get(key):
lines.append(f"| {label} | {cell(ctx[key])} |")
lines += ["", "---", "", no_mentions(report["text"])]
log = scrub(report["log"]).strip()
if log:
fence = fence_for(log)
lines += [
"",
"<details><summary>Recent activity log</summary>",
"",
fence,
no_mentions(log),
fence,
"",
"</details>",
]
return "\n".join(lines) + "\n"
class RateLimiter:
"""Sliding one-hour windows, per client IP and in all."""
def __init__(self, per_ip: int, total: int, clock=time.monotonic):
self.per_ip, self.total, self.clock = per_ip, total, clock
self.by_ip: dict[str, deque] = {}
self.all: deque = deque()
self.lock = threading.Lock()
def take(self, ip: str) -> float:
"""0 if the report may go now, else seconds until it may."""
now = self.clock()
with self.lock:
for q in [self.all, *self.by_ip.values()]:
while q and now - q[0] >= HOUR:
q.popleft()
self.by_ip = {k: q for k, q in self.by_ip.items() if q}
mine = self.by_ip.setdefault(ip, deque())
if len(mine) >= self.per_ip:
return HOUR - (now - mine[0])
if len(self.all) >= self.total:
return HOUR - (now - self.all[0])
mine.append(now)
self.all.append(now)
return 0.0
class Gitea:
def __init__(self, url: str, repo: str, token: str, timeout: float = 20):
self.api = url.rstrip("/") + "/api/v1/repos/" + repo
self.token, self.timeout = token, timeout
self.labels: dict[str, int] = {}
self.labels_at = 0.0
def _call(self, method: str, path: str, body: dict | None = None):
data = json.dumps(body).encode() if body is not None else None
req = urllib.request.Request(self.api + path, data=data, method=method)
req.add_header("Authorization", f"token {self.token}")
req.add_header("Accept", "application/json")
if data is not None:
req.add_header("Content-Type", "application/json")
with urllib.request.urlopen(req, timeout=self.timeout) as resp:
return json.load(resp)
def label_ids(self, names: list[str]) -> list[int]:
"""IDs of the repository's labels by name (cached 10 minutes);
unknown names are skipped."""
if time.monotonic() - self.labels_at > 600:
try:
found = self._call("GET", "/labels?limit=50")
self.labels = {l["name"]: l["id"] for l in found}
self.labels_at = time.monotonic()
except (urllib.error.URLError, OSError, ValueError, KeyError):
pass
return [self.labels[n] for n in names if n in self.labels]
def file(self, title: str, body: str, labels: list[str]) -> tuple[int, str]:
issue = self._call("POST", "/issues", {
"title": title,
"body": body,
"labels": self.label_ids(labels),
})
return int(issue["number"]), str(issue["html_url"])
class Relay:
def __init__(self, gitea: Gitea, limiter: RateLimiter, log=print):
self.gitea, self.limiter, self.log = gitea, limiter, log
self.slots = threading.BoundedSemaphore(MAX_IN_FLIGHT)
def handle(self, ip: str, raw: bytes) -> tuple[int, dict, dict]:
"""Return (status, json body, extra headers)."""
try:
report = validate(raw)
except BadReport as bad:
self.log(f"report from {ip}: refused {bad.status} ({bad.message})")
return bad.status, {"error": bad.message}, {}
wait = self.limiter.take(ip)
if wait > 0:
self.log(f"report from {ip}: rate limited")
return 429, {"error": "Too many reports; try again later"}, {
"Retry-After": str(int(wait) + 1)}
if not self.slots.acquire(blocking=False):
return 503, {"error": "The report service is busy; try again shortly"}, {}
try:
number, url = self.gitea.file(
issue_title(report), issue_body(report), [report["kind"], FROM_APP_LABEL])
except (urllib.error.URLError, OSError, ValueError, KeyError) as e:
self.log(f"report from {ip}: tracker error ({type(e).__name__})")
return 502, {"error": "The issue tracker did not answer; check the issue "
"list before sending again"}, {}
finally:
self.slots.release()
self.log(f"report from {ip}: filed #{number}")
return 201, {"number": number, "url": url}, {}
def make_handler(relay: Relay):
class Handler(BaseHTTPRequestHandler):
server_version = "alkane-report-relay"
sys_version = ""
def _send(self, status: int, body: dict, headers: dict | None = None):
data = json.dumps(body).encode()
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
for k, v in (headers or {}).items():
self.send_header(k, v)
self.end_headers()
self.wfile.write(data)
def do_GET(self):
if self.path == "/healthz":
self._send(200, {"status": "ok"})
else:
self._send(404, {"error": "Not found"})
def do_POST(self):
if self.path != "/v1/report":
return self._send(404, {"error": "Not found"})
try:
length = int(self.headers.get("Content-Length", ""))
except ValueError:
return self._send(411, {"error": "Length required"})
if length < 0 or length > MAX_BODY:
return self._send(413, {"error": "The report is too large"})
raw = self.rfile.read(length)
ip = self.headers.get("X-Real-IP") or self.client_address[0]
status, body, headers = relay.handle(ip, raw)
self._send(status, body, headers)
def log_message(self, fmt, *args):
pass # one line per report comes from Relay.handle instead
return Handler
def main() -> None:
token_file = os.environ.get("GITEA_TOKEN_FILE", "/run/secrets/gitea_token")
with open(token_file) as fh:
token = fh.read().strip()
if not token:
sys.exit(f"empty token in {token_file}")
gitea = Gitea(os.environ.get("GITEA_URL", "https://gitea.bluefla.re"),
os.environ.get("GITEA_REPO", "blueflare/alkane-toolkit"), token)
limiter = RateLimiter(int(os.environ.get("RATE_PER_IP", "5")),
int(os.environ.get("RATE_GLOBAL", "60")))
relay = Relay(gitea, limiter, log=lambda m: print(m, flush=True))
host, _, port = os.environ.get("LISTEN", "0.0.0.0:8080").rpartition(":")
server = ThreadingHTTPServer((host or "0.0.0.0", int(port)), make_handler(relay))
print(f"report relay on {host}:{port} -> {gitea.api}", flush=True)
server.serve_forever()
if __name__ == "__main__":
main()
+176
View File
@@ -0,0 +1,176 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Tests for the report relay: python3 tools/report-relay/test_relay.py"""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import pathlib
import sys
import threading
import unittest
import urllib.error
import urllib.request
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
import relay # noqa: E402
TOKEN = "test-token-123"
class FakeGitea:
"""Records issue creations; serves the repository's labels."""
def __init__(self, fail=False):
self.created = []
self.fail = fail
outer = self
class H(BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def _json(self, status, body):
data = json.dumps(body).encode()
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_GET(self):
assert self.headers["Authorization"] == f"token {TOKEN}"
if self.path.startswith("/api/v1/repos/blueflare/alkane-toolkit/labels"):
return self._json(200, [{"name": "bug", "id": 1}, {"name": "ui", "id": 2},
{"name": "feature", "id": 3},
{"name": "from-app", "id": 9}])
self._json(404, {})
def do_POST(self):
assert self.headers["Authorization"] == f"token {TOKEN}"
if outer.fail:
return self._json(500, {"message": "boom"})
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
outer.created.append((self.path, body))
n = len(outer.created)
self._json(201, {"number": n,
"html_url": f"https://gitea.example/blueflare/alkane-toolkit/issues/{n}"})
self.server = ThreadingHTTPServer(("127.0.0.1", 0), H)
threading.Thread(target=self.server.serve_forever, daemon=True).start()
self.url = f"http://127.0.0.1:{self.server.server_address[1]}"
def close(self):
self.server.shutdown()
self.server.server_close()
class RelayTests(unittest.TestCase):
def setUp(self):
self.gitea = FakeGitea()
self.now = [1000.0]
self.limiter = relay.RateLimiter(2, 3, clock=lambda: self.now[0])
self.logs = []
r = relay.Relay(relay.Gitea(self.gitea.url, "blueflare/alkane-toolkit", TOKEN),
self.limiter, log=self.logs.append)
self.server = ThreadingHTTPServer(("127.0.0.1", 0), relay.make_handler(r))
threading.Thread(target=self.server.serve_forever, daemon=True).start()
self.url = f"http://127.0.0.1:{self.server.server_address[1]}"
def tearDown(self):
self.server.shutdown()
self.server.server_close()
self.gitea.close()
def post(self, body, ip="198.51.100.7", path="/v1/report", raw=None):
data = raw if raw is not None else json.dumps(body).encode()
req = urllib.request.Request(self.url + path, data=data, method="POST",
headers={"Content-Type": "application/json",
"X-Real-IP": ip})
try:
with urllib.request.urlopen(req, timeout=10) as resp:
return resp.status, json.load(resp), resp.headers
except urllib.error.HTTPError as e:
return e.code, json.load(e), e.headers
def report(self, **over):
body = {"kind": "bug", "text": "Table is blank on macOS\nSteps: open the app.",
"app_version": "0.110.0", "platform": "macos-aarch64", "os": "macOS 15.1",
"appearance": "dark", "language": "en", "window": "1280x800", "log": ""}
body.update(over)
return body
def test_files_an_issue_with_title_body_and_labels(self):
status, body, _ = self.post(self.report(kind="feature", log="12:00 scan done"))
self.assertEqual(status, 201)
self.assertEqual(body["number"], 1)
self.assertTrue(body["url"].endswith("/issues/1"))
path, issue = self.gitea.created[0]
self.assertEqual(path, "/api/v1/repos/blueflare/alkane-toolkit/issues")
self.assertEqual(issue["title"], "Table is blank on macOS")
self.assertEqual(issue["labels"], [3, 9])
self.assertIn("**Type:** Feature request", issue["body"])
self.assertIn("| OS | macOS 15.1 |", issue["body"])
self.assertIn("Steps: open the app.", issue["body"])
self.assertIn("<details><summary>Recent activity log</summary>", issue["body"])
self.assertIn("filed #1", self.logs[-1])
self.assertNotIn("blank", " ".join(self.logs), "report text is never logged")
def test_scrubs_the_log_and_neutralises_mentions(self):
log = ("set pool url=stratum+tcp://w:hunter2@pool:3333 password=hunter2\n"
"Authorization: Bearer abc.def\n``` fence ```")
status, _, _ = self.post(self.report(text="@admin please look", log=log))
self.assertEqual(status, 201)
_, issue = self.gitea.created[0]
self.assertNotIn("hunter2", issue["body"])
self.assertNotIn("abc.def", issue["body"])
self.assertNotIn("@admin", issue["body"])
self.assertNotIn("@admin", issue["title"])
self.assertIn("````\n", issue["body"], "fence longer than the log's own")
def test_rejects_bad_reports(self):
cases = [
(self.report(text=" "), 422),
(self.report(kind="hack"), 422),
(self.report(text="x" * 8001), 413),
(self.report(log="y" * (16 * 1024 + 1)), 413),
(self.report(os="z" * 121), 422),
(self.report(os=5), 422),
([1, 2], 400),
]
for body, want in cases:
status, answer, _ = self.post(body)
self.assertEqual(status, want, body if not isinstance(body, dict) else answer)
self.assertIn("error", answer)
status, _, _ = self.post(None, raw=b"{not json")
self.assertEqual(status, 400)
status, _, _ = self.post(None, raw=b"x" * (relay.MAX_BODY + 1))
self.assertEqual(status, 413)
status, _, _ = self.post(self.report(), path="/v1/other")
self.assertEqual(status, 404)
self.assertEqual(self.gitea.created, [])
def test_rate_limits_per_ip_and_in_all(self):
for _ in range(2):
self.assertEqual(self.post(self.report())[0], 201)
status, body, headers = self.post(self.report())
self.assertEqual(status, 429)
self.assertGreater(int(headers["Retry-After"]), 0)
self.assertEqual(self.post(self.report(), ip="203.0.113.9")[0], 201)
self.assertEqual(self.post(self.report(), ip="203.0.113.10")[0], 429, "global cap of 3")
self.now[0] += relay.HOUR
self.assertEqual(self.post(self.report())[0], 201, "window slides")
def test_tracker_failure_is_reported(self):
self.gitea.fail = True
status, body, _ = self.post(self.report())
self.assertEqual(status, 502)
self.assertIn("check the issue list", body["error"])
def test_title_is_cut(self):
title = relay.issue_title({"text": "\n\n" + "w" * 300})
self.assertEqual(len(title), relay.MAX_TITLE_CHARS)
self.assertTrue(title.endswith("..."))
if __name__ == "__main__":
unittest.main()