diff --git a/.github/workflows/desktop.yml b/.github/workflows/desktop.yml index ced2fd72..bb8a1624 100755 --- a/.github/workflows/desktop.yml +++ b/.github/workflows/desktop.yml @@ -92,6 +92,9 @@ jobs: # the reviewers of the release dialog and when a draft can be submitted - name: Test the release rules run: node scripts/desktop-release-rules.test.mjs + # the key inputs of a failed action in *Report…* (#381) + - name: Test the report inputs + run: node scripts/desktop-report-inputs.test.mjs # the entries of the right-click menu of an item and why one is # disabled – the revision toolbar's rules (#417) - name: Test the item menu rules diff --git a/CHANGELOG.md b/CHANGELOG.md index 5331dbf9..7481f58b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,20 @@ see the notices in the README. ### Added +- **Request ids** (#381): every answer of the server carries an + `x-request-id` header (a new UUID, or a reverse proxy's own id of 1–64 + characters `[A-Za-z0-9-]`), the request's log line records it as + `request_id`, and the web UI's error pages show it under *Details for a bug + report* and in *Copy details*. +- **Desktop: key inputs in problem reports** (#381): *Report…* on an error + now names what the action worked on – project group, item, revision, file + and format, the ticked files of a check-in, the options of a BOM or folder + import – for check-out, check-in and park, *Get files*, opening files, the + 3D preview and STL export, STEP exports, new items, adding files, BOM and + folder imports and request attachments. Names and short values only (a path + is reduced to its file name, at most 12 values of 200 characters); a 3D + preview that cannot be loaded from the server now also shows an error + message with *Report…*. - SolidCraft design properties (#367): part number, revision, description, project, author, organisation and licence are read from `.sc` designs at upload and check-in (bounded, text values only, migration `0078_file_properties`), listed under the file on the revision page and in the desktop app, returned as `properties`/`properties_line` with the file in the REST API (and `design_properties` in the MCP file briefs), and found by the full-text search. The revision page – web and desktop – shows a notice when they differ from the item number, revision code or name (`property_mismatches` of `GET /api/v1/items/{n}/revisions/{c}`) – worked out on every view, nothing is changed automatically. A new item created in the web UI with a design and no name takes the design's description; the desktop's *New item* (and *Create items…* of the worklist) and *Import a CAD folder* fill an empty description from it. - **Thumbnails in lists** (#420): the picture of a revision's main model (the rule of the revision page, now in `services::thumbnails`; a stand-alone JT diff --git a/CLAUDE.md b/CLAUDE.md index d1fbe01d..14c55cb3 100755 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -98,7 +98,8 @@ src/main.rs CLI (clap): serve (default), local (test installation, src/lo src/localserver.rs `teamcraft local`: private PostgreSQL cluster (bundled postgresql/bin, --pg-bin or installed; initdb with a random scram password, 127.0.0.1 + free port, no socket) + the server on 127.0.0.1 via a config overlay; stops PostgreSQL after the server, port file against a second instance, --exit-with-stdin for the desktop app -src/lib.rs AppState, router (healthz/readyz, /api/v1, web UI), security headers, tracing +src/lib.rs AppState, router (healthz/readyz, /api/v1, web UI), security headers, request ids (x-request-id, + outermost layer; RequestId extension read by web::html_errors), tracing src/config.rs env configuration; every variable also as NAME_FILE; all optional for local use src/domain.rs pure rules: Role, ItemType, LifecycleState, ChangeStatus, revision letters, numbering schemes, skins, validation src/services/ business logic – the only code that writes to the database diff --git a/README.md b/README.md index a7ad1c01..9ef42242 100755 --- a/README.md +++ b/README.md @@ -318,7 +318,8 @@ end-to-end tests against a real PostgreSQL. saved in the working folder (group and folder taken from where they are, duplicates refused, the worklist suggesting the parts and assemblies that belong to no item yet), an Agent view, crash reports the user - decides about and an update notice (never installs anything itself). The + decides about, *Report…* on every error (a reviewed, redacted report with + the action and its key inputs; nothing is sent without a click) and an update notice (never installs anything itself). The download *with local server* also runs a private test server with its own database on the laptop (*Try TeamCraft on this computer*). - **REST API** – everything the UI does, with personal access tokens. diff --git a/desktop/README.md b/desktop/README.md index 4d3ac80c..e2059360 100755 --- a/desktop/README.md +++ b/desktop/README.md @@ -53,8 +53,8 @@ TeamCraft server – it is a client, not an offline copy. | Notifications | Bell with unread count and toasts; **Watch** / **Unwatch** | | Agent | **Agent** in the rail (`Ctrl/Cmd+4`, or *Agent* in a revision or change tab) – the app's one place for AI, on one of two backends. **Company AI**: the server's assistant (the web UI's *Ask TeamCraft*, inline in the Agent tab; the key stays on the server) – it knows the last revision (and section, BOM rule), change or request tab, offers matching quick actions (*Draft release notes*, *Check before release*, *Brief me for this review* …), the drafts checkbox and one conversation kept on the server; read-only unless drafts are allowed. **Local agent**: an agent you configure under *Settings → Agents…* (Claude Code through its ACP adapter, Gemini CLI, any [Agent Client Protocol](https://agentclientprotocol.com) agent; none by default). The app starts it in the workspace folder and hands it TeamCraft's MCP server with your token. Streamed conversation, tool calls, plan, permission prompts (*Allow once* / *Reject*; calls of TeamCraft's own read-only MCP tools – by the server's `tools/list` annotations – are allowed once without a prompt and marked *Allowed automatically (read-only)*, since the server answers with your rights only; write tools, other servers' tools, the agent's own tools and anything not identified for certain still ask; *Settings → Agents… → Ask before read-only TeamCraft tools* asks for everything), the agent's own model picker (remembered per agent), *Cancel*, quick prompts from the server's MCP prompts, the open revision or change as context. Through the app the agent may only read text files in the workspace – no writing, no terminal. With both on the server, *Company AI* / *Local agent* in the Agent toolbar switches (remembered per server and user; default: the local agent if one is set up). When the server sets `TEAMCRAFT_LOCAL_AGENTS=off` the Agent view runs on the company AI only, the Rust side ends a running agent and refuses to start one (the app asks the server again when the Agent view opens, every minute and before every message, so a changed setting needs no restart); without a company AI the view says that AI is switched off. See [the guide](../docs/src/guide/assistant.md#acp-agents-in-the-desktop-app) | | Pack and Go, STL | ZIP of a revision and its structure, with a progress dialog and *Cancel* (also while the server packs); written as `.teamcraft-download` and renamed when complete, removed on cancel or error; **Export STL…** from the 3D viewer | +| Report a problem | *Help → Report a Problem…* and *Report…* on every error message (toasts and dialog errors, #381): one review dialog with the title ": " (cut at a word, `…`), a description with the action, its key inputs (`run(text, fn, inputs)`, `toast(…, action, inputs)`, `dialog(…, { inputs })`: group, item, revision, file and format, import options – names only, a path reduced to its file name, at most 12 values of 200 characters; `ui/js/report-inputs.js`, tested by `scripts/desktop-report-inputs.test.mjs`), the error and the versions, "What were you trying to do?" (left out when empty) and, ticked, the last 50 error messages of the session – all redacted in Rust like crash reports (`src/report.rs`: home folder → `~`, addresses → `[url]`, tokens → `[token]`). Nothing leaves the app without a click: *Open issue form* (GitHub, or *Settings → Report problems to*: the operator's own form, which gets `title` and `body`, or the policy `ReportUrl`; a long text is cut in the link and copied in full), *Copy report*, *Save report…* (work offline). The web UI's error pages have *Copy details* (status, message, page, time, version, request id = the `x-request-id` header and the server log's `request_id`) | | Usage report | Once a day of use the app sends its own TeamCraft server (`POST /api/v1/usage`) an anonymous report of the day before (`src/usage.rs`, #383): a random installation id, versions, OS, architecture, package type, minutes with the window in front, and counts of fixed actions (checkout, checkin, park, cancel_checkout, get_files, open_cad, open_3d_preview, import_bom, import_folder, new_item, release, search, assistant – a test checks each has a count site) and STEP exports per program – never names, numbers, paths or anything typed. *Settings → Privacy → Show what is sent* shows it. The server shares an aggregate with the developers only if its administrator turns that on (admin guide: *Privacy*) | -| Report a problem | *Help → Report a Problem…* and *Report…* on every error message (toasts and dialog errors, #381): one review dialog with the title ": " (cut at a word, `…`), a description with the action, key inputs, the error and the versions, "What were you trying to do?" (left out when empty) and, ticked, the last 50 error messages of the session – all redacted in Rust like crash reports (`src/report.rs`: home folder → `~`, addresses → `[url]`, tokens → `[token]`). Nothing leaves the app without a click: *Open issue form* (GitHub, or *Settings → Report problems to*: the operator's own form, which gets `title` and `body`, or the policy `ReportUrl`; a long text is cut in the link and copied in full), *Copy report*, *Save report…* (work offline). The web UI's error pages have *Copy details* (status, message, page, time, version) | | Managed deployment | Policies per computer or user, read once at start and never sent (`src/policy.rs`, #378): `ServerUrl` (+ `AllowOtherServers`), `CheckForUpdates`, `CrashReportPrompt`, `WorkspaceRoot`, `OrganizationName`, `UsageStatistics` – Windows registry `Software\Policies\TeamCraft` (HKLM wins over HKCU; ADMX/ADML in `deploy/desktop-policies/windows`), macOS managed preferences of `io.github.rudalabs.teamcraft` (`plutil`), Linux `/etc/teamcraft/policies.json`. Wrong types and out-of-range values count as not set; fixed settings show *Set by your organization* and are disabled; *Managed by …* in Settings and About. Admin guide: *Deploying the desktop app* | | Crash reports | Panics, uncaught UI errors and sessions that ended unexpectedly (web view crash, force-quit hang) are written to a local, redacted report (no home folder, server addresses, tokens or item data). At the next start the app shows it and asks: *Report on GitHub…* (a prefilled issue you submit yourself), *Copy* or *Delete report*. Nothing is sent without that choice; *Settings → Crash reports* turns the question off | | Language | *Settings → Language* (#379): *As the system* (`navigator.languages`: exactly, then by base language, else English), *English*, *Deutsch* – kept on this computer, applied at the next start. Messages come from `shared/i18n/.json` through `shared/js/i18n.js` (`t(id, params)`, English fallback); translated so far: the *Settings* dialog and the empty states (#423) | diff --git a/desktop/ui/js/bom-import.js b/desktop/ui/js/bom-import.js index 36bfdb16..d3c06c8d 100755 --- a/desktop/ui/js/bom-import.js +++ b/desktop/ui/js/bom-import.js @@ -9,6 +9,7 @@ import { selectView } from "./navigator.js"; import { activeTab, reloadActive, stat, table } from "./tabs.js"; import { openRevision } from "./revision.js"; import { baseName } from "./dialogs.js"; +import { revisionInputs } from "./report-inputs.js"; // ------------------------------------------------------------ Import BOM @@ -65,7 +66,7 @@ export function bomFromCadDialog(rev) { }, function (err) { applyButton.disabled = false; d.fail(err); }); } } - ], { wide: true }); + ], { wide: true, inputs: revisionInputs(rev) }); applyButton = handle.form.querySelector(".dialog-actions button.primary"); applyButton.disabled = true; api("GET", base).then(function (p) { @@ -236,7 +237,11 @@ export function openImportBomDialog(rev) { }); } } - ], { wide: true }); + ], { wide: true, inputs: function () { + // *Report…* (#381): the file name and the options, never the BOM lines + var extra = [["file", path ? baseName(path) : ""], ["numbers", numbers.value], ["match existing", match.checked ? "yes" : "no"]]; + return rev ? revisionInputs(rev, extra) : [["group", project.value]].concat(extra); + } }); importButton = handle.form.querySelector(".dialog-actions button.primary"); importButton.disabled = true; } diff --git a/desktop/ui/js/core.js b/desktop/ui/js/core.js index 8bfc8cdf..a29d1d4a 100755 --- a/desktop/ui/js/core.js +++ b/desktop/ui/js/core.js @@ -6,6 +6,7 @@ import { showConnect } from "./connect.js"; import { agent } from "./agent.js"; import { projectItems } from "./group-sync.js"; import { label } from "/static/shared/js/format.js"; +import { failureOf } from "./report-inputs.js"; export var TAURI = window.__TAURI__; export var invoke = TAURI.core.invoke; @@ -289,15 +290,26 @@ function rememberError(text) { state.recentErrors = (state.recentErrors || []).concat([new Date().toISOString().slice(11, 19) + " " + text]).slice(-50); } -/** A message at the bottom; `action` names what failed, for *Report…*. */ -export function toast(text, kind, action) { +/** `inputs` as given to run()/dialog(): a list of [name, value] or a function returning one. */ +function inputsNow(inputs) { + if (typeof inputs !== "function") return inputs; + try { return inputs(); } catch (e) { return []; } +} + +/** + * A message at the bottom; `action` names what failed and `inputs` are its + * key inputs (as for run()), both for *Report…*. + */ +export function toast(text, kind, action, inputs) { var el = h("div.toast" + (kind ? "." + kind : ""), { role: kind === "error" ? "alert" : "status" }, [h("span", { text: text })]); if (kind === "error") { rememberError((action ? action + ": " : "") + text); if (reportHook.open) { + // the key inputs of the action (#381), taken when the error is shown + var failure = failureOf(action, text, inputsNow(inputs)); el.appendChild(h("button.link.small", { type: "button", on: { click: function () { el.remove(); - reportHook.open({ action: action || "", error: text }); + reportHook.open(failure); } } }, "Report…")); } } @@ -325,15 +337,20 @@ export function api(method, path, body) { export function enc(s) { return encodeURIComponent(s); } -/** Runs an action with status text and error toast; returns its promise. */ -export function run(statusText, fn) { +/** + * Runs an action with status text and error toast; returns its promise. + * `inputs` (optional): the key inputs of the action as [[name, value], …] + * (or a function returning them) – names and short values only, never file + * contents or item data; they go into a *Report…* of the error (#381). + */ +export function run(statusText, fn, inputs) { setStatus(statusText + " …"); return Promise.resolve().then(fn).then(function (v) { setStatus(""); return v; }, function (err) { setStatus(""); - toast(errorText(err), "error", statusText); + toast(errorText(err), "error", statusText, inputs); throw err; }); } @@ -369,7 +386,9 @@ export function dialog(title, body, actions, opts) { rememberError(title + ": " + text); // *Report…* (#381): the dialog's title is the action that failed if (reportHook.open) { - errorEl.append(" ", h("button.link.small", { type: "button", on: { click: function () { reportHook.open({ action: title, error: text }); } } }, "Report…")); + // opts.inputs: the key inputs of the dialog's action, as for run() + var failure = failureOf(title, text, inputsNow(opts.inputs)); + errorEl.append(" ", h("button.link.small", { type: "button", on: { click: function () { reportHook.open(failure); } } }, "Report…")); } errorEl.classList.remove("hidden"); } diff --git a/desktop/ui/js/dialogs.js b/desktop/ui/js/dialogs.js index 59e8ee95..b3d0fc98 100755 --- a/desktop/ui/js/dialogs.js +++ b/desktop/ui/js/dialogs.js @@ -10,6 +10,7 @@ import { newItemFolder } from "./files-view.js"; import { describeFromDesign, formatKinds } from "./folder-import.js"; import { newItemFindings, newItemKeyText, newItemPlacement, newItemSummary } from "./new-files.js"; import { openRevision } from "./revision.js"; +import { revisionInputs } from "./report-inputs.js"; // ====================================================================== // Dialogs: new item, settings, about @@ -264,6 +265,11 @@ export function openNewItemDialog(opts) { runCheck(token).catch(function (err) { if (token === inspection) { findings = null; showInfo(); info.appendChild(line(".notice.error", errorText(err))); } }); }, 400); } + // *Report…* (#381): what the new item was to be, never its description + function newItemInputs() { + return [["group", group()], ["type", type.value], ["item folder", itemFolder()], ["number", number.value.trim()], + ["files", paths.map(baseName).join(", ")]]; + } dialog("New item", h("div.stack", null, [ field("Files", h("div.stack", null, [pick, fileList]), "the CAD model with its drawing and a STEP export for the 3D preview – uploaded right after the item is created; files in the working folder keep their folder"), h("div.grid-2", null, [field("Type", type), field("Project group", project)]), @@ -313,7 +319,8 @@ export function openNewItemDialog(opts) { if (err && err.createdRevision) { var rev = err.createdRevision; d.close(); - toast("Created " + rev.item_number + "/" + rev.code + ", but the files could not be uploaded: " + errorText(err), "error"); + toast("Created " + rev.item_number + "/" + rev.code + ", but the files could not be uploaded: " + errorText(err), "error", + "Uploading the files of the new item", revisionInputs(rev, [["files", paths.map(baseName).join(", ")]])); afterCreate(rev); return; } @@ -321,7 +328,7 @@ export function openNewItemDialog(opts) { }); } } - ], { wide: true }); + ], { wide: true, inputs: newItemInputs }); function afterCreate(rev) { openRevision(rev.item_number, rev.code); if (createdIn) delete state.expanded[createdIn]; diff --git a/desktop/ui/js/folder-import.js b/desktop/ui/js/folder-import.js index ebe0b680..5a6e469f 100755 --- a/desktop/ui/js/folder-import.js +++ b/desktop/ui/js/folder-import.js @@ -489,7 +489,11 @@ export function openImportFolderDialog() { { label: "Close", onClick: function (d) { stop = true; d.close(); } }, { label: "Scan", onClick: function (d) { if (!running) scanNow(d); } }, { label: "Import", primary: true, submit: true, onClick: importNow } - ], { wide: true }); + ], { wide: true, inputs: function () { + // *Report…* (#381): the folder's name (not its path) and the options + return [["folder", folder.value], ["group", project.value], ["numbers from names", fromNames.checked ? "yes" : "no"], + ["documents item", extras.checked ? "yes" : "no"], ["bom", bom.checked ? "yes" : "no"]]; + } }); } /** The report line of the BOM lines created from the CAD references. */ diff --git a/desktop/ui/js/report-inputs.js b/desktop/ui/js/report-inputs.js new file mode 100644 index 00000000..a96ef748 --- /dev/null +++ b/desktop/ui/js/report-inputs.js @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: MPL-2.0 +// TeamCraft Desktop – Key inputs of a failed action for *Report…* (#381). +// No DOM, no state: unit-tested by scripts/desktop-report-inputs.test.mjs +// (plain Node.js, run by the CI job `ui`). core.js passes the result of +// run(statusText, fn, inputs) on to the report dialog; desktop/src/report.rs +// redacts the values (home folder, server address, tokens) and caps them +// again. + +/** At most this many inputs per report (report.rs: MAX_INPUTS). */ +export var MAX_REPORT_INPUTS = 12; +/** A value longer than this is cut and ended with "…" (report.rs: MAX_INPUT). */ +export var MAX_REPORT_INPUT = 200; + +/** + * `inputs` = [[name, value], …] as [name, value] pairs of strings: entries + * without a name or value (null, undefined, "") are left out, a path is + * reduced to its file name (the folders stay on this computer), long values + * are cut. Anything that is not an array gives []. + */ +export function reportInputs(inputs) { + if (!Array.isArray(inputs)) return []; + var out = []; + for (var i = 0; i < inputs.length && out.length < MAX_REPORT_INPUTS; i++) { + var kv = inputs[i]; + if (!Array.isArray(kv) || kv.length < 2) continue; + if (kv[0] === null || kv[0] === undefined || kv[1] === null || kv[1] === undefined) continue; + var name = String(kv[0]).trim(); + var value = String(kv[1]).trim(); + if (!name || !value) continue; + if (/^(file|path|folder)$/i.test(name) || /[\\/]/.test(value) && /^[A-Za-z]:[\\/]|^[\\/~]/.test(value)) { + value = lastPathPart(value); + } + if (value.length > MAX_REPORT_INPUT) value = value.slice(0, MAX_REPORT_INPUT - 1) + "…"; + out.push([name, value]); + } + return out; +} + +/** Last part of a Windows or Unix path ("C:\\work\\a.ipt" → "a.ipt"). */ +function lastPathPart(value) { + var parts = value.split(/[\\/]+/).filter(function (p) { return p !== ""; }); + return parts.length ? parts[parts.length - 1] : value; +} + +/** + * The key inputs of an action on revision `rev`: its project group, item + * number and revision code, then `extra` ([[name, value], …], e.g. the file). + */ +export function revisionInputs(rev, extra) { + rev = rev || {}; + return [["group", rev.project_code], ["item", rev.item_number], ["revision", rev.code]].concat(extra || []); +} + +/** The failure handed to the report dialog by an error message. */ +export function failureOf(action, error, inputs) { + return { action: action || "", error: error || "", inputs: reportInputs(inputs) }; +} diff --git a/desktop/ui/js/report.js b/desktop/ui/js/report.js index 6043984b..e46ae43f 100755 --- a/desktop/ui/js/report.js +++ b/desktop/ui/js/report.js @@ -6,6 +6,7 @@ // *Save report…*. import { call, dialog, errorText, field, h, reportHook, state, toast } from "./core.js"; +import { reportInputs } from "./report-inputs.js"; /** Opens the report dialog for `failure` = { action, error, inputs }. */ export function reportDialog(failure) { @@ -18,7 +19,7 @@ export function reportDialog(failure) { var problem = { action: failure.action || "", error: failure.error || "", - inputs: (failure.inputs || []).map(function (kv) { return [String(kv[0]), String(kv[1])]; }), + inputs: reportInputs(failure.inputs), server_version: state.serverVersion || null }; // the server's version, asked once per app run (null when it does not answer) diff --git a/desktop/ui/js/requests.js b/desktop/ui/js/requests.js index 838a631d..57d638ab 100755 --- a/desktop/ui/js/requests.js +++ b/desktop/ui/js/requests.js @@ -117,7 +117,8 @@ export function attachmentImage(a) { export function attachToRequest(r) { chooseAttachment().then(function (path) { if (!path) return; - run("Attaching " + baseName(path), function () { return call("upload_request_attachment", { number: r.number, path: path }); }) + run("Attaching " + baseName(path), function () { return call("upload_request_attachment", { number: r.number, path: path }); }, + [["request", r.number], ["file", baseName(path)]]) .then(function (a) { toast("Attached " + a.filename + " to " + r.number); reloadActive(); }, function () {}); }, function (e) { toast(errorText(e), "error"); }); } diff --git a/desktop/ui/js/revision.js b/desktop/ui/js/revision.js index a2d2c777..48382715 100755 --- a/desktop/ui/js/revision.js +++ b/desktop/ui/js/revision.js @@ -19,6 +19,7 @@ import { openRequest, requestChangeDialog } from "./requests.js"; import { bomFromCadDialog, importBomDialog } from "./bom-import.js"; import { cadTargetChip, groupDefault, mainCadFile, openWithCad } from "./cad-open.js"; import { incompleteGet, notOpenedText } from "./get-rules.js"; +import { revisionInputs } from "./report-inputs.js"; /** Opens a revision tab; `viewerFile` (a file id) picks the file of its 3D viewer. */ export function openRevision(number, code, sub, viewerFile) { @@ -475,13 +476,17 @@ export function checkOutAndOpen(tab, file) { doCheckout(tab, function (r) { var why = incompleteGet(r); if (why) { toast(notOpenedText(file.filename, why) + " The check-out stays.", "info"); return; } - openWithCad(rev, file).catch(function (e) { toast(errorText(e), "error"); }); + openWithCad(rev, file).catch(function (e) { + toast(errorText(e), "error", "Opening " + file.filename + " in the CAD program", revisionInputs(rev, [["file", file.filename], ["format", file.format]])); + }); }); } /** Opens a CAD file of my own check-out from the working folder. */ export function openCadFile(tab, file) { - openWithCad(tab.data.rev, file).catch(function (e) { toast(errorText(e), "error"); }); + openWithCad(tab.data.rev, file).catch(function (e) { + toast(errorText(e), "error", "Opening " + file.filename + " in the CAD program", revisionInputs(tab.data.rev, [["file", file.filename], ["format", file.format]])); + }); } export function revFiles(tab, el) { diff --git a/desktop/ui/js/workspace.js b/desktop/ui/js/workspace.js index 82e30103..0cc8eb2b 100755 --- a/desktop/ui/js/workspace.js +++ b/desktop/ui/js/workspace.js @@ -16,6 +16,7 @@ import { incompleteGet, notOpenedText } from "./get-rules.js"; import { formatKinds } from "./folder-import.js"; import { isNewModelOfItsOwn, workspacePath } from "./new-files.js"; import { newItemDialog } from "./dialogs.js"; +import { revisionInputs } from "./report-inputs.js"; import { t } from "/static/shared/js/i18n.js"; // ---------------------------------------------------------------- 3D viewer @@ -51,10 +52,10 @@ export function revViewer(tab, el) { click: function () { var name = current.filename.replace(/\.[^.]+$/, "") + ".stl"; var data; - try { data = new Uint8Array(state.viewer.exportStl()); } catch (e) { toast(errorText(e), "error"); return; } + try { data = new Uint8Array(state.viewer.exportStl()); } catch (e) { toast(errorText(e), "error", "Exporting " + name, previewInputs); return; } invoke("save_stl", data, { headers: { "x-file-name": encodeURIComponent(name) } }).then(function (path) { if (path) toast("Saved " + path); - }, function (e) { toast(errorText(e), "error"); }); + }, function (e) { toast(errorText(e), "error", "Saving " + name, previewInputs); }); } } }, "Export STL…"); @@ -70,15 +71,24 @@ export function revViewer(tab, el) { ])); // the server converts the file into a mesh (pure Rust) – the app only draws it var versionId = tab.viewerVersion; + // *Report…* of a failed preview (#381): the file, its format and version + var previewInputs = revisionInputs(tab.data.rev, [["file", current.filename], ["format", previewFormat(current)], + ["version", versionId ? "older version" : "v" + current.version]]); try { - state.viewer = window.TeamCraftViewer.mount(canvas, { + var viewer = state.viewer = window.TeamCraftViewer.mount(canvas, { statusEl: status, overlayEl: overlay, resetBtn: resetBtn, upBtn: upBtn, onReady: function () { if (stlBtn) stlBtn.disabled = false; }, load: function () { - return invoke("file_bytes", versionId ? { kind: "version-mesh", id: versionId } : { kind: "mesh", id: current.id }); + return invoke("file_bytes", versionId ? { kind: "version-mesh", id: versionId } : { kind: "mesh", id: current.id }) + .catch(function (e) { + // only while this viewer is still on screen: a late failure of a viewer already + // replaced or disposed is not about the preview the user sees + if (state.viewer === viewer) toast(errorText(e), "error", "Loading the 3D preview of " + current.filename, previewInputs); + throw e; + }); } }); } catch (err) { @@ -108,7 +118,8 @@ export function revWorkspace(tab, el) { return h("button.small", { type: "button", title: "Create a STEP export with " + PROGRAM_LABEL[program] + " next to the file – check it in with the model for the 3D preview", on: { click: function () { - run("Exporting " + f.name + " to STEP with " + PROGRAM_LABEL[program], function () { return exportStep(rev, f.name); }) + run("Exporting " + f.name + " to STEP with " + PROGRAM_LABEL[program], function () { return exportStep(rev, f.name); }, + revisionInputs(rev, [["file", f.name], ["program", program]])) .then(function (step) { toast("Created " + step + " – check it in together with " + f.name); renderActive(); }, function () {}); } } }, "STEP"); @@ -124,7 +135,7 @@ export function revWorkspace(tab, el) { run("Writing the item data into " + f.name, function () { return call("solidcraft_write_item_data", { project: rev.project_code, item: rev.item_number, code: rev.code, file: f.name, data: { item_number: rev.item_number, revision: rev.code, name: rev.name, item_id: String(rev.item_id || ""), revision_id: String(rev.id) } }); - }).then(function () { toast(f.name + " now carries " + rev.item_number + "/" + rev.code + " – check it in to keep it"); renderActive(); }, function () {}); + }, revisionInputs(rev, [["file", f.name]])).then(function () { toast(f.name + " now carries " + rev.item_number + "/" + rev.code + " – check it in to keep it"); renderActive(); }, function () {}); } } }, "Item data"); } @@ -135,7 +146,7 @@ export function revWorkspace(tab, el) { tool("Open folder", "folder", function () { openWorkspaceFolder(tab); }), tool("Get files", "download", function () { getting(call("workspace_download", { project: rev.project_code, item: rev.item_number, code: rev.code })) - .then(function (r) { reportDownload(r); renderActive(); }, function (e) { toast(errorText(e), "error"); }); + .then(function (r) { reportDownload(r); renderActive(); }, function (e) { toast(errorText(e), "error", "Getting the files of " + rev.item_number + "/" + rev.code, revisionInputs(rev)); }); }, { title: "Copy the current files of this revision into the working folder without checking out (read-only)" }), tool("Get with structure…", "download", function () { getStructureDialog(rev); }, { title: "Copy this item and everything it consists of (BOM and referenced CAD files) into the working folder" }), @@ -372,7 +383,7 @@ export function openLocalFile(tab, f) { { label: "Get latest and open", primary: true, submit: true, onClick: function (d) { d.close(); getting(call("workspace_download", { project: rev.project_code, item: rev.item_number, code: rev.code })) - .then(function (r) { reportDownload(r); open(r); renderActive(); }, function (e) { toast(errorText(e), "error"); }); + .then(function (r) { reportDownload(r); open(r); renderActive(); }, function (e) { toast(errorText(e), "error", "Getting the latest files to open " + f.name, revisionInputs(rev, [["file", f.name]])); }); } } ]); return; @@ -411,14 +422,14 @@ export function openFile(tab, filename, local, warned, got) { } return call("workspace_open", { project: rev.project_code, item: rev.item_number, code: rev.code, file: filename }); }); - }).catch(function (e) { if (e) toast(errorText(e), "error"); }); + }).catch(function (e) { if (e) toast(errorText(e), "error", "Opening " + filename, revisionInputs(rev, [["file", filename]])); }); } export function doCheckout(tab, then) { var rev = tab.data.rev; run("Checking out " + rev.item_number + "/" + rev.code, function () { return getting(call("workspace_checkout", { project: rev.project_code, item: rev.item_number, code: rev.code })); - }).then(function (r) { + }, revisionInputs(rev)).then(function (r) { // with structure: the parts came read-only, local files were never overwritten toast(r.summary || ("Checked out " + rev.item_number + "/" + rev.code + " – the files in " + r.dir + " are writable now")); if (r.kept.length || r.clashes.length || r.failed.length || r.unresolved.length) reportDownload(r); @@ -439,7 +450,7 @@ export function doCancelCheckout(tab) { function release() { run("Releasing the check-out", function () { return api("POST", "/items/" + enc(rev.item_number) + "/revisions/" + enc(rev.code) + "/cancel-checkout"); - }).then(function () { + }, revisionInputs(rev)).then(function () { toast("Released the check-out of " + rev.item_number + "/" + rev.code); call("usage_count", { action: "cancel_checkout" }).catch(function () {}); // the unchanged local files become read-only again @@ -520,7 +531,13 @@ export function checkinDialog(tab, park) { // check-in; during an export Cancel skips it (the CAD program it started // is ended), and the check-in follows with the STEP files written so far. var running = false, cancelled = false, phase = "", skipping = false; - var opts = { wide: true }; + // *Report…* of a failure (#381): the revision, the ticked file names, STEP + function ticked() { return boxes.filter(function (b) { return b.input.checked; }).map(function (b) { return b.name; }); } + function checkinInputs() { + return revisionInputs(rev, [["files", ticked().join(", ")], ["park", park ? "yes" : ""], + ["step exports", stepNote && withStep.checked ? "yes" : ""]]); + } + var opts = { wide: true, inputs: checkinInputs }; function stopped() { return { status: 499, message: "Cancelled." }; } dialog((park ? "Park " : "Check in ") + rev.item_number + "/" + rev.code, body, [ { label: "Cancel", onClick: function (d) { @@ -615,7 +632,8 @@ export function checkinDialog(tab, park) { // the chosen files are on the server already: never offer to send them again d.close(); toast(files.join(", ") + " are on the server (parked) and " + ref + " is still checked out to you – " + - (cancelled || isCancelled(err) ? "the check-in was cancelled." : "the check-in failed: " + errorText(err)), cancelled || isCancelled(err) ? "info" : "error"); + (cancelled || isCancelled(err) ? "the check-in was cancelled." : "the check-in failed: " + errorText(err)), cancelled || isCancelled(err) ? "info" : "error", + "Checking in " + ref, checkinInputs); reloadActive(); refreshWorklistCount(); return; @@ -630,7 +648,7 @@ export function checkinDialog(tab, park) { d.fail(err); }); } - }, function (err) { toast(errorText(err), "error"); }); + }, function (err) { toast(errorText(err), "error", "Comparing the working folder of " + rev.item_number + "/" + rev.code, revisionInputs(rev)); }); } /** `0:07`, `12:34` – how long an export has been running. */ @@ -794,7 +812,7 @@ export function reviseRevision(number, code) { if (!ok) return; run("Creating the next revision", function () { return api("POST", "/items/" + enc(number) + "/revisions/" + enc(code) + "/revise"); - }).then(function (nr) { + }, [["item", number], ["revision", code]]).then(function (nr) { toast("Created " + nr.item_number + "/" + nr.code + " (in work, checked out to you)"); openRevision(nr.item_number, nr.code, "workspace"); refreshWorklistCount(); @@ -808,7 +826,7 @@ export function doObsolete(tab) { if (!ok) return; run("Marking obsolete", function () { return api("POST", "/items/" + enc(rev.item_number) + "/revisions/" + enc(rev.code) + "/obsolete"); - }).then(function () { toast(rev.item_number + "/" + rev.code + " is now obsolete"); reloadActive(); }, function () {}); + }, revisionInputs(rev)).then(function () { toast(rev.item_number + "/" + rev.code + " is now obsolete"); reloadActive(); }, function () {}); }); } @@ -876,7 +894,7 @@ export function addFilesDialog(tab) { var confirmed = ticks.filter(function (t) { return t.input.checked; }).map(function (t) { return t.name; }); var start = mine ? Promise.resolve() : run("Checking out " + rev.item_number + "/" + rev.code, function () { return getting(call("workspace_checkout", where)); - }); + }, revisionInputs(rev)); start.then(function () { return call("workspace_add_files", Object.assign({ paths: paths, subfolder: subfolder.value.trim(), confirmed: confirmed }, where)); }).then(function (r) { @@ -887,6 +905,6 @@ export function addFilesDialog(tab) { if (checkNow.checked && r.copied.length) { reloadActive(); checkinDialog(tab); } else { tab.sub = "workspace"; reloadActive(); } }, function (e) { d.fail(e); reloadActive(); }); } } - ], { wide: true }); + ], { wide: true, inputs: function () { return revisionInputs(rev, [["files", paths.map(function (p) { return String(p).split(/[\\/]/).pop(); }).join(", ")]]); } }); }, function (e) { toast(errorText(e), "error"); }); } diff --git a/docs/OPEN_WORK.md b/docs/OPEN_WORK.md index 362bb5cb..ae5b494e 100755 --- a/docs/OPEN_WORK.md +++ b/docs/OPEN_WORK.md @@ -160,9 +160,13 @@ feature milestone – the features in *What works* are enough; what is missing i desktop policy `UsageStatistics` (#378) once the developers' share is decided per desktop - [ ] Desktop: report a problem from any error (#381). Done: *Report…* on every toast and dialog error, *Help → Report a Problem…*, redacted prefilled report (`desktop/src/report.rs`), *Open issue form* / *Copy* / *Save*, own tracker - setting, *Copy details* on web error pages. Left: screenshot and affected-file attachments, the key inputs of - each action (most messages name only the action), a request id on web errors (the server version in the report: done) - (the tracker as a desktop policy: done, `ReportUrl`) + setting, *Copy details* on web error pages, the server version in the report, the tracker as a desktop policy + (`ReportUrl`), a request id on every answer and web error page (`x-request-id`, in the log line), the key inputs + of the main actions (check-out/in, park, get, open, 3D preview, STEP/STL export, new item, add files, BOM and + folder import, request attachments). Left: screenshot and affected-file attachments (pending a decision: what a + screenshot may show, and the warning for a file that leaves the organisation); key inputs for the remaining + smaller actions (change orders, requests, settings); *Report…* on inline error notices (new-item duplicate check, BOM + preview "No BOM lines found", the 3D viewer's canvas overlay incl. parse and graphics-memory errors) - [ ] Desktop: one-click update (#377). Done: update window (release notes, *Skip this version*, *Remind me later*), *Help → Check for Updates…*, download of this installation's file verified against `SHA256SUMS.txt`, Windows installers started, `.dmg` opened. Left: swapping a portable zip or AppImage in place with *Restart now*, a diff --git a/docs/src/admin/operations.md b/docs/src/admin/operations.md index 62443c69..d2bcbcc1 100755 --- a/docs/src/admin/operations.md +++ b/docs/src/admin/operations.md @@ -422,6 +422,16 @@ large check-ins, Pack and Go and backups. address and size the pool to the bouncer's `default_pool_size`; note the `options` startup parameter under [Sizing](#sizing). +## Request ids + +Every answer carries an `x-request-id` header, and the server writes the same +id as `request_id` into the log line of the request (with +`TEAMCRAFT_LOG_FORMAT=json` as a field of the `http` span). The web UI's error +pages show it under *Details for a bug report*, so a user's report leads to +the matching log lines. A reverse proxy that sets its own `x-request-id` +keeps its id, as long as it is 1–64 characters of `A–Z`, `a–z`, `0–9` and +`-`; anything else is replaced by a new UUID (#381). + ## Metrics for Prometheus Set `TEAMCRAFT_METRICS_TOKEN` (at least 16 characters, also as `_FILE`) and diff --git a/docs/src/dev/threat-model.md b/docs/src/dev/threat-model.md index a49b1f13..f15f6f3e 100755 --- a/docs/src/dev/threat-model.md +++ b/docs/src/dev/threat-model.md @@ -178,7 +178,8 @@ enial of service, **E**levation of privilege. | Desktop policies abused or leaked | T / I | the policies (#378) are only read, once at start, and never sent; machine-wide sources (`HKLM`, the computer's managed preferences, `/etc/teamcraft/policies.json`) need administrator rights to change, and a machine value wins over the user's; every value is checked (an `http(s)` address normalised like a typed one, an absolute folder, one line of at most 64 characters) and anything else counts as not set; a fixed server is enforced in the app's commands (`set_server`, `switch_connection`, `local_server_start`), not only in the UI | `desktop/src/policy.rs` `wrong_types_and_ranges_count_as_not_set`, `the_registry_answer_is_read`, `the_computer_wins_value_by_value`, `a_fixed_server_refuses_others_unless_allowed` | mitigated; a user-level policy (`HKCU`, the user's profile) is only as strong as the user's own rights | | Update notice abused | T / I | the daily release check (opt-out) asks only `api.github.com` for the latest release, without redirects and with a 10 s timeout; a version from the answer is used only if it is a plain `x.y.z`; the notice opens a fixed release page – the app downloads and installs nothing. GitHub sees the address and app version once a day (`desktop/src/update.rs`) | `desktop/src/update.rs` `newer_versions`, `tags`, `at_most_daily` | mitigated | | Server writes outside the working folder | T | every server-supplied name goes through `safe_name` / `normalize_folder` (no `..`, no separators, reserved Windows names) before `join_rel` (`desktop/src/layout.rs` `local_rel`) | `desktop/src/layout.rs` `names`, `folders_like_the_server`, `layout_mapping` | mitigated | -| Problem report leaks data | I | *Report a problem* (#381) shows the whole report for review and sends nothing by itself: the text is redacted in Rust (`crash::redact`: home folder, URLs, `tc_pat_`/`tc_sess_` tokens) before the dialog shows it, inputs are names and short values only (no file contents, no item data), the session log is the app's own error messages and only when ticked; the issue form address is GitHub's or an http(s) address the user set, with the text as query parameters (cut to 7000 characters) | `desktop/src/report.rs` `the_title_is_cut_at_a_word_and_redacted`, `the_body_names_action_inputs_error_versions_and_drops_an_unanswered_question`, `the_log_is_redacted_and_capped`, `the_form_address_fits_and_says_when_it_was_cut` | mitigated; what the user types into the report is sent as typed | +| Problem report leaks data | I | *Report a problem* (#381) shows the whole report for review and sends nothing by itself: the text is redacted in Rust (`crash::redact`: home folder, URLs, `tc_pat_`/`tc_sess_` tokens) before the dialog shows it, inputs are names and short values only (no file contents, no item data; a path is cut to its file name, at most 12 values of 200 characters, `ui/js/report-inputs.js`), the session log is the app's own error messages and only when ticked; the issue form address is GitHub's or an http(s) address the user set, with the text as query parameters (cut to 7000 characters) | `desktop/src/report.rs` `the_title_is_cut_at_a_word_and_redacted`, `the_body_names_action_inputs_error_versions_and_drops_an_unanswered_question`, `the_log_is_redacted_and_capped`, `the_form_address_fits_and_says_when_it_was_cut`; `scripts/desktop-report-inputs.test.mjs` | mitigated; what the user types into the report is sent as typed | +| Forged request id in logs and error pages | T / R | every answer gets an `x-request-id` (#381); an incoming one is kept only when it is 1–64 characters of `[A-Za-z0-9-]` (no spaces, line breaks or markup that could forge a log line or break a header or the error page), otherwise a new UUID replaces it; the id is a correlation hint, never a credential, and the error page escapes it like any other text | `src/lib.rs` `only_short_plain_request_ids_are_taken_over`, `every_response_carries_the_request_id`; `tests/request_id.rs` | mitigated; a client can still choose a plain id of its own, so ids from outside are not unique | | Crash report leaks data | I | reports (panics, uncaught UI errors, sessions that ended unexpectedly) are written locally and redacted before writing: home directory → `~`, URLs → `[url]`, `tc_pat_`/`tc_sess_` tokens → `[token]`; no server address, user name, token or file contents; at most 20 reports of 64 KiB. Nothing is uploaded: the user sees the whole text and opens a prefilled GitHub issue themselves (the address is built in Rust from the stored reports, never from text of the web view), copies or deletes it (`desktop/src/crash.rs`, `desktop/ui/js/crash.js`) | `desktop/src/crash.rs` `redaction_removes_home_urls_and_tokens`, `issue_links_fit_and_say_when_they_were_cut`, `reports_are_written_listed_capped_and_discarded` | mitigated; a panic message may still name an item number or file name the user can see in the dialog | | Opening a file starts a program | E | *Check out and open* starts only programs found in fixed install folders (*Program Files*, `/Applications`, the `PATH`) or the SOLIDWORKS folder from `HKLM` – never a path from the server or the web view; the web view sends only format families and versions, which only pick among those programs; the file is one located inside the working folder (`workspace::locate`), passed as one argument without a shell (`desktop/src/cadlaunch.rs`, `commands::workspace_open_cad`). Reusing a running Inventor (#350) runs a fixed PowerShell script from the app (written to the temp folder, the file, install folder and project file passed as named arguments, never as script text); it only talks to an Inventor the user can see (an invisible automation instance is left alone) and opens only the located working-folder file | `desktop/src/cadlaunch.rs` `scan_finds_versioned_installations`, `plans_follow_the_group_and_the_file`, `registry_answers_are_read`, `the_script_says_only_what_is_read`, `the_nearest_single_project_file_counts` | mitigated; a program planted in *Program Files* needs administrator rights there | | Old desktop app against a newer server | T | none: no version handshake | – | **open** (OPEN_WORK *desktop ↔ server compatibility*) | diff --git a/docs/src/guide/desktop.md b/docs/src/guide/desktop.md index fdae9e04..c8f89c42 100755 --- a/docs/src/guide/desktop.md +++ b/docs/src/guide/desktop.md @@ -758,7 +758,7 @@ check off (for example on computers without internet access). Every error message has a **Report…** link, and *Help → Report a Problem…* opens the same dialog without one. It shows the report before anything leaves the app: a title (what you did and the error), the action, its key -inputs (no file contents or item data), the error and the versions; add what +inputs, the error and the versions; add what you were trying to do (left out if empty) and, if you like, the error messages of this session. Your home folder, server addresses and tokens are removed. Edit the text as you like, then: @@ -770,8 +770,17 @@ removed. Edit the text as you like, then: * *Copy report* puts it on the clipboard; * *Save report…* writes it to a file (both work without a network). +The key inputs are what the action worked on, for example the project group, +item number and revision of a check-out or check-in, the names of the ticked +files, the file and format of a 3D preview, or the file name and options of a +BOM or folder import. They are names and short values only: a path is +reduced to the file name, and file contents, item names, descriptions and +BOM lines are never included. + The error pages of the web UI have *Details for a bug report* with *Copy -details* (status, message, page, time and version). +details* (status, message, page, time, version and the request id). The +request id is also in the server log, so your administrator can find the +error there. ## Usage report diff --git a/docs/src/reference/api.md b/docs/src/reference/api.md index e1478e0d..ef062901 100755 --- a/docs/src/reference/api.md +++ b/docs/src/reference/api.md @@ -11,6 +11,13 @@ curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/v1/me curl -s -H "Authorization: Bearer $TOKEN" "http://localhost:8080/api/v1/items?q=bracket&system=solidworks" ``` +Every answer of the server (API, web pages, health checks) carries an +**`x-request-id`** header (#381): a new UUID, or the request's own +`x-request-id` when it is 1–64 characters of `A–Z`, `a–z`, `0–9` and `-` +(set by a reverse proxy, for example). The same id is written to the +request's log line (`request_id`) and shown on the web UI's error pages, so a +bug report can be matched to the server log. JSON error bodies are unchanged. + ## Token scopes A token has a scope that narrows what its user may do with it; it never diff --git a/scripts/ci-changes.sh b/scripts/ci-changes.sh index d462019a..39f6d98b 100755 --- a/scripts/ci-changes.sh +++ b/scripts/ci-changes.sh @@ -66,6 +66,8 @@ DESKTOP_UI='^(desktop/ui|static|shared)/|^scripts/[^/]*\.mjs$|^\.github/workflow # scripts/desktop-lock-layout.test.mjs compares the desktop layout with the # server's sizes in this file (#428) DESKTOP_UI+='|^src/services/lock_overview\.rs$' +# scripts/desktop-report-inputs.test.mjs checks its caps against this file (#381) +DESKTOP_UI+='|^desktop/src/report\.rs$' # codeql – codeql.yml, one analysis per language. CODEQL_ACTIONS='^\.github/(workflows|actions|codeql)/' @@ -181,6 +183,7 @@ self_test() { check desktop/src/workspace.rs server=false desktop_rust=true desktop_ui=false 'codeql=["rust"]' 'deny=[]' check desktop/src/compat.rs server=true desktop_rust=true check desktop/src/usage.rs server=true desktop_rust=true + check desktop/src/report.rs server=false desktop_rust=true desktop_ui=true check desktop/Cargo.lock server=false desktop_rust=true 'deny=["desktop/Cargo.toml"]' 'codeql=["rust"]' check desktop/tauri.conf.json server=false desktop_rust=true desktop_ui=false 'codeql=[]' check desktop/README.md server=false desktop_rust=false desktop_ui=false diff --git a/scripts/desktop-report-inputs.test.mjs b/scripts/desktop-report-inputs.test.mjs new file mode 100644 index 00000000..934e4bf0 --- /dev/null +++ b/scripts/desktop-report-inputs.test.mjs @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: MPL-2.0 +// Unit tests of desktop/ui/js/report-inputs.js (the key inputs of a failed +// action that *Report…* puts into the problem report, #381). +// Plain Node.js, no packages; run by the CI job `ui`: +// +// node scripts/desktop-report-inputs.test.mjs +// +// The module sits in a folder without package.json, so it is imported as a +// data: URL module instead of by path. +import assert from "node:assert/strict"; +import fs from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const here = path.dirname(fileURLToPath(import.meta.url)); +const file = path.resolve(here, "../desktop/ui/js/report-inputs.js"); +const src = fs.readFileSync(file, "utf8"); +const { reportInputs, revisionInputs, failureOf, MAX_REPORT_INPUTS, MAX_REPORT_INPUT } = await import( + "data:text/javascript;base64," + Buffer.from(src).toString("base64")); + +let n = 0; +function test(name, fn) { fn(); n++; console.log("ok - " + name); } + +test("the inputs of run() reach the failure of the report dialog", () => { + const f = failureOf("Checking in PRT-000042", "file is locked", [["item", "PRT-000042"], ["file", "bracket.ipt"]]); + assert.deepEqual(f, { + action: "Checking in PRT-000042", + error: "file is locked", + inputs: [["item", "PRT-000042"], ["file", "bracket.ipt"]] + }); +}); +test("no inputs give an empty list, not undefined", () => { + assert.deepEqual(failureOf("Saving", "x").inputs, []); + assert.deepEqual(failureOf(undefined, undefined, null), { action: "", error: "", inputs: [] }); + assert.deepEqual(reportInputs("item=1"), []); +}); +test("empty names and values are left out, others become trimmed strings", () => { + assert.deepEqual(reportInputs([["item", ""], ["revision", null], ["", "x"], [" group ", " ROVER "], ["count", 3], ["bad"]]), + [["group", "ROVER"], ["count", "3"]]); +}); +test("paths are reduced to the file name", () => { + assert.deepEqual(reportInputs([ + ["file", "C:\\Users\\max\\work\\bracket.ipt"], + ["target", "/home/max/vault/pump.step"], + ["folder", "~/work/rover/"], + ["note", "a/b is fine"] + ]), [["file", "bracket.ipt"], ["target", "pump.step"], ["folder", "rover"], ["note", "a/b is fine"]]); +}); +test("long values are cut and at most MAX_REPORT_INPUTS are kept", () => { + const long = reportInputs([["name", "x".repeat(500)]])[0][1]; + assert.equal(long.length, MAX_REPORT_INPUT); + assert.ok(long.endsWith("…")); + const many = Array.from({ length: 30 }, (_, i) => ["k" + i, "v" + i]); + assert.equal(reportInputs(many).length, MAX_REPORT_INPUTS); +}); +test("a revision names its group, item and revision, then the extra inputs", () => { + const rev = { project_code: "ROVER", item_number: "PRT-000042", code: "B", name: "Bracket (item data)" }; + assert.deepEqual(reportInputs(revisionInputs(rev, [["file", "/home/max/work/bracket.ipt"]])), + [["group", "ROVER"], ["item", "PRT-000042"], ["revision", "B"], ["file", "bracket.ipt"]]); + // no item name or other item data; a missing revision gives no inputs + assert.equal(JSON.stringify(revisionInputs(rev)).includes("Bracket"), false); + assert.deepEqual(reportInputs(revisionInputs(undefined)), []); +}); +test("the caps match desktop/src/report.rs", () => { + const rs = fs.readFileSync(path.resolve(here, "../desktop/src/report.rs"), "utf8"); + assert.match(rs, new RegExp("const MAX_INPUTS: usize = " + MAX_REPORT_INPUTS + ";")); + assert.match(rs, new RegExp("const MAX_INPUT: usize = " + MAX_REPORT_INPUT + ";")); +}); + +console.log(`${n} tests passed`); diff --git a/src/lib.rs b/src/lib.rs index 15b25c2b..9467fed0 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -235,14 +235,69 @@ pub fn router(state: AppState) -> Router { // strings can carry one-time OIDC codes and search terms. TraceLayer::new_for_http() .make_span_with(|req: &axum::http::Request| { - tracing::info_span!("http", method = %req.method(), path = %req.uri().path()) + let id = req + .extensions() + .get::() + .map(|r| r.0.as_str()) + .unwrap_or(""); + tracing::info_span!( + "http", + method = %req.method(), + path = %req.uri().path(), + request_id = %id + ) }) .on_request(()) .on_response(DefaultOnResponse::new().level(Level::INFO)), ) + // outermost: the id is known to the trace span, the error pages and + // every response header (#381) + .layer(axum::middleware::from_fn(assign_request_id)) .with_state(state) } +/// Name of the header that carries the id of a request (#381). +pub const REQUEST_ID_HEADER: &str = "x-request-id"; + +/// Id of one HTTP request (#381): sent back as `x-request-id`, written to +/// the request's log line and shown on error pages, so a bug report can be +/// matched to the server log. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RequestId(pub String); + +/// Whether an incoming `x-request-id` (from a reverse proxy) is taken over: +/// 1–64 characters of `[A-Za-z0-9-]`, nothing that could break a log line +/// or a header. +pub fn valid_request_id(value: &str) -> bool { + !value.is_empty() + && value.len() <= 64 + && value + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-') +} + +/// Gives every request an id: a valid incoming `x-request-id` or a new +/// UUID. Stored in the request extensions and set on the response. +async fn assign_request_id( + mut request: axum::extract::Request, + next: axum::middleware::Next, +) -> Response { + let name = HeaderName::from_static(REQUEST_ID_HEADER); + let id = request + .headers() + .get(&name) + .and_then(|v| v.to_str().ok()) + .filter(|v| valid_request_id(v)) + .map(str::to_string) + .unwrap_or_else(|| uuid::Uuid::new_v4().to_string()); + let value = HeaderValue::from_str(&id).expect("an id of [A-Za-z0-9-] is a header value"); + request.headers_mut().insert(name.clone(), value.clone()); + request.extensions_mut().insert(RequestId(id)); + let mut response = next.run(request).await; + response.headers_mut().insert(name, value); + response +} + /// Liveness probe: the process is up and serving HTTP. async fn healthz() -> &'static str { "ok" @@ -460,4 +515,55 @@ mod tests { assert!(text.contains("teamcraft_http_requests_total{class=\"2xx\"} 1")); assert!(text.contains("teamcraft_http_request_duration_seconds_count 2")); } + + #[test] + fn only_short_plain_request_ids_are_taken_over() { + assert!(valid_request_id("3f2a-77b0")); + assert!(valid_request_id(&"a".repeat(64))); + assert!(!valid_request_id("")); + assert!(!valid_request_id(&"a".repeat(65))); + assert!(!valid_request_id("abc def")); + assert!(!valid_request_id("abc\nINFO forged")); + assert!(!valid_request_id("