From 401bf1ad4dfbe02466420b38926595c709dff231 Mon Sep 17 00:00:00 2001 From: maksudur03 <42118979+maksudur03@users.noreply.github.com> Date: Mon, 28 Sep 2026 20:33:57 +0600 Subject: [PATCH] Feature/dashboard UI refactor (#46) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(dashboard): declutter the Network detail pane The Network detail head had grown three stacked strips above the tabs — a facts row, an actions row, and the tabs themselves — pushing the request and response panes well down the viewport. Each strip also duplicated something already on screen: the facts repeated what the compare panes' "General" groups showed, and the actions row spent a full row on buttons that are used occasionally, not per-capture. Consolidate onto the Summary tab and a kebab menu: - Move the facts strip (time, duration, content type, correlation, mock rule, vs-original) out of the head and into Summary, where the mock rule keeps its link into Mocks via a new `mockRuleLink` kv-grid row type. - Drop the "General" group from both compare panes. Everything it held now lives in Summary, so the tab is just headers and body. - Give the failure branch's error string its own `response.error` Summary row instead of parenthesising it into `response.status`; it was previously only reachable through the General group that just went away. - Replace the actions row with a kebab (⋮) menu beside the expand button. Opt-in per pane via `actionsMenu`, so WebSockets/Push/Crashes — two or three actions each — keep their flat rows. Items carry the same `data-detail-action` ids, so the existing delegated handlers are unchanged. Closes on pick, outside click, and Escape. - Order Summary first among the tabs. Default selection is still Request & response. Co-Authored-By: Claude Opus 5 * docs: specify the Network detail pane layout in DESIGN_SPEC The spec's Network entry still described a "detail action row (Clone to composer, cURL, fetch, JSON, Related events)" that predates the current actions/tabs split, and nothing documented the detail pane's anatomy at all — so the previous commit's rearrangement had no spec to disagree with. Add §4a covering the shared head (badge/title/status/expand), the two variants of its middle band (Network's kebab menu vs. the other panes' flat actions row), Network's tab order and why Summary leads while Request & response stays the default selection, the absent "General" group, group collapse defaults, and Simple-mode hiding. Also correct three stale claims while in here: §5 named `.list`/`.detail-pane` (now `.list-pane`/`.detail-pane-v2` inside `.split-shell`), §6's Network toolbar list omitted the service dropdown and More-filters disclosure, and the new §4a records that Timeline hand-rolls its own head instead of using `detailHeadHtml` — logged as an open item rather than left as a silent drift. Co-Authored-By: Claude Opus 5 * fix: never let verification block a release build - Split the packaged scan per format: verifyDevConsolePackagedArtifact (APK/AAR, assemble path) and verifyDevConsolePackagedBundle (AAB, bundle path). One task naming both built an AAB during every release APK build, which broke Studio's signing wizard: -Pandroid.injected.apk.location puts the final AAB inside package's output dir, so Gradle rejects produceBundleIdeListingFile for an undeclared dependency. - Treat scan-limit-exceeded as a warning, not a violation. R8's mapping file overran the 64 MiB read cap and failed real release builds. - Skip BUNDLE-METADATA/ entries: Play strips them, and proguard.map names every class the build ever saw. - Warn instead of throwing FileNotFoundException on a missing or unreadable package. - Match forbidden paths at any segment, so assets/devconsole-web/ is also found at base/assets/devconsole-web/ inside an AAB. - Add devConsole.verificationEnabled / -Pdevconsole.verification.enabled=false to take verification off assemble/bundle, warning loudly and leaving verifyDevConsoleProtectedArtifacts runnable by name. Tests: 11 new functional tests, 42/42 pass. Verified against a real app on both assembleLiveRelease and bundleLiveRelease with the wizard's injected location. * docs: align the §4a detail-pane diagram The box borders didn't line up: the 🔍 and 🔒 emoji are double-width, so the lines carrying them ran two columns past the frame while every other row sat short of it. Dropped both emoji and repadded every line to a uniform 62-column interior, verified by measuring East Asian width rather than counting characters. Co-Authored-By: Claude Opus 5 * fix: support console clipboard fallback and unredacted auth tokens by default - Add fallback using document.execCommand('copy') when navigator.clipboard or window.isSecureContext is unavailable (e.g. over plain HTTP on LAN) - Route copyStateJson and copyTableCsv through copyToClipboard with user toast feedback - Remove authorization headers and Bearer regex pattern from default redaction policy to allow inspecting and copying authorization tokens by default during debugging - Update documentation in SECURITY_AND_REDACTION.md and THREAT_MODEL.md - Update unit tests across SDK modules and add regression tests for clipboard copy fallback Fixes #40 * fix: show the request query string in the network list Both list surfaces rendered only the captured path, so two calls to the same endpoint -- /orders?status=open and /orders?status=all -- were two identical rows and had to be opened one by one to be told apart. The query was captured and redacted all along; it just never reached the row. - Add NetworkUrl.queryString (the redacted `a=1&b=2` form display already built inline) and emit it as "query" beside "path" in the network list summary JSON, mirrored byte-for-byte in the evidence snapshot - Web: rowHtml gains an optional mainSub, and Network rows pass the query into it -- dimmed after the path, with a title carrying both in full, so the endpoint stays the part that survives the row's ellipsis - Device: pathWithQuery() reads the query back off the transaction url and titles the traffic row with it; traffic search matches the query, and the two url.ifBlank fallbacks no longer drop it path itself stays bare everywhere: mock-rule prefill escapes it into a regex, and the path= filter and path facets group on the endpoint alone. InspectorTransactionUi deliberately gains no field for the same reason -- widening that data class would break its constructor and copy signatures for every compiled caller, and url already carries the query. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01NPxRnPJNgsb3y455VCrDGU * Fix arraylist visualization issue of showing index of every item in json * Fix url truncating visualization at both app and web * Fix "\" issue while copying entire endpoint from app Add separate create and post request at sample screen * Fix mock editor keyboard and spacing issues and add full screen response body editing * Add pretty view with collapsible JSON to the expanded body view at web dashboard --------- Co-authored-by: Munif Co-authored-by: Claude Opus 5 Co-authored-by: shakibuzzaman Co-authored-by: Md. Asaduzzaman Nur --- CHANGELOG.md | 44 ++ docs/DESIGN_SPEC.md | 74 ++- docs/SECURITY_AND_REDACTION.md | 7 +- docs/THREAT_MODEL.md | 4 +- .../gradle/DevConsoleVariantPolicyPlugin.kt | 275 +++++++++-- ...onsoleVariantPolicyPluginFunctionalTest.kt | 273 +++++++++++ .../devconsole/sample/compose/MainActivity.kt | 85 +++- .../io/devconsole/sample/MainActivity.kt | 9 +- .../composer/ComposerRequestTest.kt | 2 +- .../devconsole/core/EventBatchWriterTest.kt | 4 +- .../io/devconsole/core/EventPipelineTest.kt | 6 +- .../export/EventExportWriterTest.kt | 8 +- .../io/devconsole/FullInspectorDataSource.kt | 5 + .../io/devconsole/AndroidFileInspectorTest.kt | 2 +- .../AndroidInspectorExporterTest.kt | 4 +- .../AndroidPreferencesInspectorTest.kt | 4 +- .../kotlin/io/devconsole/CrashCaptureTest.kt | 4 +- .../io/devconsole/CrashPolicyGatingTest.kt | 4 +- .../devconsole/FullInspectorDataSourceTest.kt | 26 +- .../devconsole/RemoteConfigInspectorTest.kt | 13 +- .../io/devconsole/logs/LogRecorderTest.kt | 4 +- sdk/network/api/network.api | 1 + .../io/devconsole/network/NetworkCapture.kt | 9 +- .../network/NetworkCaptureFactoryTest.kt | 9 +- .../devconsole/network/NetworkExportTest.kt | 6 +- .../io/devconsole/push/PushEventStoreTest.kt | 4 +- .../io/devconsole/security/RedactionEngine.kt | 6 +- .../security/RedactionEngineTest.kt | 30 +- .../server/api/LocalServerEngine.kt | 1 + .../server/ktor/DevConsoleKtorModule.kt | 4 +- .../resources/devconsole-web/dashboard.css | 67 ++- .../resources/devconsole-web/dashboard.js | 431 ++++++++++++++--- .../main/resources/devconsole-web/index.html | 7 + .../server/ktor/DashboardAssetsTest.kt | 32 ++ .../server/ktor/DevConsoleKtorModuleTest.kt | 48 +- .../FeatureFlagAndExportBundleRoutesTest.kt | 4 +- .../DevConsoleOkHttpWebSocketListenerTest.kt | 2 +- .../socket/paho/DevConsolePahoMqttTest.kt | 2 +- .../devconsole/socket/SocketRecorderTest.kt | 16 +- sdk/ui-compose/src/main/AndroidManifest.xml | 11 +- .../ui/compose/InspectorDataSource.kt | 61 +++ .../ui/compose/InspectorMockRuleEditor.kt | 454 +++++++++++++++--- .../ui/compose/InspectorNetworkSnippets.kt | 36 +- .../ui/compose/InspectorObserveFrameDetail.kt | 2 +- .../ui/compose/InspectorObserveLogDetail.kt | 2 +- .../ui/compose/InspectorObserveNetDetail.kt | 2 +- .../ui/compose/InspectorObserveTrafficTab.kt | 22 +- .../ui/compose/InspectorTextField.kt | 19 + .../ui/compose/InspectorDataSourceTest.kt | 63 +++ .../compose/InspectorNetworkSnippetsTest.kt | 105 ++++ 50 files changed, 2032 insertions(+), 281 deletions(-) create mode 100644 sdk/ui-compose/src/test/kotlin/io/devconsole/ui/compose/InspectorNetworkSnippetsTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index c1bd20a..2a9e716 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,50 @@ before it can reach a release. (There was briefly a separate `sdk:plugin-api` mo third-party plugin framework; it was removed before ever shipping — see Removed, below — so it never joined this list.) +## Unreleased + +Nothing this plugin does should ever be the reason a release cannot be built. Everything below comes +from one release APK build that the plugin stopped for reasons that had nothing to do with whether +the DevConsole runtime leaked. + +### Fixed + +- **A release APK build no longer builds an app bundle nobody asked for — or fails trying.** The + packaged-artifact scan named `SingleArtifact.APK` and `SingleArtifact.BUNDLE` on one task and hung + it off `assemble`, so every release APK build also ran `packageBundle`, + `signBundle` and `produceBundleIdeListingFile`. From Android Studio's "Generate + Signed APK" wizard that was fatal: the wizard passes `-Pandroid.injected.apk.location`, which puts + AGP's final AAB path *inside* `package`'s output directory, and Gradle then rejects + `produceBundleIdeListingFile` for consuming that directory without declaring a dependency + on `package`. The scan is now one task per packaging format — + `verifyDevConsolePackagedArtifact` for the APK/AAR on the assemble path, and a new + `verifyDevConsolePackagedBundle` for the AAB on the bundle path — so the plugin only ever + asks for artifacts the build was already going to produce. +- **A truncated read is no longer reported as a leak.** An entry too large to read to the end was + recorded as a `scan-limit-exceeded` violation, which failed release builds over the size of R8's + mapping file. Not finishing a read says nothing about what shipped: it is now a warning and an + `unscannable` list in the JSON report, and never fails a build on its own. +- **App-bundle build metadata is left out of the scan.** `BUNDLE-METADATA/` holds R8's `proguard.map` + and the dependency manifest; Play strips it, so nothing under it can carry DevConsole onto a + device. Reading it was the trigger for the false positive above, and the mapping file names every + class the build ever saw. +- **A missing or unreadable package is a warning, not a failed build.** Scanning an artifact the + build never produced threw `FileNotFoundException` out of the task. Whatever leaves an expected + artifact missing is a task-graph or environment problem, and must not stop a release. +- **Path-based detection now works inside app bundles.** An APK carries the dashboard at + `assets/devconsole-web/`, but a bundle nests every module under its own directory and carries it at + `base/assets/devconsole-web/`. Prefix matching was anchored at the start of the entry name, so it + matched in APKs and silently never matched in AABs — a gap that stayed hidden only while one task + scanned both formats and the APK caught the leak first. + +### Added + +- **`devConsole.verificationEnabled`**, also settable as `-Pdevconsole.verification.enabled=false`. + Takes verification off `assemble`/`bundle` so a build can always get out the door, warning loudly + every time and leaving `verifyDevConsoleProtectedArtifacts` runnable by name. A plugin that can + block a release needs a way past it that does not involve editing build files — the signing wizard + has no command line. Turning it off does not make a release safe to ship; it makes it unverified. + ## 1.3.1 — 2026-08-24 ### Changed diff --git a/docs/DESIGN_SPEC.md b/docs/DESIGN_SPEC.md index 9d00310..c7c4b50 100644 --- a/docs/DESIGN_SPEC.md +++ b/docs/DESIGN_SPEC.md @@ -232,6 +232,63 @@ must recompute mid-scroll rather than assume) — see §9. - **Toolbar** (`.toolbar`): flex row, wraps, `align-items: flex-end` so label-stacked fields (uppercase label over input) share a bottom baseline with bare buttons. (This was a bug — see §9.) +## 4a. Detail pane anatomy (split views) + +Four of the five split views build their right-hand pane from one shared helper (`detailHeadHtml`) — +Network, WebSockets, Push and Crashes. Timeline hand-rolls its own head from the same classes and +is the odd one out; folding it into the helper is an open item (§9). + +``` +┌ .detail-head ────────────────────────────────────────────────┐ +│ [BADGE] [MOCK] title (mono, ellipsis) STATUS [⤢] [⋮] │ head row +│ ‹facts strip — see table below› │ .detail-facts +│ ‹actions row — see table below› │ .detail-actions-row +│ Summary │ Request & response │ Headers │ … [Stacked] │ .detail-tabs +├ .detail-find (compare/diff tabs only) ───────────────────────┤ +│ Find in headers, payload and response… N matches │ +├ .detail-body (scrolls) ──────────────────────────────────────┤ +│ ┌ .sbs-pane ────────┐ ┌ .sbs-pane ────────┐ │ .sbs-grid +│ │ ● Request [Copy] │ │ ● Response [Copy] │ │ +│ │ › Request headers │ │ › Response headers│ │ collapsed +│ │ ⌄ Payload │ │ ⌄ Body │ │ expanded +│ └───────────────────┘ └───────────────────┘ │ +│ redaction footnote │ +└──────────────────────────────────────────────────────────────┘ +``` + +**Two variants of the head's middle band.** The facts strip and the actions row are per-pane +choices, not fixed furniture: + +| Pane | Facts strip | Actions | +|---|---|---| +| **Network** | none — folded into the Summary tab | kebab **⋮** menu, right of the expand button | +| WebSockets, Push, Crashes | `.detail-facts` row under the title | flat `.detail-actions-row` | + +The kebab is opt-in via `detailHeadHtml`'s `actionsMenu` flag. Network carries up to seven actions +(flag, pin, resend, clone, mock *or* unmock, cURL, fetch) — a full row of buttons for controls +used occasionally, not per-capture. The other panes carry two or three, where a row costs nothing +and a menu would only add a click. Menu items keep the same `data-detail-action` ids as row +buttons, so a pane's delegated click handler is identical either way. The panel is +`position: fixed` (placed by `positionDetailMenuPanel()`) because `.detail-pane-v2` is +`overflow: hidden` and an absolutely positioned popup clips at the pane edge — the same trade +`.drop-panel` makes. It closes on pick, outside click, and Escape (returning focus to its button). + +**Network's tab order** is `Summary · Request & response · [Diff vs baseline] · Headers · Timing · +Related events`. Summary leads because it absorbed the head's old facts strip and is the "what is +this capture" view: time, duration, request url/method, response status, `response.error` on a +failure, content type, correlation id, mock rule (a link into Mocks), vs-original field count, and +tags. Diff only appears once a baseline is pinned. **Default selection is Request & response**, not +the first tab — the body is what an engineer opens a capture to read. + +Consequently Network's two compare panes hold only headers and body; they have no "General" group, +because everything it used to duplicate now lives one tab left. Group collapse defaults come from +`detailGroupHtml`: `Payload`/`Body` open, everything else collapsed, an explicit user toggle always +winning over the default. + +**Simple mode** hides the Headers/Timing/Related tabs outright, and hides individual actions that +carry `hideInSimple` (`fetch` always; `pin` only while no baseline is pinned, so a pinned baseline +can never be stranded behind a mode switch). + ## 4b. Layout (Android in-app inspector) - **Bottom nav, 4 destinations:** `01 Observe · 02 Control · 03 Data · 04 More` (numbered + label, @@ -300,7 +357,9 @@ tray screen becomes a natural addition, just not one this pass builds. - **Segmented control** (`.seg`): joined buttons, one active (signal). - **Chips / chip-row** (`.chip-row`): wrapping pill filters. - **Cards** (`.card`): panel surface, 1px `--line`, 16px pad, `h2` with small leading icon. -- **Split panes:** `.list` (rows) + `.splitter` + `.detail-pane` (min-width 300px). +- **Split panes:** `.list-pane` (rows) + `.splitter` (5px) + `.detail-pane-v2`, inside `.split-shell` + (min 360px per column). Detail-pane anatomy — head, facts/actions variants, tabs, find bar, + side-by-side groups — is specified in §4a. - **Data table** (`.db-table`): real ``, `
`, truncation via `.db-truncated`, horizontal scroll in `.db-table-wrap`. - **Empty states** (`.empty-state`): centered icon + title + sub. Used widely — design these well; @@ -317,9 +376,10 @@ tray screen becomes a natural addition, just not one this pass builds. 1. **Overview** — landing / status summary, connect prompt when unauthenticated, and the previous-run-crashed banner (see [CRASH_AND_ANR.md](CRASH_AND_ANR.md#the-previous-run-crashed-banner)). -2. **Network** — captured HTTP list + detail; toolbar (search, status seg, method seg, Apply, Clear, - HAR, Postman); detail action row (Clone to composer, cURL, fetch, JSON, Related events); redacted - metadata pane. +2. **Network** — captured HTTP list + detail; toolbar (search, status seg, method seg, service drop, + HAR, Postman, Clear) over a "More filters" disclosure; redacted metadata pane whose actions + (flag, pin, resend, clone, mock/unmock, cURL, fetch) live in the head's **⋮ menu** and whose tabs + lead with Summary — see §4a. 3. **WebSockets** — socket sessions (URL, STATE/SENT/RECV), frame list per socket. 4. **Timeline** — unified event timeline (search). 5. **Crashes** — crash/ANR list + detail: kind/thread badges, breadcrumb strip, the all-thread dump @@ -427,6 +487,10 @@ The final web visual contract relies on a rigid separation of concerns. `dashboa next step: the device's More screen, the `adb forward tcp:8080 tcp:8080` command (with its caveat that 8080 is only the first port tried), and the two browser access modes: a bare URL by default or the `#code=` credential when SESSION_CODE is selected. +- **Network's detail head carried three stacked strips** (facts, actions, tabs) before the panes + even started, each duplicating something already on screen — the facts repeated the compare + panes' "General" groups. Facts moved into the Summary tab, the General groups went away, the + actions row became a ⋮ menu, and Summary was ordered first. See §4a. - **Cross-surface parity was undecided** — resolved and documented in §4c, with a full 16-row table of which web views the Android inspector mirrors, which it deliberately doesn't (Composer, capture rules, Overview), and one open naming mismatch (Timeline / Logs) left as a documented gap rather @@ -435,6 +499,8 @@ The final web visual contract relies on a rigid separation of concerns. `dashboa **Open / worth a design eye:** - **Table/detail density** on Network/Database detail panes — readability at scale — is still unaddressed by this pass. +- **Timeline's detail head is hand-rolled** rather than built from `detailHeadHtml` like the other + three split views (§4a), so it drifts from them silently whenever the shared helper changes. - **The Timeline / Logs naming mismatch** (§4c) — same underlying event stream, different name on each surface. Documented, not fixed; fixing it is a code change (renaming `ObserveTab.LOGS` or the web view, or both to a shared name), out of scope for a documentation-only pass. diff --git a/docs/SECURITY_AND_REDACTION.md b/docs/SECURITY_AND_REDACTION.md index 7634a8e..b748e41 100644 --- a/docs/SECURITY_AND_REDACTION.md +++ b/docs/SECURITY_AND_REDACTION.md @@ -11,11 +11,10 @@ regardless of which inspector captured it. ## Default policy -`RedactionPolicy.default()`'s 25 sensitive field names (case-insensitive, matched against header +`RedactionPolicy.default()`'s sensitive field names (case-insensitive, matched against header names, JSON keys, and form field names): ```text -authorization proxy-authorization www-authenticate authentication cookie set-cookie x-api-key api-key apikey x-auth-token x-access-token x-csrf-token x-xsrf-token access_token refresh_token id_token @@ -24,9 +23,7 @@ passphrase secret client_secret private_key session_id ``` -A matched value is replaced wholesale with ``. Independently of the field-name list, any -`Bearer ` text pattern is redacted wherever it appears (e.g. inside a body that isn't valid -JSON/form data), so a leaked bearer token in a non-standard location is still caught. +A matched value is replaced wholesale with ``. `Authorization` headers and Bearer tokens are kept unredacted by default to allow developers to inspect and copy tokens during debugging; custom `textPatterns` and additional `sensitiveFieldNames` can be configured via `RedactionPolicy` if further redaction is desired. ## Redaction strategies beyond removal diff --git a/docs/THREAT_MODEL.md b/docs/THREAT_MODEL.md index 266f19b..791b6a0 100644 --- a/docs/THREAT_MODEL.md +++ b/docs/THREAT_MODEL.md @@ -165,8 +165,8 @@ a remedy, not a proof of control. ## Redaction is an allowlist, and allowlists miss things -`RedactionPolicy.default()` matches a fixed set of ~25 field names (case-insensitively) against -header names, JSON keys, and form/query field names, plus one regex for `Bearer ` text. See +`RedactionPolicy.default()` matches a set of sensitive field names (case-insensitively) against +header names, JSON keys, and form/query field names. See [SECURITY_AND_REDACTION.md](SECURITY_AND_REDACTION.md) for the exact list. Everything not on that list is transmitted and stored **verbatim**. In practice that means: diff --git a/gradle-plugin/src/main/kotlin/io/devconsole/gradle/DevConsoleVariantPolicyPlugin.kt b/gradle-plugin/src/main/kotlin/io/devconsole/gradle/DevConsoleVariantPolicyPlugin.kt index c47dfe1..0646401 100644 --- a/gradle-plugin/src/main/kotlin/io/devconsole/gradle/DevConsoleVariantPolicyPlugin.kt +++ b/gradle-plugin/src/main/kotlin/io/devconsole/gradle/DevConsoleVariantPolicyPlugin.kt @@ -42,6 +42,18 @@ import java.util.zip.ZipInputStream enum class DevConsoleVariantPolicy { ENABLED, DISABLED, PROTECTED } +/** + * One protected variant's packaged-artifact scans, split by the lifecycle task that produces each + * format so neither ever drags the other's packaging pipeline into the build. + * + * @property assembled scans what `assemble` produces -- the APK, or the AAR on a library. + * @property bundled scans what `bundle` produces. Null on a library, which has no bundle. + */ +internal data class VariantPackagedScans( + val assembled: TaskProvider, + val bundled: TaskProvider?, +) + abstract class DevConsoleExtension { /** * Variants that are allowed to carry the real DevConsole runtime. Defaults to `["debug"]`, which @@ -86,6 +98,22 @@ abstract class DevConsoleExtension { abstract val defaultPolicy: Property abstract val failBuildOnUnsafeVariant: Property + /** + * Whether verification runs as part of `assemble` / `bundle`. Defaults to true, + * and to the `devconsole.verification.enabled` Gradle property when a host sets one. + * + * The kill switch exists because this plugin sits directly on the release path, and a plugin that + * can stop a release from being built is a worse problem than the one it was written to prevent. + * If it ever misbehaves, `-Pdevconsole.verification.enabled=false` gets a build out the door + * without editing build files -- which matters when the build is being run from Android Studio's + * signing wizard rather than a command line. It logs a warning every time, and the verification + * tasks stay registered so `verifyDevConsoleProtectedArtifacts` still runs them on demand. + * + * Turning it off does not make a release safe to ship: it makes it unverified. Use it to unblock, + * then put it back. + */ + abstract val verificationEnabled: Property + /** * Adds the right DevConsole artifact per variant so hosts do not hand-write the debug/release * split -- writing `implementation` instead of `debugImplementation` is the mistake the variant @@ -159,6 +187,18 @@ abstract class VerifyDevConsoleProtectedArtifactsTask : DefaultTask() { } } +/** + * Scans the packages a single variant produced for DevConsole runtime content. + * + * One instance per variant *per packaging format*, never one covering both. A task's + * [packagedArtifacts] naming an artifact its own lifecycle task does not build pulls that artifact's + * whole production pipeline into the graph, and the plugin has no business deciding that + * `assembleRelease` should also build an AAB. See `registerPackagedArtifactScans`. + * + * This task only ever fails a build over an actual policy violation. A missing or unreadable + * artifact is reported as a warning: the point of the plugin is to keep the debugging runtime out of + * release packages, and it fails at that job just as badly if it stops a release from being built. + */ @DisableCachingByDefault( because = "its report embeds absolute file paths from packagedArtifacts, which are not reproducible " + "across machines/checkouts and would make a cached result misleading", @@ -179,10 +219,20 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { val artifacts = packagedArtifacts.files .flatMap { artifact -> - if (artifact.isDirectory) { - artifact.walkTopDown().filter(File::isFile).toList() - } else { - listOf(artifact) + when { + artifact.isDirectory -> artifact.walkTopDown().filter(File::isFile).toList() + // An artifact this task was pointed at but that the build never produced is a + // task-graph problem, not a policy violation, and must never take a release + // build down with it -- see the class KDoc. Skipping it here keeps the scan + // reporting on what actually got packaged. + artifact.isFile -> listOf(artifact) + else -> { + logger.warn( + "DevConsole: skipping ${artifact.absolutePath} while verifying " + + "${variantName.get()} -- it was not produced by this build.", + ) + emptyList() + } } }.distinctBy(File::getAbsolutePath) .sortedBy(File::getAbsolutePath) @@ -194,10 +244,22 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { sha256 = artifact.sha256(), ) } - val violations = artifacts.flatMap(::scanArtifact).distinct().sorted() + val findings = artifacts.flatMap(::scanArtifactSafely).distinct().sorted() + // An entry too big to read to the end is a hole in the scan's coverage, not evidence that + // anything leaked. Reporting it as a violation failed release builds over the size of R8's + // mapping file -- a build artifact that is not even delivered to a device. It stays in the + // report and in the log so the gap is visible, but it never fails a build on its own. + val (unscannable, violations) = findings.partition { it.contains(SCAN_LIMIT_MARKER) } val report = jsonReport.get() report.parentFile.mkdirs() - report.writeText(reportJson(inspected, violations)) + report.writeText(reportJson(inspected, violations, unscannable)) + if (unscannable.isNotEmpty()) { + logger.warn( + "DevConsole: could not scan ${unscannable.size} oversized entr" + + "${if (unscannable.size == 1) "y" else "ies"} in ${variantName.get()} " + + "-- ${unscannable.joinToString()}", + ) + } if (violations.isNotEmpty()) { val message = "Protected variant ${variantName.get()} contains forbidden DevConsole packaged content: " + @@ -206,14 +268,51 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { } } + /** + * Reads one artifact, downgrading an I/O failure to a warning. A truncated, locked or otherwise + * unreadable package is an environment problem; failing the build over it would block a release + * for a reason that has nothing to do with whether DevConsole leaked into it. + */ + private fun scanArtifactSafely(artifact: File): List = + runCatching { scanArtifact(artifact) }.getOrElse { failure -> + logger.warn( + "DevConsole: could not read ${artifact.absolutePath} while verifying " + + "${variantName.get()} -- ${failure.message}. Skipping it.", + ) + emptyList() + } + + /** + * Forbidden path prefixes matching [entryName], anchored at any path segment rather than only at + * the start. + * + * An APK carries the dashboard at `assets/devconsole-web/`, but an app bundle nests every module + * under its own directory and carries the same files at `base/assets/devconsole-web/`. A plain + * `startsWith` therefore matched inside APKs and silently never matched inside AABs -- a gap that + * stayed hidden only while one task scanned both formats and the APK caught the leak first. + */ + private fun forbiddenPathsIn(entryName: String): List = + FORBIDDEN_ENTRY_PREFIXES.filter { prefix -> + entryName.startsWith(prefix) || entryName.contains("/$prefix") + } + + /** + * Whether an entry is app-bundle build metadata rather than shipped content. + * + * `BUNDLE-METADATA/` holds R8's `proguard.map` and the dependency manifest. Play strips it, so + * nothing under it can carry DevConsole onto a device -- and reading it is actively harmful: the + * mapping file for a large app runs past this scanner's read limit, and it names every class the + * build ever saw, so scanning it invites failures that say nothing about what shipped. + */ + private fun isBuildMetadata(entryName: String): Boolean = entryName.startsWith("BUNDLE-METADATA/") + private fun scanArtifact(artifact: File): List = when (artifact.extension.lowercase()) { "apk", "aab", "aar", "jar", "zip" -> ZipFile(artifact).use { zip -> - zip.entries().asSequence().flatMap { entry -> + zip.entries().asSequence().filterNot { isBuildMetadata(it.name) }.flatMap { entry -> val pathMatches = - FORBIDDEN_ENTRY_PREFIXES - .filter { entry.name.startsWith(it) } + forbiddenPathsIn(entry.name) .map { signature -> "${artifact.name}!/${entry.name} [$signature]" } if (entry.isDirectory) { pathMatches.asSequence() @@ -247,9 +346,7 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { val entry = nested.nextEntry ?: break if (!entry.isDirectory) { val nestedLocation = "$location!/${entry.name}" - FORBIDDEN_ENTRY_PREFIXES - .filter { entry.name.startsWith(it) } - .forEach { add("$nestedLocation [$it]") } + forbiddenPathsIn(entry.name).forEach { add("$nestedLocation [$it]") } addAll(scanBounded(nested, MAX_NESTED_ENTRY_BYTES, nestedLocation)) } nested.closeEntry() @@ -268,7 +365,7 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { ): List { val bounded = input.readBytesBounded(limit) return scanBytes(bounded.bytes, location) + - if (bounded.limitExceeded) listOf("$location [scan-limit-exceeded:$limit]") else emptyList() + if (bounded.limitExceeded) listOf("$location [$SCAN_LIMIT_MARKER$limit]") else emptyList() } private fun scanBytes( @@ -323,6 +420,7 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { private fun reportJson( inspected: List, violations: List, + unscannable: List, ): String = buildString { append("{\n \"variant\":\"").append(variantName.get().jsonEscape()).append("\",\n") @@ -344,6 +442,12 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { append("\n \"").append(violation.jsonEscape()).append('"') } if (violations.isNotEmpty()) append('\n').append(" ") + append("],\n \"unscannable\":[") + unscannable.forEachIndexed { index, entry -> + if (index > 0) append(',') + append("\n \"").append(entry.jsonEscape()).append('"') + } + if (unscannable.isNotEmpty()) append('\n').append(" ") append("]\n}\n") } @@ -373,6 +477,9 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { ) private companion object { + /** Tags an entry the scanner could not read to the end. Never a violation -- see `verify`. */ + const val SCAN_LIMIT_MARKER = "scan-limit-exceeded:" + const val MAX_SCANNED_ENTRY_BYTES = 64 * 1024 * 1024 const val MAX_NESTED_ENTRY_BYTES = 16 * 1024 * 1024 @@ -403,6 +510,9 @@ abstract class VerifyDevConsolePackagedArtifactTask : DefaultTask() { // Bump in step with SDK_VERSION in convention-publishing; the tag must exist before this resolves. private const val DEFAULT_SDK_VERSION = "1.3.1" +/** Gradle property backing [DevConsoleExtension.verificationEnabled]. */ +private const val VERIFICATION_ENABLED_PROPERTY = "devconsole.verification.enabled" + /** JitPack's group for this repo — what auto-wiring declares. */ private const val DEVCONSOLE_GROUP = "com.github.devconsole-android.DevConsole" @@ -418,7 +528,7 @@ private val CORE_RUNTIME_COORDINATE_NAMES = setOf("devconsole", "devconsole-noop class DevConsoleVariantPolicyPlugin : Plugin { override fun apply(target: Project) = with(target) { - val packagedVerificationTasks = linkedMapOf>() + val packagedVerificationTasks = linkedMapOf() // variant name -> AGP build type name (e.g. "productionDebug" -> "debug"). Populated by the // AndroidComponentsExtension#onVariants callbacks in registerPackagedArtifactScans below. The // build type is what lets a flavored variant like "productionDebug" resolve against @@ -451,6 +561,9 @@ class DevConsoleVariantPolicyPlugin : Plugin { // restore the old release-pattern-only behavior. defaultPolicy.convention(DevConsoleVariantPolicy.PROTECTED) failBuildOnUnsafeVariant.convention(true) + verificationEnabled.convention( + providers.gradleProperty(VERIFICATION_ENABLED_PROPERTY).map(String::toBoolean).orElse(true), + ) autoWireDependencies.convention(true) sdkVersion.convention(DEFAULT_SDK_VERSION) } @@ -512,9 +625,25 @@ class DevConsoleVariantPolicyPlugin : Plugin { } } + /** + * Registers the packaged-artifact scans for every protected variant, **one task per packaging + * format**, and records them in [scans] so `registerProtectedVerifier` can hang each one off the + * lifecycle task that actually produces its format. + * + * A task's inputs decide what Gradle builds. When a single task named both `SingleArtifact.APK` + * and `SingleArtifact.BUNDLE` and hung off `assemble`, asking for a release APK also + * built the AAB -- `packageReleaseBundle`, `signReleaseBundle` and + * `produceBundleIdeListingFile` all joined an APK-only graph. Besides the wasted build + * time, it broke Android Studio's "Generate Signed APK" wizard outright: the wizard passes + * `-Pandroid.injected.apk.location`, which puts AGP's final AAB path *inside* + * `package`'s output directory, and Gradle then rejects + * `produceBundleIdeListingFile` for consuming that directory without declaring a + * dependency on `package`. Keeping each format on its own task means the plugin only + * ever asks for artifacts the build was already going to produce. + */ private fun Project.registerPackagedArtifactScans( extension: DevConsoleExtension, - scans: MutableMap>, + scans: MutableMap, actualVariantBuildTypes: MutableMap, ) { plugins.withId("com.android.application") { @@ -525,22 +654,27 @@ class DevConsoleVariantPolicyPlugin : Plugin { if (variantPolicy(variant.name, variant.buildType, extension) != DevConsoleVariantPolicy.PROTECTED) { return@onVariants } - scans[variant.name] = - tasks.register( - "verify${variant.name.capitalized()}DevConsolePackagedArtifact", + scans[variant.name] = VariantPackagedScans( + // Keeps the historical task name: this is the scan on the assemble path, the + // one hosts and CI already invoke. + assembled = registerPackagedScan( + extension = extension, + taskName = "verify${variant.name.capitalized()}DevConsolePackagedArtifact", + variantName = variant.name, + reportName = "${variant.name}-artifacts", ) { - group = "verification" - variantName.set(variant.name) - failOnUnsafeVariant.set(extension.failBuildOnUnsafeVariant) - packagedArtifacts.from(variant.artifacts.get(SingleArtifact.APK)) - packagedArtifacts.from(variant.artifacts.get(SingleArtifact.BUNDLE)) - packagedArtifacts.from(variant.artifacts.get(SingleArtifact.MERGED_MANIFEST)) - jsonReport.set( - layout.buildDirectory.file( - "reports/devconsole/${variant.name}-artifacts.json", - ).map { it.asFile }, - ) - } + it.from(variant.artifacts.get(SingleArtifact.APK)) + it.from(variant.artifacts.get(SingleArtifact.MERGED_MANIFEST)) + }, + bundled = registerPackagedScan( + extension = extension, + taskName = "verify${variant.name.capitalized()}DevConsolePackagedBundle", + variantName = variant.name, + reportName = "${variant.name}-bundle", + ) { + it.from(variant.artifacts.get(SingleArtifact.BUNDLE)) + }, + ) } } plugins.withId("com.android.library") { @@ -551,25 +685,40 @@ class DevConsoleVariantPolicyPlugin : Plugin { if (variantPolicy(variant.name, variant.buildType, extension) != DevConsoleVariantPolicy.PROTECTED) { return@onVariants } - scans[variant.name] = - tasks.register( - "verify${variant.name.capitalized()}DevConsolePackagedArtifact", + // A library has no bundle task, so there is no second format to scan. + scans[variant.name] = VariantPackagedScans( + assembled = registerPackagedScan( + extension = extension, + taskName = "verify${variant.name.capitalized()}DevConsolePackagedArtifact", + variantName = variant.name, + reportName = "${variant.name}-artifacts", ) { - group = "verification" - variantName.set(variant.name) - failOnUnsafeVariant.set(extension.failBuildOnUnsafeVariant) - packagedArtifacts.from(variant.artifacts.get(SingleArtifact.AAR)) - packagedArtifacts.from(variant.artifacts.get(SingleArtifact.MERGED_MANIFEST)) - jsonReport.set( - layout.buildDirectory.file( - "reports/devconsole/${variant.name}-artifacts.json", - ).map { it.asFile }, - ) - } + it.from(variant.artifacts.get(SingleArtifact.AAR)) + it.from(variant.artifacts.get(SingleArtifact.MERGED_MANIFEST)) + }, + bundled = null, + ) } } } + private fun Project.registerPackagedScan( + extension: DevConsoleExtension, + taskName: String, + variantName: String, + reportName: String, + artifacts: (ConfigurableFileCollection) -> Unit, + ): TaskProvider = + tasks.register(taskName) { + group = "verification" + this.variantName.set(variantName) + failOnUnsafeVariant.set(extension.failBuildOnUnsafeVariant) + artifacts(packagedArtifacts) + jsonReport.set( + layout.buildDirectory.file("reports/devconsole/$reportName.json").map { it.asFile }, + ) + } + /** * Resolves a variant's policy. [buildType] is the AGP build-type name backing this variant (e.g. * "debug" for the flavored variant "productionDebug") -- read from AGP's own variant model rather @@ -677,23 +826,33 @@ class DevConsoleVariantPolicyPlugin : Plugin { policies: Map, report: TaskProvider, extension: DevConsoleExtension, - packagedVerificationTasks: Map>, + packagedScans: Map, ) { val protectedPaths = extension.protectedDependencyPaths.get() val protectedVariants = policies.filterValues { it == DevConsoleVariantPolicy.PROTECTED }.keys + val wireOntoLifecycleTasks = extension.verificationEnabled.get() + if (!wireOntoLifecycleTasks) { + logger.warn( + "DevConsole: verification is switched off for project '$path' via " + + "$VERIFICATION_ENABLED_PROPERTY, so assemble/bundle will not check that the " + + "DevConsole runtime stayed out of ${protectedVariants.joinToString()}. Anything " + + "built this way is unverified -- switch it back on before shipping.", + ) + } // One verifier task per protected variant, never a single shared one. A shared task carries // every protected variant's runtime classpath and packaged artifact as its own inputs, so // wiring it onto assemble made building one variant resolve -- and on a flavored // project fully build -- every other protected variant too: `assembleProductionRelease` // dragged `dexBuilderStagingRelease`, `bundleStagingRelease` and every other flavor's // dependency download into the graph. Per-variant tasks keep each variant's inputs to itself. - val verifiers = protectedVariants.map { variant -> + val verifiers = protectedVariants.flatMap { variant -> + // Dependency-graph checks only: no packaged artifact among its inputs, so it is safe to + // hang off both lifecycle tasks without deciding anything about what gets packaged. val verifier = tasks.register( "verify${variant.capitalized()}DevConsoleProtectedArtifacts", ) { group = "verification" dependsOn(report) - packagedVerificationTasks[variant]?.let { packaged -> dependsOn(packaged) } violations.set(declaredViolations(variant, protectedPaths)) resolvedRuntimeComponents.put(variant, resolvedRuntimeComponents(variant)) protectedProjectPaths.set(protectedPaths) @@ -706,14 +865,28 @@ class DevConsoleVariantPolicyPlugin : Plugin { // lazy and safe even when a task name does not exist for this project type (e.g. // bundle on a library module, which has no bundle task) -- it never forces the task // to be created, it just configures it if and when it is. + // + // Each packaged scan goes on the lifecycle task that produces its format, and nowhere else. + // Hanging the AAB scan off assemble is what made a release APK build an AAB it was + // never asked for -- see registerPackagedArtifactScans for what that broke. + val scans = packagedScans[variant] val capitalized = variant.capitalized() - tasks.matching { it.name == "assemble$capitalized" }.configureEach { it.dependsOn(verifier) } - tasks.matching { it.name == "bundle$capitalized" }.configureEach { it.dependsOn(verifier) } - verifier + if (wireOntoLifecycleTasks) { + tasks.matching { it.name == "assemble$capitalized" }.configureEach { assemble -> + assemble.dependsOn(verifier) + scans?.assembled?.let { scan -> assemble.dependsOn(scan) } + } + tasks.matching { it.name == "bundle$capitalized" }.configureEach { bundle -> + bundle.dependsOn(verifier) + scans?.bundled?.let { scan -> bundle.dependsOn(scan) } + } + } + listOfNotNull(verifier, scans?.assembled, scans?.bundled) } // Aggregate lifecycle task: the name hosts and CI already invoke, and what `check` hangs off. // Asking for it explicitly still verifies every protected variant -- it just is no longer on - // any single variant's build path. + // any single variant's build path. This one deliberately covers every format, so `check` on a + // flavored app does build the AABs; that is the price of asking for full verification by name. val aggregate = tasks.register("verifyDevConsoleProtectedArtifacts") { task -> task.group = "verification" task.description = "Verifies every protected variant excludes the full DevConsole runtime." diff --git a/gradle-plugin/src/test/kotlin/io/devconsole/gradle/DevConsoleVariantPolicyPluginFunctionalTest.kt b/gradle-plugin/src/test/kotlin/io/devconsole/gradle/DevConsoleVariantPolicyPluginFunctionalTest.kt index e94898a..51d4bbd 100644 --- a/gradle-plugin/src/test/kotlin/io/devconsole/gradle/DevConsoleVariantPolicyPluginFunctionalTest.kt +++ b/gradle-plugin/src/test/kotlin/io/devconsole/gradle/DevConsoleVariantPolicyPluginFunctionalTest.kt @@ -10,6 +10,8 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream class DevConsoleVariantPolicyPluginFunctionalTest { @get:Rule @@ -117,6 +119,25 @@ class DevConsoleVariantPolicyPluginFunctionalTest { ) } + /** + * Writes a zip into the fixture's project directory. Built here rather than in the fixture's + * build script because Kotlin DSL resolves `java` to Gradle's `java` extension, so a script + * cannot name `java.util.zip` at all. + */ + private fun writeZip( + relativePath: String, + vararg entries: Pair, + ) { + val target = File(projectDir.root, relativePath).apply { parentFile.mkdirs() } + ZipOutputStream(target.outputStream().buffered()).use { zip -> + entries.forEach { (name, bytes) -> + zip.putNextEntry(ZipEntry(name)) + zip.write(bytes) + zip.closeEntry() + } + } + } + private fun runner(vararg args: String): GradleRunner = GradleRunner.create() // AGP 8.13.0 requires Gradle 8.13+ and is not compatible with Gradle 9.x, so pin the TestKit // daemon to 8.14.3 (the repo's own wrapper version) rather than inheriting any 9.x Gradle. @@ -1005,10 +1026,262 @@ class DevConsoleVariantPolicyPluginFunctionalTest { assertTrue(result.output, !result.output.contains("PartnerRelease")) } + @Test + fun `assembleRelease builds a clean protected variant to completion without building an app bundle`() { + writeFixture( + devConsoleBlock = "", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + ) + + val result = runner("assembleRelease").build() + + assertTrue(result.output, result.output.contains("BUILD SUCCESSFUL")) + assertTrue(result.output, result.output.contains(":verifyReleaseDevConsolePackagedArtifact")) + // The packaged scan naming SingleArtifact.BUNDLE alongside SingleArtifact.APK dragged the whole + // bundle pipeline into every release APK build -- wasted work, and the direct cause of the + // wizard failure the next test covers. + BUNDLE_PIPELINE_TASKS.forEach { task -> + assertTrue(result.output, !result.output.contains(task)) + } + } + + @Test + fun `assembleRelease builds to completion from the Studio signed-APK wizard's injected output location`() { + writeFixture( + devConsoleBlock = "", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + ) + val destination = File(projectDir.root, "wizard-out").apply { mkdirs() } + + // What Android Studio's "Generate Signed Bundle / APK" wizard passes. It puts AGP's final AAB + // path inside package's output directory, so any graph holding both package + // and produceBundleIdeListingFile is rejected by Gradle for an undeclared dependency. + // An APK build has no business containing the latter; it only did because of the packaged scan. + val result = runner( + "assembleRelease", + "-Pandroid.injected.apk.location=${destination.absolutePath}", + ).build() + + assertTrue(result.output, result.output.contains("BUILD SUCCESSFUL")) + } + + @Test + fun `bundleRelease builds to completion from the Studio signed-bundle wizard's injected output location`() { + writeFixture( + devConsoleBlock = "", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + ) + val destination = File(projectDir.root, "wizard-out").apply { mkdirs() } + + val result = runner( + "bundleRelease", + "-Pandroid.injected.apk.location=${destination.absolutePath}", + ).build() + + assertTrue(result.output, result.output.contains("BUILD SUCCESSFUL")) + assertTrue(result.output, result.output.contains(":verifyReleaseDevConsolePackagedBundle")) + } + + @Test + fun `bundleRelease still opens the app bundle and rejects content only the package reveals`() { + writeFixture( + devConsoleBlock = "autoWireDependencies.set(false)", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + ) + // Nothing in the dependency graph gives this away -- only reading the packaged bytes does. So + // a failure here proves the AAB itself was opened, which is the coverage the split must keep. + File(projectDir.root, "src/main/assets/devconsole-web/index.html").apply { parentFile.mkdirs() } + .writeText("forbidden dashboard") + + val result = runner("bundleRelease").buildAndFail() + + assertTrue(result.output, result.output.contains("assets/devconsole-web/")) + assertTrue(result.output, result.output.contains(".aab!/")) + } + + @Test + fun `a packaged artifact the build never produced is a warning, not a failed release`() { + writeFixture( + devConsoleBlock = "", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + // Points a scan at a path nothing writes. Whatever leaves an expected artifact missing -- + // a task-graph mistake here, an AGP change, a host's own output redirection -- must never + // be the reason a release build fails. This used to throw FileNotFoundException. + extraBuildScript = + """ + tasks.register("verifyMissingArtifact") { + variantName.set("release") + failOnUnsafeVariant.set(true) + packagedArtifacts.from(layout.buildDirectory.file("nowhere/absent.aab")) + jsonReport.set(layout.buildDirectory.file("reports/devconsole/absent.json").map { it.asFile }) + } + """.trimIndent(), + ) + + val result = runner("verifyMissingArtifact").build() + + assertTrue(result.output, result.output.contains("BUILD SUCCESSFUL")) + assertTrue(result.output, result.output.contains("it was not produced by this build")) + } + + @Test + fun `an entry too large to read through is reported but does not fail the build`() { + writeFixture( + devConsoleBlock = "autoWireDependencies.set(false)", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + // A real liveRelease AAB failed on exactly this: R8's mapping file ran past the scanner's + // 64 MiB read limit, and the resulting "scan-limit-exceeded" note was counted as a + // violation. Not finishing a read says nothing about what leaked, and must never stop a + // release. Hence the deliberately oversized entry below. + extraBuildScript = + """ + tasks.register("verifyOversized") { + variantName.set("release") + failOnUnsafeVariant.set(true) + packagedArtifacts.from(file("oversized.zip")) + jsonReport.set(layout.buildDirectory.file("reports/devconsole/oversized.json").map { it.asFile }) + } + """.trimIndent(), + ) + // One entry past the scanner's 64 MiB read limit. Harmless content: the point is that the read + // cannot finish, not what is in it. + writeZip("oversized.zip", "huge.txt" to ByteArray(65 * 1024 * 1024) { 'a'.code.toByte() }) + + val result = runner("verifyOversized").build() + + assertTrue(result.output, result.output.contains("BUILD SUCCESSFUL")) + assertTrue(result.output, result.output.contains("could not scan")) + val report = File(projectDir.root, "build/reports/devconsole/oversized.json").readText() + assertTrue(report, report.contains("scan-limit-exceeded")) + assertTrue(report, report.contains("\"violations\":[]")) + } + + @Test + fun `app bundle build metadata is left out of the scan`() { + writeFixture( + devConsoleBlock = "autoWireDependencies.set(false)", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + // BUNDLE-METADATA is Play-stripped build output, so a DevConsole name appearing there says + // nothing about what reaches a device -- R8's mapping file names every class the build saw. + extraBuildScript = + """ + tasks.register("verifyFakeBundle") { + variantName.set("release") + failOnUnsafeVariant.set(true) + packagedArtifacts.from(file("fake.aab")) + jsonReport.set(layout.buildDirectory.file("reports/devconsole/fake-bundle.json").map { it.asFile }) + } + """.trimIndent(), + ) + writeZip( + "fake.aab", + "BUNDLE-METADATA/com.android.tools.build.obfuscation/proguard.map" to + "DEVCONSOLE_ENABLED_FULL_V1".toByteArray(), + ) + + val result = runner("verifyFakeBundle").build() + + assertTrue(result.output, result.output.contains("BUILD SUCCESSFUL")) + } + + @Test + fun `the verification kill switch lets a violating release build ship anyway, loudly`() { + writeFixture( + devConsoleBlock = "autoWireDependencies.set(false)", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + ) + File( + projectDir.root, + "src/main/java/io/devconsole/internal/enabled/FullRuntimeMarker.java", + ).apply { parentFile.mkdirs() } + .writeText( + """ + package io.devconsole.internal.enabled; + public final class FullRuntimeMarker { + public static final String SIGNATURE = "DEVCONSOLE_ENABLED_FULL_V1"; + } + """.trimIndent(), + ) + + // Same project that `assembleRelease fails a violating project, not just check` proves is + // rejected. Nothing this plugin does should ever be the reason a release cannot be built at + // all, so there has to be a way past it that does not involve editing build files. + val result = runner("assembleRelease", "-Pdevconsole.verification.enabled=false").build() + + assertTrue(result.output, result.output.contains("BUILD SUCCESSFUL")) + assertTrue(result.output, result.output.contains("verification is switched off")) + assertTrue(result.output, !result.output.contains(":verifyReleaseDevConsolePackagedArtifact")) + } + + @Test + fun `the kill switch leaves the verification tasks runnable by name`() { + writeFixture( + devConsoleBlock = "autoWireDependencies.set(false)", + androidPlugin = "com.android.application", + applicationIdLine = + """ + applicationId = "io.devconsole.fixture" + versionCode = 1 + """.trimIndent(), + ) + File(projectDir.root, "src/main/assets/devconsole-web/index.html").apply { parentFile.mkdirs() } + .writeText("forbidden dashboard") + + // Off the release path, not gone: a host that switched the wiring off can still audit. + val result = runner("verifyDevConsoleProtectedArtifacts", "-Pdevconsole.verification.enabled=false") + .buildAndFail() + + assertTrue(result.output, result.output.contains("assets/devconsole-web/")) + } + private companion object { /** A valid, empty zip -- enough for Gradle to accept the stub as a jar artifact. */ val EMPTY_JAR_BYTES = byteArrayOf(0x50, 0x4B, 0x05, 0x06) + ByteArray(18) + + /** Tasks that only ever run when the build was actually asked for an app bundle. */ + val BUNDLE_PIPELINE_TASKS = + listOf(":packageReleaseBundle", ":signReleaseBundle", ":produceReleaseBundleIdeListingFile") } } diff --git a/samples/compose-app/src/main/kotlin/io/devconsole/sample/compose/MainActivity.kt b/samples/compose-app/src/main/kotlin/io/devconsole/sample/compose/MainActivity.kt index e6bf7cb..484ed4e 100644 --- a/samples/compose-app/src/main/kotlin/io/devconsole/sample/compose/MainActivity.kt +++ b/samples/compose-app/src/main/kotlin/io/devconsole/sample/compose/MainActivity.kt @@ -112,14 +112,33 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody import org.eclipse.paho.client.mqttv3.MqttAsyncClient import org.eclipse.paho.client.mqttv3.MqttConnectOptions import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence import java.io.File import java.util.concurrent.atomic.AtomicInteger +/** Public read endpoint: answers gzipped and chunked, which is what the capture tee is built for. */ +private const val SAMPLE_GET_URL = "https://jsonplaceholder.typicode.com/todos/1" + +/** + * Public write endpoint. It echoes the posted object back with an `id`, so one tap produces a + * capture carrying a real request body *and* a real response body -- the pair every other sample + * action leaves half empty, and the only way to exercise the request side of the inspector + * (payload viewer, cURL/fetch reproduction, mock-from-request) against live traffic. + * + * Nothing is persisted: jsonplaceholder fakes the write and always answers 201 with id 101. + */ +private const val SAMPLE_POST_URL = "https://jsonplaceholder.typicode.com/posts" + +/** Sent by [SampleActivity.sendJsonPost]; small enough to read whole in either inspector. */ +private const val SAMPLE_POST_BODY = + """{"title":"DevConsole sample","body":"Posted from the compose sample app","userId":9912837}""" + private const val SHOW_ORDER_HISTORY_FLAG = "compose_sample.show_order_history" private const val MOCK_RULE_ID = "compose-sample-orders" @@ -251,7 +270,12 @@ private fun devConsoleStatusText( * owns the server lifecycle and builds its own launch surface on top of [DevConsole.state], but can * also drop into the SDK's own in-app inspector (More screen QR, Data rail, exports) via * [DevConsole.open]. + * + * Carries one private method per capability it demonstrates (GET, POST, Ktor, MQTT, screenshot, + * push, ...), which is what the `TooManyFunctions` suppression below covers: splitting them across + * helper classes would put each demonstration somewhere other than the screen that offers it. */ +@Suppress("TooManyFunctions") // One private method per capability demonstrated; see the class doc. class MainActivity : ComponentActivity() { private val socketClient = OkHttpClient() @@ -274,9 +298,14 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) DevConsole.initialize(application, buildConfig()) - // Mock rules are SESSION-scoped and dropped whenever the embedded server restarts (fresh - // session), so the rule is (re)installed from the Running-state observer below rather than - // once here -- a one-shot install here would silently stop mocking after any restart. + // Installed here AND re-installed from the Running-state observer below. Both are needed: + // mock rules are SESSION-scoped and DevConsole's stop sequence calls clearSessionRules(), + // so a one-shot install would silently stop mocking after any restart -- but installing + // *only* on Running left the rule missing until the embedded server was first started, + // which made a fresh launch show an empty Mocks list and send "Send mocked request" + // to the real network. Mocking is in-process and needs no server, so it should not wait + // for one. + installMockRule() seedSampleData(applicationContext) setContent { @@ -399,7 +428,7 @@ class MainActivity : ComponentActivity() { if (state is DevConsoleState.Running) { endpoint = DevConsole.endpoint() // Re-seed on every Running transition (first start AND every restart) -- see the - // comment in onCreate for why a one-shot install at init isn't enough. + // comment in onCreate for why the install there isn't enough on its own. installMockRule() // Poll while running so either access mode updates the displayed URL after a restart; // SESSION_CODE also re-issues the code after a browser consumes it. @@ -505,16 +534,20 @@ class MainActivity : ComponentActivity() { SectionLabel("Exercise the SDK") CapabilityCard( - title = "Send network request", + title = "Send GET request", subtitle = "OkHttp interceptor -- chunked response, body captured via the tee", onClick = { scope.launch { - lastResponse = - sendRequest("https://jsonplaceholder.typicode.com/todos/1", "Network response") + lastResponse = sendRequest(SAMPLE_GET_URL, "GET response") showOrderHistory = DevConsole.featureFlagValue(SHOW_ORDER_HISTORY_FLAG) } }, ) + CapabilityCard( + title = "Send POST request", + subtitle = "JSON request body and JSON response body -- both captured on the same transaction", + onClick = { scope.launch { lastResponse = sendJsonPost() } }, + ) CapabilityCard( title = "Send Ktor request", subtitle = "Ktor plugin on the CIO engine -- request and response bodies, no OkHttp involved", @@ -630,6 +663,37 @@ class MainActivity : ComponentActivity() { } } + /** + * Posts [SAMPLE_POST_BODY] to [SAMPLE_POST_URL] through the same instrumented client the GET + * card uses, so the capture carries both bodies. + * + * The request body goes through OkHttp's own `RequestBody`, not a pre-serialized string handed + * to the recorder: the capture interceptor reads what OkHttp actually writes to the wire, so + * what the inspector shows is the request as sent rather than as intended. `Content-Type` comes + * from the media type here (not a manual header) for the same reason -- it is the one the call + * really carries. + */ + private suspend fun sendJsonPost(): String = + withContext(Dispatchers.IO) { + requestCount.incrementAndGet() + try { + val request = + Request + .Builder() + .url(SAMPLE_POST_URL) + .post(SAMPLE_POST_BODY.toRequestBody("application/json; charset=utf-8".toMediaType())) + .build() + instrumentedClient.newCall(request).execute().use { response -> + val body = response.body?.string().orEmpty() + "POST response: ${response.code} (${body.length} chars)" + } + } catch (cancellation: kotlinx.coroutines.CancellationException) { + throw cancellation + } catch (error: Exception) { + "POST failed: ${error.message ?: error.javaClass.simpleName}" + } + } + /** * Issues one request through the sample's Ktor client, whose debug build installs * `DevConsoleKtorClientPlugin` (see `SampleKtorClient` in `src/debug`). @@ -963,7 +1027,12 @@ private fun ConnectUrlPanel( TextButton( onClick = { clipboard.setText(AnnotatedString(connectUrl)) - val message = if (usesSessionCode) "Session code copied -- keep it private" else "Dashboard URL copied" + val message = + if (usesSessionCode) { + "Session code copied -- keep it private" + } else { + "Dashboard URL copied" + } Toast.makeText(context, message, Toast.LENGTH_SHORT).show() }, modifier = Modifier.align(Alignment.End), diff --git a/samples/foundation-app/src/main/kotlin/io/devconsole/sample/MainActivity.kt b/samples/foundation-app/src/main/kotlin/io/devconsole/sample/MainActivity.kt index 81eb5ec..a59e70b 100644 --- a/samples/foundation-app/src/main/kotlin/io/devconsole/sample/MainActivity.kt +++ b/samples/foundation-app/src/main/kotlin/io/devconsole/sample/MainActivity.kt @@ -332,7 +332,12 @@ class MainActivity : Activity() { val url = connectUrl ?: return@setOnClickListener val clipboard = getSystemService(ClipboardManager::class.java) clipboard.setPrimaryClip(ClipData.newPlainText("DevConsole dashboard URL", url)) - val message = if (url.contains("#code=")) "Session code copied -- keep it private" else "Dashboard URL copied" + val message = + if (url.contains("#code=")) { + "Session code copied -- keep it private" + } else { + "Dashboard URL copied" + } Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT).show() } } @@ -501,7 +506,7 @@ class MainActivity : Activity() { "No session code is required in the default open mode. Use SESSION_CODE before sharing " + "a LAN-bound dashboard on an untrusted network.\n\n" } + - "This debugging session uses local-network HTTP. Other participants on an untrusted network " + + "This debugging session uses local-network HTTP. Other participants on an untrusted network " + "may observe or modify traffic. Use ADB localhost mode or a trusted isolated network " + "for sensitive testing." statusView.text = message diff --git a/sdk/composer/src/test/kotlin/io/devconsole/composer/ComposerRequestTest.kt b/sdk/composer/src/test/kotlin/io/devconsole/composer/ComposerRequestTest.kt index aeb0641..29a1d7c 100644 --- a/sdk/composer/src/test/kotlin/io/devconsole/composer/ComposerRequestTest.kt +++ b/sdk/composer/src/test/kotlin/io/devconsole/composer/ComposerRequestTest.kt @@ -66,7 +66,7 @@ class ComposerRequestTest { ComposerRequest( method = "POST", url = "https://api.test/orders?access_token=url-canary", - headers = mapOf("Authorization" to "Bearer header-canary", "Accept" to "application/json"), + headers = mapOf("Cookie" to "session=header-canary", "Accept" to "application/json"), body = """{"password":"body-canary","keep":"visible"}""", query = listOf(ComposerQueryParameter("token", "query-canary")), formFields = mapOf("client_secret" to "form-canary"), diff --git a/sdk/core/src/test/kotlin/io/devconsole/core/EventBatchWriterTest.kt b/sdk/core/src/test/kotlin/io/devconsole/core/EventBatchWriterTest.kt index 4bca35d..18d78b5 100644 --- a/sdk/core/src/test/kotlin/io/devconsole/core/EventBatchWriterTest.kt +++ b/sdk/core/src/test/kotlin/io/devconsole/core/EventBatchWriterTest.kt @@ -19,8 +19,8 @@ class EventBatchWriterTest { val writer = EventBatchWriter(store, this, capacity = 2, maxBatchSize = 2, flushIntervalMs = 1000) writer.start() - writer.submit(event("Bearer first-secret")) - writer.submit(event("Bearer second-secret")) + writer.submit(event("password=first-secret")) + writer.submit(event("password=second-secret")) testScheduler.runCurrent() assertEquals(1, store.batches.size) diff --git a/sdk/core/src/test/kotlin/io/devconsole/core/EventPipelineTest.kt b/sdk/core/src/test/kotlin/io/devconsole/core/EventPipelineTest.kt index 928704f..1675545 100644 --- a/sdk/core/src/test/kotlin/io/devconsole/core/EventPipelineTest.kt +++ b/sdk/core/src/test/kotlin/io/devconsole/core/EventPipelineTest.kt @@ -44,14 +44,14 @@ class EventPipelineTest { pluginId = "network", type = "network.request", severity = EventSeverity.INFO, - summary = "Authorization: Bearer raw-token", - tags = mapOf("Authorization" to "Bearer another-token", "route" to "/orders"), + summary = "password=raw-token", + tags = mapOf("Cookie" to "session=another-token", "route" to "/orders"), ), ) val event = pipeline.snapshot().single() assertFalse(event.summary.contains("raw-token")) - assertEquals("", event.tags.getValue("Authorization")) + assertEquals("", event.tags.getValue("Cookie")) assertEquals("/orders", event.tags.getValue("route")) } diff --git a/sdk/export/src/test/kotlin/io/devconsole/export/EventExportWriterTest.kt b/sdk/export/src/test/kotlin/io/devconsole/export/EventExportWriterTest.kt index c303a20..99f100a 100644 --- a/sdk/export/src/test/kotlin/io/devconsole/export/EventExportWriterTest.kt +++ b/sdk/export/src/test/kotlin/io/devconsole/export/EventExportWriterTest.kt @@ -23,7 +23,7 @@ class EventExportWriterTest { ExportRequest( sessionId = "session-1", destination = destination, - events = listOf(event(summary = "request Bearer export-secret")), + events = listOf(event(summary = "password=export-secret")), ), ) @@ -122,7 +122,7 @@ class EventExportWriterTest { "event-1" to TimelineAnnotation( bookmarked = true, - note = "Authorization: Bearer note-secret", + note = "password=note-secret", ), ), ) @@ -131,7 +131,7 @@ class EventExportWriterTest { ZipFile(destination).use { zip -> val timeline = zip.getInputStream(zip.getEntry("timeline.jsonl")).bufferedReader().readText() assertTrue(timeline.contains("\"bookmarked\":true")) - assertTrue(timeline.contains("\"note\":\"Authorization: \"")) + assertTrue(timeline.contains("\"note\":\"password=\"")) assertFalse(timeline.contains("note-secret")) } } finally { @@ -316,7 +316,7 @@ class EventExportWriterTest { val request = evidenceRequest(destination).withEvidenceBundle( baseEvidenceBundle().copy( - reportMarkdown = "Summary: Authorization: Bearer report-secret", + reportMarkdown = "password=report-secret", ), ) diff --git a/sdk/full/src/main/kotlin/io/devconsole/FullInspectorDataSource.kt b/sdk/full/src/main/kotlin/io/devconsole/FullInspectorDataSource.kt index 8b49cf1..473f8c0 100644 --- a/sdk/full/src/main/kotlin/io/devconsole/FullInspectorDataSource.kt +++ b/sdk/full/src/main/kotlin/io/devconsole/FullInspectorDataSource.kt @@ -1336,6 +1336,11 @@ private fun NetworkTransaction.evidenceDetailJson(): String = capture.request.url.path .escapeJson(), ).append('"') + append(",\"query\":\"") + .append( + capture.request.url.queryString + .escapeJson(), + ).append('"') append(",\"status\":").append(capture.response?.statusCode ?: "null") val contentType = (capture.response?.contentType ?: capture.request.contentType).orEmpty() append(",\"contentType\":\"").append(contentType.escapeJson()).append('"') diff --git a/sdk/full/src/test/kotlin/io/devconsole/AndroidFileInspectorTest.kt b/sdk/full/src/test/kotlin/io/devconsole/AndroidFileInspectorTest.kt index 173de14..0348515 100644 --- a/sdk/full/src/test/kotlin/io/devconsole/AndroidFileInspectorTest.kt +++ b/sdk/full/src/test/kotlin/io/devconsole/AndroidFileInspectorTest.kt @@ -50,7 +50,7 @@ class AndroidFileInspectorTest { @Test fun `preview redacts text file contents`() { File(application.filesDir, "secret.txt") - .writeText("authorization: Bearer sk-super-secret-value-1234567890") + .writeText("password=sk-super-secret-value-1234567890") val preview = inspector.preview("files", "secret.txt") diff --git a/sdk/full/src/test/kotlin/io/devconsole/AndroidInspectorExporterTest.kt b/sdk/full/src/test/kotlin/io/devconsole/AndroidInspectorExporterTest.kt index 7a1e156..5e17cc5 100644 --- a/sdk/full/src/test/kotlin/io/devconsole/AndroidInspectorExporterTest.kt +++ b/sdk/full/src/test/kotlin/io/devconsole/AndroidInspectorExporterTest.kt @@ -59,7 +59,7 @@ class AndroidInspectorExporterTest { NetworkRequestInput( method = "GET", url = "https://api.example.test$path", - headers = mapOf("Authorization" to "Bearer $SECRET_TOKEN"), + headers = mapOf("Cookie" to "session=$SECRET_TOKEN"), ), NetworkResponseInput( statusCode = 200, @@ -187,7 +187,7 @@ class AndroidInspectorExporterTest { wallTimeMs = 10, monoTimeNs = 10, severity = 3, - summary = "Checkout failed: Authorization Bearer $SECRET_TOKEN", + summary = "password=$SECRET_TOKEN", tagsJson = "{}", payloadJson = null, ), diff --git a/sdk/full/src/test/kotlin/io/devconsole/AndroidPreferencesInspectorTest.kt b/sdk/full/src/test/kotlin/io/devconsole/AndroidPreferencesInspectorTest.kt index ebd7c42..6c53938 100644 --- a/sdk/full/src/test/kotlin/io/devconsole/AndroidPreferencesInspectorTest.kt +++ b/sdk/full/src/test/kotlin/io/devconsole/AndroidPreferencesInspectorTest.kt @@ -49,12 +49,12 @@ class AndroidPreferencesInspectorTest { application .getSharedPreferences("secret_prefs", Context.MODE_PRIVATE) .edit() - .putString("auth_token", "Bearer sk-super-secret-value-1234567890") + .putString("access_token", "sk-super-secret-value-1234567890") .putInt("count", 3) .commit() val file = inspector.files().first { it.name == "secret_prefs" } - val tokenEntry = file.entries.first { it.key == "auth_token" } + val tokenEntry = file.entries.first { it.key == "access_token" } assertFalse(tokenEntry.value.contains("sk-super-secret-value-1234567890")) assertTrue(tokenEntry.redacted) diff --git a/sdk/full/src/test/kotlin/io/devconsole/CrashCaptureTest.kt b/sdk/full/src/test/kotlin/io/devconsole/CrashCaptureTest.kt index f4b36ed..2408180 100644 --- a/sdk/full/src/test/kotlin/io/devconsole/CrashCaptureTest.kt +++ b/sdk/full/src/test/kotlin/io/devconsole/CrashCaptureTest.kt @@ -96,10 +96,10 @@ class CrashCaptureTest { @Test fun `the crash payload is redacted`() { - capture().recordAnr("main", "called with Authorization: Bearer topsecret123") + capture().recordAnr("main", "password=topsecret123") assertTrue( - "expected the token to be redacted: ${appended.single().payloadJson}", + "expected the password to be redacted: ${appended.single().payloadJson}", "topsecret123" !in appended.single().payloadJson.orEmpty(), ) } diff --git a/sdk/full/src/test/kotlin/io/devconsole/CrashPolicyGatingTest.kt b/sdk/full/src/test/kotlin/io/devconsole/CrashPolicyGatingTest.kt index 0c995c6..4a5531e 100644 --- a/sdk/full/src/test/kotlin/io/devconsole/CrashPolicyGatingTest.kt +++ b/sdk/full/src/test/kotlin/io/devconsole/CrashPolicyGatingTest.kt @@ -27,7 +27,8 @@ import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(sdk = [34]) class CrashPolicyGatingTest { - private fun anrWatchdogThreadRunning(): Boolean = Thread.getAllStackTraces().keys.any { it.name == ANR_THREAD_NAME } + private fun anrWatchdogThreadRunning(): Boolean = + Thread.getAllStackTraces().keys.any { it.name == ANR_THREAD_NAME && it.isAlive } /** * Thread start/interrupt-triggered exit are asynchronous relative to the call that triggers @@ -89,6 +90,7 @@ class CrashPolicyGatingTest { @Test fun `anrWatchdogEnabled = false never starts the watchdog thread`() = runTest { + awaitThreadState(expectedRunning = false, timeoutMs = 2_000L) val provider = PlatformFacadeProvider() val config = DevConsoleConfig.default().withCrashPolicy(CrashPolicy(anrWatchdogEnabled = false)) provider.initialize(ApplicationProvider.getApplicationContext(), config) diff --git a/sdk/full/src/test/kotlin/io/devconsole/FullInspectorDataSourceTest.kt b/sdk/full/src/test/kotlin/io/devconsole/FullInspectorDataSourceTest.kt index 2fc1a01..2b41c2c 100644 --- a/sdk/full/src/test/kotlin/io/devconsole/FullInspectorDataSourceTest.kt +++ b/sdk/full/src/test/kotlin/io/devconsole/FullInspectorDataSourceTest.kt @@ -90,12 +90,13 @@ class FullInspectorDataSourceTest { id: String, startedAtEpochMs: Long, completedAtEpochMs: Long?, + url: String = "https://api.example.test/orders", ) { val capture = captureFactory.capture( NetworkRequestInput( method = "GET", - url = "https://api.example.test/orders", + url = url, headers = mapOf("X-Request-Id" to id), ), NetworkResponseInput( @@ -728,6 +729,29 @@ class FullInspectorDataSourceTest { assertTrue(evidence.items("session-1").isEmpty()) } + /** + * The traffic list row shows path + query, and it reads that query back off `url` -- so the + * mapping has to carry the query across there, while `path` stays the bare endpoint that + * mock-rule prefill escapes into a regex. + */ + @Test + fun `snapshot carries a transaction's query string on the url, not folded into the path`() { + val store = networkStore() + recordTransaction( + store, + "tx-query", + startedAtEpochMs = 0, + completedAtEpochMs = 5, + url = "https://api.example.test/orders?status=open&page=2", + ) + val source = FullInspectorDataSource(store, MockEngine(emptyList()), configSupplier = { null }) + + val transaction = source.snapshot().transactions.single() + + assertEquals("/orders", transaction.path) + assertEquals("https://api.example.test/orders?status=open&page=2", transaction.url) + } + @Test fun `snapshot maps transactions, mock rules, and capabilities from the underlying engines`() { val store = networkStore() diff --git a/sdk/full/src/test/kotlin/io/devconsole/RemoteConfigInspectorTest.kt b/sdk/full/src/test/kotlin/io/devconsole/RemoteConfigInspectorTest.kt index b8d8d6f..21c9179 100644 --- a/sdk/full/src/test/kotlin/io/devconsole/RemoteConfigInspectorTest.kt +++ b/sdk/full/src/test/kotlin/io/devconsole/RemoteConfigInspectorTest.kt @@ -207,11 +207,18 @@ class RemoteConfigInspectorTest { } @Test - fun `a bearer token inside an ordinary value is still scrubbed`() { - val entries = redactingBoundary().apply(listOf(entry("welcome_banner", "Bearer abc123def"))) + fun `a secret inside an ordinary value is still scrubbed when policy specifies pattern`() { + val policy = + RedactionPolicy( + sensitiveFieldNames = emptySet(), + textPatterns = listOf(Regex("secret-[0-9]+")), + ) + val entries = + RedactingRemoteConfig(RedactionEngine(policy), policy) + .apply(listOf(entry("welcome_banner", "secret-12345"))) assertTrue(entries.single().redacted) - assertFalse(entries.single().value.contains("abc123def")) + assertFalse(entries.single().value.contains("secret-12345")) } @Test diff --git a/sdk/logs/src/test/kotlin/io/devconsole/logs/LogRecorderTest.kt b/sdk/logs/src/test/kotlin/io/devconsole/logs/LogRecorderTest.kt index 0788ccc..1c9bc8d 100644 --- a/sdk/logs/src/test/kotlin/io/devconsole/logs/LogRecorderTest.kt +++ b/sdk/logs/src/test/kotlin/io/devconsole/logs/LogRecorderTest.kt @@ -15,13 +15,13 @@ class LogRecorderTest { @Test fun `records a redacted entry`() { LogRecorder(redaction, sink, nowEpochMs = { 42L }) - .record(LogLevel.INFO, "Checkout", "calling with Authorization: Bearer abc123def") + .record(LogLevel.INFO, "Checkout", "password=abc123def") val entry = emitted.single() assertEquals(LogLevel.INFO, entry.level) assertEquals("Checkout", entry.tag) assertEquals(42L, entry.timestampEpochMs) - assertTrue("expected the bearer token to be redacted, got: ${entry.message}", "abc123def" !in entry.message) + assertTrue("expected the password to be redacted, got: ${entry.message}", "abc123def" !in entry.message) assertNull(entry.stackTrace) } diff --git a/sdk/network/api/network.api b/sdk/network/api/network.api index 05bcfb1..0f08c88 100644 --- a/sdk/network/api/network.api +++ b/sdk/network/api/network.api @@ -571,6 +571,7 @@ public final class io/devconsole/network/NetworkUrl { public final fun getHost ()Ljava/lang/String; public final fun getPath ()Ljava/lang/String; public final fun getQuery ()Ljava/util/Map; + public final fun getQueryString ()Ljava/lang/String; public final fun getScheme ()Ljava/lang/String; public fun hashCode ()I public fun toString ()Ljava/lang/String; diff --git a/sdk/network/src/main/kotlin/io/devconsole/network/NetworkCapture.kt b/sdk/network/src/main/kotlin/io/devconsole/network/NetworkCapture.kt index 8134692..c72974a 100644 --- a/sdk/network/src/main/kotlin/io/devconsole/network/NetworkCapture.kt +++ b/sdk/network/src/main/kotlin/io/devconsole/network/NetworkCapture.kt @@ -80,10 +80,17 @@ data class NetworkUrl( val path: String, val query: Map, ) { + /** + * The already-redacted query in `a=1&b=2` form, empty when the request carried no parameters. + * Split out of [display] so a list row can show the query beside the path without having to + * re-parse a full URL string. + */ + val queryString: String get() = query.entries.joinToString("&") { "${it.key}=${it.value}" } + val display: String get() = buildString { append("$scheme://$host$path") - if (query.isNotEmpty()) append('?').append(query.entries.joinToString("&") { "${it.key}=${it.value}" }) + if (query.isNotEmpty()) append('?').append(queryString) } } diff --git a/sdk/network/src/test/kotlin/io/devconsole/network/NetworkCaptureFactoryTest.kt b/sdk/network/src/test/kotlin/io/devconsole/network/NetworkCaptureFactoryTest.kt index c10cc4c..1412e52 100644 --- a/sdk/network/src/test/kotlin/io/devconsole/network/NetworkCaptureFactoryTest.kt +++ b/sdk/network/src/test/kotlin/io/devconsole/network/NetworkCaptureFactoryTest.kt @@ -11,21 +11,22 @@ class NetworkCaptureFactoryTest { private val factory = NetworkCaptureFactory(RedactionEngine(RedactionPolicy.default())) @Test - fun `redacts headers query and textual body before producing capture`() { + fun `redacts headers query and textual body before producing capture while preserving authorization`() { val capture = factory.capture( request = NetworkRequestInput( method = "POST", url = "https://example.test/orders?access_token=raw-secret", - headers = mapOf("Authorization" to "Bearer header-secret"), - body = "Bearer body-secret".encodeToByteArray(), + headers = mapOf("Authorization" to "Bearer header-token", "Cookie" to "session=header-secret"), + body = "{\"password\":\"body-secret\"}".encodeToByteArray(), contentType = "application/json", ), response = null, ) - assertEquals("", capture.request.headers.getValue("Authorization")) + assertEquals("Bearer header-token", capture.request.headers.getValue("Authorization")) + assertEquals("", capture.request.headers.getValue("Cookie")) assertFalse( capture.request.url.display .contains("raw-secret"), diff --git a/sdk/network/src/test/kotlin/io/devconsole/network/NetworkExportTest.kt b/sdk/network/src/test/kotlin/io/devconsole/network/NetworkExportTest.kt index dcf697f..fef37a5 100644 --- a/sdk/network/src/test/kotlin/io/devconsole/network/NetworkExportTest.kt +++ b/sdk/network/src/test/kotlin/io/devconsole/network/NetworkExportTest.kt @@ -15,8 +15,8 @@ class NetworkExportTest { NetworkRequestInput( method = "POST", url = "https://example.test/orders?access_token=query-secret", - headers = mapOf("Authorization" to "Bearer header-secret"), - body = "Bearer body-secret".encodeToByteArray(), + headers = mapOf("Cookie" to "session=header-secret"), + body = "{\"password\":\"body-secret\"}".encodeToByteArray(), contentType = "application/json", ), NetworkResponseInput(statusCode = 201), @@ -286,7 +286,7 @@ class NetworkExportTest { NetworkRequestInput( method = "POST", url = "https://example.test/orders", - headers = mapOf("Authorization" to "Bearer header-secret"), + headers = mapOf("Cookie" to "session=header-secret"), body = "{\"token\":\"body-secret\"}".encodeToByteArray(), contentType = "application/json", ), diff --git a/sdk/push/src/test/kotlin/io/devconsole/push/PushEventStoreTest.kt b/sdk/push/src/test/kotlin/io/devconsole/push/PushEventStoreTest.kt index 35094a1..430a258 100644 --- a/sdk/push/src/test/kotlin/io/devconsole/push/PushEventStoreTest.kt +++ b/sdk/push/src/test/kotlin/io/devconsole/push/PushEventStoreTest.kt @@ -22,7 +22,7 @@ class PushEventStoreTest { sentAtEpochMs = 10, receivedAtEpochMs = 20, notification = PushNotification(title = "New order", body = "Order #42"), - rawMetadata = mapOf("authorization" to "Bearer raw-secret"), + rawMetadata = mapOf("password" to "raw-secret"), lifecycle = PushLifecycle.OPENED, simulated = true, ), @@ -31,7 +31,7 @@ class PushEventStoreTest { assertEquals(PushLifecycle.OPENED, event.lifecycle) assertTrue(event.simulated) assertEquals("", event.data.getValue("access_token")) - assertEquals("", event.rawMetadata.getValue("authorization")) + assertEquals("", event.rawMetadata.getValue("password")) assertEquals(listOf(event), store.events()) } } diff --git a/sdk/security/src/main/kotlin/io/devconsole/security/RedactionEngine.kt b/sdk/security/src/main/kotlin/io/devconsole/security/RedactionEngine.kt index 4025a93..c5776cb 100644 --- a/sdk/security/src/main/kotlin/io/devconsole/security/RedactionEngine.kt +++ b/sdk/security/src/main/kotlin/io/devconsole/security/RedactionEngine.kt @@ -33,10 +33,6 @@ data class RedactionPolicy( RedactionPolicy( sensitiveFieldNames = setOf( - "authorization", - "proxy-authorization", - "www-authenticate", - "authentication", "cookie", "set-cookie", "x-api-key", @@ -59,7 +55,7 @@ data class RedactionPolicy( "private_key", "session_id", ), - textPatterns = listOf(Regex("Bearer\\s+[A-Za-z0-9._~-]+", RegexOption.IGNORE_CASE)), + textPatterns = emptyList(), ) } } diff --git a/sdk/security/src/test/kotlin/io/devconsole/security/RedactionEngineTest.kt b/sdk/security/src/test/kotlin/io/devconsole/security/RedactionEngineTest.kt index 2b9532b..2a444ce 100644 --- a/sdk/security/src/test/kotlin/io/devconsole/security/RedactionEngineTest.kt +++ b/sdk/security/src/test/kotlin/io/devconsole/security/RedactionEngineTest.kt @@ -10,11 +10,17 @@ class RedactionEngineTest { @Test fun `redacts default sensitive headers case insensitively`() { - val redacted = engine.redactFields(mapOf("Authorization" to "Bearer secret", "Accept" to "application/json")) - assertEquals("", redacted["Authorization"]) + val redacted = engine.redactFields(mapOf("Cookie" to "session=secret", "Accept" to "application/json")) + assertEquals("", redacted["Cookie"]) assertEquals("application/json", redacted["Accept"]) } + @Test + fun `preserves authorization header unredacted by default`() { + val fields = engine.redactFields(mapOf("Authorization" to "Bearer secret-token-123")) + assertEquals("Bearer secret-token-123", fields["Authorization"]) + } + @Test fun `redacts access token query values before storage`() { assertEquals("", engine.redactFields(mapOf("access_token" to "abc"))["access_token"]) @@ -22,13 +28,27 @@ class RedactionEngineTest { @Test fun `bounds text before applying regex rules`() { - val result = engine.redactText("Bearer abc.def", maxLength = 8) + val regexEngine = + RedactionEngine( + RedactionPolicy( + sensitiveFieldNames = emptySet(), + textPatterns = listOf(Regex("Bearer\\s+[A-Za-z0-9._~-]+", RegexOption.IGNORE_CASE)), + ), + ) + val result = regexEngine.redactText("Bearer abc.def", maxLength = 8) assertFalse(result.contains("abc.def")) } @Test fun `applies redaction before enforcing preview limit`() { - val result = engine.redactText("Bearer very-secret-token", maxLength = 10) + val regexEngine = + RedactionEngine( + RedactionPolicy( + sensitiveFieldNames = emptySet(), + textPatterns = listOf(Regex("Bearer\\s+[A-Za-z0-9._~-]+", RegexOption.IGNORE_CASE)), + ), + ) + val result = regexEngine.redactText("Bearer very-secret-token", maxLength = 10) assertFalse(result.contains("very-secret-token")) assertEquals("", result) @@ -49,7 +69,7 @@ class RedactionEngineTest { mapOf( "X-Auth-Token" to "abc123", "x-access-token" to "def456", - "Authentication" to "ghi789", + "x-api-key" to "ghi789", "jwt" to "eyJhbGciOi", "token" to "opaque", ), diff --git a/sdk/server-api/src/main/kotlin/io/devconsole/server/api/LocalServerEngine.kt b/sdk/server-api/src/main/kotlin/io/devconsole/server/api/LocalServerEngine.kt index 701eee3..4fef1ab 100644 --- a/sdk/server-api/src/main/kotlin/io/devconsole/server/api/LocalServerEngine.kt +++ b/sdk/server-api/src/main/kotlin/io/devconsole/server/api/LocalServerEngine.kt @@ -202,6 +202,7 @@ sealed interface SessionCodeExchangeResult { * host's `EditingCapabilities` flags instead of a session-level role. [SessionCodeAuthority] is the * sole way a session gets minted, via [createSession]. */ +@Suppress("TooManyFunctions") class SessionAuthority( private val nowEpochMs: () -> Long = System::currentTimeMillis, private val sessionTtlMs: Long = DEFAULT_SESSION_TTL_MS, diff --git a/sdk/server-ktor/src/main/kotlin/io/devconsole/server/ktor/DevConsoleKtorModule.kt b/sdk/server-ktor/src/main/kotlin/io/devconsole/server/ktor/DevConsoleKtorModule.kt index 435279a..dce61ec 100644 --- a/sdk/server-ktor/src/main/kotlin/io/devconsole/server/ktor/DevConsoleKtorModule.kt +++ b/sdk/server-ktor/src/main/kotlin/io/devconsole/server/ktor/DevConsoleKtorModule.kt @@ -5347,7 +5347,9 @@ private fun NetworkTransaction.summaryJson(): String { "\"completedAtEpochMs\":${completedAtEpochMs ?: "null"},\"durationMs\":${durationMs ?: "null"}," + "\"method\":\"${capture.request.method.escapeJson()}\"," + "\"host\":\"${capture.request.url.host.escapeJson()}\"," + - "\"path\":\"${capture.request.url.path.escapeJson()}\",\"status\":${capture.response?.statusCode ?: "null"}," + + "\"path\":\"${capture.request.url.path.escapeJson()}\"," + + "\"query\":\"${capture.request.url.queryString.escapeJson()}\"," + + "\"status\":${capture.response?.statusCode ?: "null"}," + "\"contentType\":\"$contentType\"," + "\"error\":$error,\"tags\":$tags,\"correlationId\":$correlationId}" } diff --git a/sdk/server-ktor/src/main/resources/devconsole-web/dashboard.css b/sdk/server-ktor/src/main/resources/devconsole-web/dashboard.css index 69922f0..1bcd2f2 100644 --- a/sdk/server-ktor/src/main/resources/devconsole-web/dashboard.css +++ b/sdk/server-ktor/src/main/resources/devconsole-web/dashboard.css @@ -741,6 +741,8 @@ body.detail-zoom .split-shell.collapse-detail { grid-template-columns: 1fr; } .col-status { flex: 0 0 62px; text-align: right; } .col-flag { flex: 0 0 24px; text-align: right; } @media (max-width: 620px) { .col-duration { display: none; } } +/* The stacked pair keeps both values at every width — it costs one column, not two, and Network's + list head is sr-only anyway, so there is no header to drift out of step with it. */ /* Export-selection checkbox (Network list only): header "select all visible" + one per row. Same 24x24/6px-radius footprint as .row-flag so the row height and header never shift when a @@ -765,9 +767,14 @@ body.detail-zoom .split-shell.collapse-detail { grid-template-columns: 1fr; } .detail-pane-v2 { min-width: 0; display: flex; flex-direction: column; overflow: hidden; background: var(--ground); } /* ---- List rows ---- */ +/* `min-height`, not `height`: a row with a path too long for one line grows to fit it (up to the + four-line clamp on .row-main-text below). The virtualizer measures each painted row rather than + multiplying by --d-row-h, so a taller row costs nothing but its own pixels — see + createVirtualList's `heights` cache. Vertical padding is what keeps a grown row off its own + borders; a one-line row still lands on exactly --d-row-h. */ .row { - position: relative; display: flex; align-items: center; gap: 8px; width: 100%; height: var(--d-row-h); - padding: 0 8px 0 12px; text-align: left; cursor: pointer; border-radius: 0; border-inline: 0; border-top: 0; border-bottom: 1px solid var(--line); + position: relative; display: flex; align-items: center; gap: 8px; width: 100%; min-height: var(--d-row-h); + padding: 4px 8px 4px 12px; text-align: left; cursor: pointer; border-radius: 0; border-inline: 0; border-top: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--ink); font: inherit; font-size: var(--d-row-font); } .row:hover { background: var(--surface-2); } @@ -781,10 +788,38 @@ body.detail-zoom .split-shell.collapse-detail { grid-template-columns: 1fr; } font-family: var(--font-mono); font-size: var(--d-row-main-font); color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } +/* Opt-in (rowHtml's `wrapMain`, Network only): a long path wraps instead of being cut at the first + line, then ellipsises once past four — enough for the query-string-heavy endpoints that were + losing everything after the host, without letting one pathological URL own the whole viewport. + `word-break: break-all`, not `overflow-wrap`: a query string is one unbreakable token, and + overflow-wrap only breaks a token that cannot fit on a line of its own, so it pushed the whole + query to line two and left the rest of line one blank. break-all fills every line to the edge, + which is what a URL wants — and what prose does not, which is why this is opt-in rather than the + rule for every list. The line clamp supplies its own ellipsis, so the single-line + `text-overflow` above is turned back off here. */ +.row-main-text.row-main-wrap { + display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 4; line-clamp: 4; + white-space: normal; text-overflow: clip; word-break: break-all; +} +/* Query string trailing a Network row's path: present and readable, but never louder than the endpoint. */ +.row-main-sub { color: var(--text-3); } .row-tag { flex: 0 0 auto; font-family: var(--font-mono); font-size: 9.5px; font-weight: 700; letter-spacing: 0.06em; padding: 1px 4px; border-radius: 99px; border: 1px solid var(--border-strong); } +/* Network stacks its status over its duration in one column (rowHtml's `stackMetrics`), the way + the Android row reads them; the widths below still apply to the side-by-side lists. Right- + aligned and baseline-free so the pair reads as one block however tall the path beside it is. */ +/* `line-height: 1` on the pair, not the default ~1.2: stacked, the two values would otherwise + total more than --d-row-h and push every Network row ~12px taller than the other lists' — the + opposite of the space this layout is meant to hand back. At 1 they fit the plain row height, and + only a wrapped path grows the row. */ +/* `flex: 0 0 auto`, not the 74px the side-by-side lists reserve: stacked, the pair is at most as + wide as "1931 ms", and a fixed track just parked that slack to the left of a short "200" — read + as a gap between the path and the status roughly four times the row's own 8px one. Auto-sizing + hands those pixels back to the path and leaves exactly that 8px. */ +.row-metrics { flex: 0 0 auto; display: flex; flex-direction: column; align-items: flex-end; justify-content: center; gap: 2px; line-height: 1; } +.row-metrics .row-duration, .row-metrics .row-status { flex: 0 0 auto; } /* Sized for the longest consumers: Timeline puts an event type here, not Network's "198 ms". */ .row-duration { flex: 0 0 74px; font-family: var(--font-mono); font-size: 11.5px; font-variant-numeric: tabular-nums; text-align: right; color: var(--text-3); } .row-status { flex: 0 0 62px; font-family: var(--font-mono); font-size: 12.5px; font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; } @@ -851,6 +886,30 @@ body.detail-zoom .split-shell.collapse-detail { grid-template-columns: 1fr; } .detail-head-status { font-family: var(--font-mono); font-size: 13px; font-weight: 700; } .detail-expand { display: grid; place-items: center; width: 26px; height: 26px; padding: 0; flex: 0 0 auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2); color: var(--muted); } .detail-expand.active { border-color: var(--signal); background: var(--signal-soft); color: var(--signal); } + +/* Kebab (⋮) actions menu, sitting immediately right of .detail-expand — same 26px footprint, so + the head row's right edge reads as one pair of controls rather than two sizes. */ +.detail-menu-btn { display: grid; place-items: center; width: 26px; height: 26px; padding: 0; flex: 0 0 auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2); color: var(--muted); } +.detail-menu-btn:hover { color: var(--ink); border-color: var(--border-strong); } +.detail-menu-btn.active { border-color: var(--signal); background: var(--signal-soft); color: var(--signal); } +/* position: fixed, not absolute: .detail-pane-v2 is overflow: hidden, so an absolutely positioned + popup would be clipped at the pane's edge. Placed by positionDetailMenuPanel(), same contract + as the toolbar dropdowns' .drop-panel. */ +.detail-menu-panel { + position: fixed; z-index: 200; min-width: 200px; max-width: min(92vw, 320px); + display: grid; gap: 1px; padding: 4px; + background: var(--panel); border: 1px solid var(--border-strong); border-radius: 8px; box-shadow: var(--elev-3); +} +.detail-menu-item { + display: flex; align-items: center; gap: 8px; width: 100%; height: 30px; padding: 0 8px; + border: none; border-radius: 6px; background: transparent; text-align: left; color: var(--muted); font-size: 12.5px; +} +.detail-menu-item .ic { flex: 0 0 auto; color: var(--text-3); } +.detail-menu-item:hover:not(:disabled) { background: var(--surface-2); color: var(--ink); } +.detail-menu-item:hover:not(:disabled) .ic { color: inherit; } +.detail-menu-item.on { color: var(--signal); background: var(--signal-soft); } +.detail-menu-item.on .ic { color: var(--signal); } +.detail-menu-item:disabled { opacity: 0.5; cursor: not-allowed; } .detail-facts { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 8px; } .detail-fact { display: flex; align-items: baseline; gap: 8px; font-size: 11.5px; } .detail-fact-k { font-size: 10.5px; font-weight: 600; color: var(--text-3); } @@ -920,6 +979,10 @@ body.detail-zoom .split-shell.collapse-detail { grid-template-columns: 1fr; } /* Find-highlight must win over both grid sizes' background overrides above — same (0,3,0) specificity as `.kv-grid.kv-grid-lg .kv-v` but declared after it, so source order decides. */ .kv-grid .kv-k.hit, .kv-grid .kv-v.hit { background: var(--signal-soft); } +/* Clickable kv value (Network summary's "mock rule: " → Mocks). A ` + : esc(r.v); + return `${esc(r.k)}${v}`; + }) .join('')}`; } function barsHtml(bars) { @@ -1361,31 +1484,110 @@ // overlay clones the already-escaped .code-block markup rather than re-deriving it from data. // ================================================================ let codeFullscreenOpenerEl = null; - // Only ever one focusable in this modal (the close button — the cloned .code-block markup - // carries no interactive elements), so the trap just keeps Tab/Shift+Tab pinned there instead - // of leaking out to the page underneath. + // The body a fullscreen view was opened for, so its Pretty/Raw toggle can rebuild either mode + // without reaching back into the pane underneath (which a re-render may have replaced). + let codeFullscreenBody = null; + // The raw block's markup, captured when the overlay opens. Raw mode clones what the pane is + // already showing rather than re-deriving it, so the two can never disagree. + let codeFullscreenRawHtml = ''; + // The modal used to hold exactly one focusable (close), because it only ever showed cloned + // .code-block markup. A body opens with a Pretty/Raw toggle too, so the trap now cycles the + // real focusables instead of pinning Tab to one button. function codeFullscreenKeydown(e) { if (e.key !== 'Tab') return; + const focusables = [...$('codeFullscreenModal').querySelectorAll('button:not([hidden])')].filter( + (el) => el.offsetParent !== null, + ); + if (!focusables.length) return; e.preventDefault(); - $('codeFullscreenClose').focus(); + const at = focusables.indexOf(document.activeElement); + const next = e.shiftKey ? at - 1 : at + 1; + focusables[(next + focusables.length) % focusables.length].focus(); } function openCodeFullscreen(sourceBlock, title) { const overlay = $('codeFullscreenModal'); if (!overlay || !sourceBlock) return; $('codeFullscreenTitle').textContent = title || 'JSON body'; + codeFullscreenBody = null; + $('codeFullscreenSeg').hidden = true; // Clones the already-escaped markup rather than re-deriving it from data, so this cannot // disagree with what the page underneath is showing. $('codeFullscreenBody').innerHTML = `
${sourceBlock.innerHTML}
`; + showCodeFullscreen(overlay); + } + + /** + * Fullscreen for a body viewer, which unlike a plain code block has both renderings available: + * the collapsible tree and the raw text. The expand button used to hand the overlay the *raw* + * block unconditionally, so a body being read as a tree lost its structure — and its collapse + * arrows — at exactly the size where they matter most. + * + * `mode` is whatever the inline viewer was showing, so expanding enlarges what you were already + * reading instead of switching the view under you. + */ + function openBodyFullscreen(mountKey, title, mode, rawBlock) { + const overlay = $('codeFullscreenModal'); + const entry = pendingBodyViewers.get(mountKey); + if (!overlay || !entry) return; + $('codeFullscreenTitle').textContent = title || 'JSON body'; + codeFullscreenBody = entry; + codeFullscreenRawHtml = rawBlock?.innerHTML || ''; + // Raw is always available; Pretty only for something the tree can actually be built from. + const canPretty = entry.kind === 'json' || entry.kind === 'xml'; + $('codeFullscreenSeg').hidden = !canPretty; + renderCodeFullscreenBody(canPretty ? mode || 'pretty' : 'raw'); + showCodeFullscreen(overlay); + } + + function showCodeFullscreen(overlay) { codeFullscreenOpenerEl = document.activeElement; overlay.hidden = false; document.addEventListener('keydown', codeFullscreenKeydown); $('codeFullscreenClose').focus(); } + + /** Paints one mode into the overlay from `codeFullscreenBody`'s original raw text. */ + function renderCodeFullscreenBody(mode) { + const entry = codeFullscreenBody; + const host = $('codeFullscreenBody'); + if (!entry || !host) return; + $('codeFullscreenSeg') + .querySelectorAll('[data-fs-mode]') + .forEach((b) => b.classList.toggle('active', b.dataset.fsMode === mode)); + host.innerHTML = ''; + if (mode === 'raw') { + host.innerHTML = `
${codeFullscreenRawHtml}
`; + return; + } + // Its own cache key ('fs:'): a cached tree is a live DOM node, and reusing the inline one + // would MOVE it out of the pane underneath, leaving that body blank once the overlay closes. + try { + const built = buildOrReuseBodyViewerTree('fs:' + entry.groupKey, entry.kind, entry.raw, entry.diffSig, () => { + if (entry.kind === 'xml') { + const pre = document.createElement('pre'); + pre.className = 'body-viewer-xml'; + pre.textContent = prettyPrintXml(entry.raw) ?? entry.raw; + return pre; + } + const w = document.createElement('div'); + w.className = 'json-tree'; + w.appendChild(jsonNode(JSON.parse(entry.raw), 0, entry.diffInfo)); + return w; + }); + host.appendChild(built); + } catch { + // Same fallback the inline mount makes: a body that no longer parses shows as its own text + // rather than an empty pane. + host.textContent = entry.raw; + } + } function closeCodeFullscreen() { const overlay = $('codeFullscreenModal'); if (!overlay || overlay.hidden) return; overlay.hidden = true; $('codeFullscreenBody').innerHTML = ''; + codeFullscreenBody = null; + codeFullscreenRawHtml = ''; document.removeEventListener('keydown', codeFullscreenKeydown); codeFullscreenOpenerEl?.focus?.(); codeFullscreenOpenerEl = null; @@ -1875,9 +2077,16 @@ for (let index = from; index < upto; index++) { const [key, val] = entries[index]; const line = document.createElement('div'); - const keySpan = document.createElement('span'); - keySpan.className = 'json-key'; - keySpan.textContent = JSON.stringify(key) + ': '; + // An array child's `key` is its index (jsonNode hands entries over as `[String(i), v]`), + // carried only so diff paths stay addressable — it is a position, not a field name, so it + // is never printed. Rendering it the object way put `"0": ` in front of every element, + // which reads as an object whose key happens to be the string "0". The Compose tree makes + // the same distinction by passing `keyLabel = null` for array items (InspectorBodyFormat). + const keySpan = isArray ? null : document.createElement('span'); + if (keySpan) { + keySpan.className = 'json-key'; + keySpan.textContent = JSON.stringify(key) + ': '; + } // diffInfo is only ever set for the one mocked-response body viewer that requests it // (see diffMockBody) — every other jsonNode/jsonContainer call in the app passes nothing, // so childPath stays undefined and this is just an extra falsy check per row. @@ -1890,7 +2099,8 @@ // subtree, so it keeps the row-level highlight on `line` itself. const isContainerVal = val !== null && typeof val === 'object'; if (diffInfo && !isContainerVal && diffInfo.hits.has(pathKey(childPath))) line.classList.add('json-diff-hit'); - line.append(keySpan, jsonNode(val, depth + 1, diffInfo, childPath)); + if (keySpan) line.append(keySpan); + line.append(jsonNode(val, depth + 1, diffInfo, childPath)); if (index < entries.length - 1) line.append(document.createTextNode(',')); children.append(line); } @@ -3461,11 +3671,15 @@ checkbox: true, checked: networkSelectedIds.has(t.id), badgeText: t.method, badgeTone: methodTone(t.method), mainText: t.path, + // The query is what separates two rows on the same endpoint, so it belongs in the row -- + // dimmed and after the path, which stays the part that survives the ellipsis. + mainSub: t.query ? '?' + t.query : '', // Single row-tag slot: a pinned diff baseline always wins over the mocked marker. tagText: networkPinnedId === t.id ? 'BASE' : t.tags?.mocked === 'true' ? 'MOCK' : false, tagTone: 'put', duration: t.durationMs != null ? t.durationMs + ' ms' : t.error ? 'failed' : '—', statusText: t.status != null ? String(t.status) : t.error ? 'ERR' : '—', sTone: statusTone(t.status), + stackMetrics: true, wrapMain: true, flagKind: 'network', flagLabel: t.method + ' ' + t.path, posinset: i + 1, setsize: total, }); @@ -3686,6 +3900,10 @@ const inside = (id) => path.some((el) => el.id === id); if (networkHostDropOpen && !inside('networkHostDrop')) { networkHostDropOpen = false; renderNetworkHostDrop(); } if (socketConnDropOpen && !inside('socketConnDrop')) { socketConnDropOpen = false; renderSocketConnDrop(); } + // The detail pane's kebab menu closes on the same rule. Its own button and items are handled + // in wireNetworkDetailPane (which runs first, on the pane), so by the time this fires they + // have already cleared the flag — this only catches genuine outside clicks. + if (networkActionsMenuOpen && !inside('detailActionsMenuPanel') && !inside('detailActionsMenuBtn')) { networkActionsMenuOpen = false; renderNetworkDetail(); } } function renderNetworkHostDrop() { @@ -4456,7 +4674,7 @@ } function copyStateJson() { if (selectedStateValue === undefined || selectedStateValue === null) return; - navigator.clipboard?.writeText(JSON.stringify(selectedStateValue, null, 2)).then(() => toast('State copied.')); + copyToClipboard(JSON.stringify(selectedStateValue, null, 2), 'State'); } /** The command body is raw text, not JSON — inputSchema only tells us how to *shape* the * control (a boolean picker, an enum select, a number field, or a free-text/JSON textarea for @@ -5055,7 +5273,7 @@ const lines = [dbLastRows.columns.map(esc2).join(',')].concat( dbLastRows.rows.map((row) => dbLastRows.columns.map((c, i) => esc2(Array.isArray(row) ? row[i] : row[c])).join(',')), ); - navigator.clipboard?.writeText(lines.join('\n')).then(() => toast((dbSelectedTable || 'Table') + ' copied as CSV')); + copyToClipboard(lines.join('\n'), (dbSelectedTable || 'Table') + ' as CSV'); } // ================================================================ @@ -6578,8 +6796,15 @@ * inside `.detail-head` instead, where `flex`/`min-height`/`overflow` mean nothing — so a body * taller than the pane grew the head past it and `.detail-pane-v2`'s `overflow: hidden` simply * cut it off, with nothing anywhere able to scroll to the rest. */ - function detailHeadHtml({ badgeText, badgeTone, title, statusText, sTone, mocked, extraBadge, facts, actions, tabs, layoutToggle }) { + /** `actionsMenu` opts a pane out of the flat `.detail-actions-row` and into a kebab (⋮) menu + * beside the expand button — same `actions` model, same `data-detail-action` ids, so a pane's + * existing delegated click handler covers the items unchanged. Opt-in per pane (Network only + * today) rather than a blanket switch: the other detail panes carry two or three actions each, + * where a row costs nothing and a menu would only add a click. `menuOpen` is the caller's own + * state, since these panes re-render wholesale and the flag has to outlive the innerHTML swap. */ + function detailHeadHtml({ badgeText, badgeTone, title, statusText, sTone, mocked, extraBadge, facts, actions, tabs, layoutToggle, actionsMenu, menuOpen }) { const zoom = document.body.classList.contains('detail-zoom'); + const hasActions = actions && actions.length; return `
${esc(badgeText)} @@ -6588,6 +6813,25 @@ ${esc(title)} ${esc(statusText || '')} + ${ + actionsMenu && hasActions + ? ` + ${ + menuOpen + ? `` + : '' + }` + : '' + }
${ facts && facts.length @@ -6600,7 +6844,7 @@ .join('')}
` : '' } - ${actions && actions.length ? `
${actions.map((a) => ``).join('')}
` : ''} + ${hasActions && !actionsMenu ? `
${actions.map((a) => ``).join('')}
` : ''} ${ tabs && tabs.length ? `
${tabs.map((t) => ``).join('')}${ @@ -6778,8 +7022,9 @@ const reqPane = { side: 'Request', dotTone: 'put', meta: detail.method + (detail.request?.contentType ? ' · ' + detail.request.contentType : ''), metaTone: 'muted', actions: [{ id: 'copy-request', label: 'Copy', icon: 'copy', title: 'Copy the whole request (line, headers, payload)' }], + // No 'General' group: url/method/sent-at are the Summary tab's job now (see + // networkSummaryKvs), and this tab is for the headers and the body. groups: [ - { label: 'General', copyLabel: 'request line', kvs: markKvHits([{ k: 'url', v: detail.request?.url || '' }, { k: 'method', v: detail.method }, { k: 'sent at', v: time(detail.startedAtEpochMs) }], query) }, { label: 'Request headers', copyLabel: 'request headers', meta: String(headerRows(detail.request?.headers).length), kvs: markKvHits(headerRows(detail.request?.headers), query) }, reqBody ? { label: 'Payload', copyLabel: 'request payload', meta: detail.request?.contentType || '', code: reqBody, body: { raw: bodyRawText(detail.request?.body), contentType: detail.request?.contentType } } @@ -6794,16 +7039,16 @@ actions: hasResponse ? [{ id: 'copy-response', label: 'Copy', icon: 'copy', title: 'Copy the whole response (status, headers, body)' }] : [{ id: 'copy-error', label: 'Copy error', icon: 'copy' }], + // Same as the request pane: no 'General' group — status/duration/content type (and, on the + // failure branch, the error string) live in the Summary tab. groups: hasResponse ? [ - { label: 'General', copyLabel: 'status line', kvs: markKvHits([{ k: 'status', v: String(detail.status) }, { k: 'duration', v: (detail.durationMs ?? '—') + ' ms' }, { k: 'content type', v: detail.response.contentType || '—' }], query) }, { label: 'Response headers', copyLabel: 'response headers', meta: String(headerRows(detail.response.headers).length), kvs: markKvHits(headerRows(detail.response.headers), query) }, resBody ? { label: 'Body', copyLabel: 'response body', meta: detail.response.contentType || '', code: resBody, body: { raw: bodyRawText(detail.response.body), contentType: detail.response.contentType, diffInfo, diffSig } } : { label: 'Body', empty: 'No body.' }, ] : [ - { label: 'General', copyLabel: 'failure detail', kvs: markKvHits([{ k: 'status', v: '— no response' }, { k: 'error', v: detail.error || 'unknown' }], query) }, { label: 'Response headers', empty: 'Connection failed before headers arrived.' }, { label: 'Body', empty: 'No body.' }, ], @@ -6858,14 +7103,26 @@ }; } - function networkSummaryKvs(detail) { + /** The detail head used to repeat time/duration/content type/correlation/mock rule as a facts + * strip under the title; they live here now (one place, one format) so the head stays title + + * actions + tabs. `mockRuleId`/`mockDiffTotal` are computed in renderNetworkDetail (they need + * mockRulesCache) and passed down rather than recomputed. */ + function networkSummaryKvs(detail, { mockRuleId, mockDiffTotal } = {}) { const rows = [ + { k: 'time', v: time(detail.startedAtEpochMs) }, + { k: 'duration', v: detail.durationMs != null ? detail.durationMs + ' ms' : '—' }, { k: 'request.url', v: detail.request?.url || '' }, { k: 'request.method', v: detail.method }, - { k: 'response.status', v: detail.status != null ? String(detail.status) : detail.error ? '— (' + detail.error + ')' : '—', tone: statusTone(detail.status) }, + { k: 'response.status', v: detail.status != null ? String(detail.status) : detail.error ? '— no response' : '—', tone: statusTone(detail.status) }, ]; + // The error string used to be parenthesised into response.status above, and to double as the + // compare tab's failure "General" group. Both are gone: this is its one home, on its own row + // so a long exception message isn't buried inside another value. + if (detail.error) rows.push({ k: 'response.error', v: detail.error, tone: 'error' }); if (detail.response?.contentType) rows.push({ k: 'response.contentType', v: detail.response.contentType }); if (detail.correlationId) rows.push({ k: 'correlationId', v: detail.correlationId }); + if (mockRuleId) rows.push({ k: 'mock rule', v: mockRuleId, mockRuleLink: true }); + if (mockDiffTotal > 0) rows.push({ k: 'vs original', v: mockDiffTotal + (mockDiffTotal === 1 ? ' field differs' : ' fields differ') + ' from original', tone: 'warn' }); const tags = Object.entries(detail.tags || {}); if (tags.length) rows.push({ k: 'tags', v: tags.map(([k, v]) => k + '=' + v).join(', ') }); return rows; @@ -6917,10 +7174,12 @@ if (networkTab === 'diff' && !pinId) networkTab = 'compare'; const diffCount = hasPin ? diffHeaderStats(networkDetailCache.get(pinId), detail) : null; const relatedFresh = networkRelatedEventsForId === detail.id; - const tabs = [{ id: 'compare', label: 'Request & response' }] + // Summary leads: it absorbed the head's old facts strip (time/duration/content type/ + // correlation/mock rule/error), so it is the "what is this capture" tab and reads first. + // `networkTab` still defaults to 'compare' — order here, not which tab opens selected. + const tabs = [{ id: 'summary', label: 'Summary' }, { id: 'compare', label: 'Request & response' }] .concat(pinId ? [{ id: 'diff', label: 'Diff vs baseline', count: hasPin ? String(diffCount.changed + diffCount.added) : 'pick one' }] : []) .concat([ - { id: 'summary', label: 'Summary' }, { id: 'headers', label: 'Headers', count: String(headerRows(detail.request?.headers).length + headerRows(detail.response?.headers).length) }, { id: 'timing', label: 'Timing' }, { id: 'related', label: 'Related events', count: relatedFresh ? String(networkRelatedEvents.length) : undefined }, @@ -6967,12 +7226,10 @@ badgeText: detail.method, badgeTone: methodTone(detail.method), title: detail.host + detail.path, statusText: detail.status != null ? String(detail.status) : detail.error ? 'FAILED' : '—', sTone: statusTone(detail.status), mocked: isMocked, - facts: [{ k: 'time', v: time(detail.startedAtEpochMs) }, { k: 'duration', v: detail.durationMs != null ? detail.durationMs + ' ms' : '—' }] - .concat(detail.response?.contentType ? [{ k: 'content type', v: detail.response.contentType }] : []) - .concat(detail.correlationId ? [{ k: 'correlation', v: detail.correlationId }] : []) - .concat(mockRuleId ? [{ k: 'mock rule', v: mockRuleId, link: true }] : []) - .concat(mockDiffTotal > 0 ? [{ k: 'vs original', v: mockDiffTotal + (mockDiffTotal === 1 ? ' field differs' : ' fields differ') + ' from original' }] : []), + // No facts strip: time/duration/content type/correlation/mock rule now live in the Summary + // tab (see networkSummaryKvs) instead of being repeated under the title. actions, tabs, layoutToggle, + actionsMenu: true, menuOpen: networkActionsMenuOpen, }); let findBar = ''; let bodyHtml; @@ -6994,7 +7251,7 @@ bodyHtml = `
${panes.map(sbsPaneHtml).join('')}
`; } } else if (networkTab === 'summary') { - bodyHtml = kvGridHtml(networkSummaryKvs(detail), true); + bodyHtml = kvGridHtml(networkSummaryKvs(detail, { mockRuleId, mockDiffTotal }), true); } else if (networkTab === 'headers') { bodyHtml = kvGridHtml(networkHeadersKvs(detail), true); } else if (networkTab === 'timing') { @@ -7023,9 +7280,28 @@ bodyHtml += `
${icon('lock', 'ic-sm')}Headers and bodies are redacted by the on-device allowlist before they reach this browser. Values shown as •••• never left the app.
`; pane.innerHTML = head + findBar + `
${bodyHtml}
`; mountBodyViewers(pane); + // The menu panel is position: fixed (the detail pane is overflow: hidden, so an absolutely + // positioned popup would be clipped at the pane edge) — same trade the toolbar dropdowns make. + if (networkActionsMenuOpen) positionDetailMenuPanel(); restoreFocus(focusSnap, pane); } + /** Right-aligns the actions menu under its kebab button, flipping above when there isn't room + * below — the .drop-panel contract in positionDropPanel, minus the multi-select chrome. */ + function positionDetailMenuPanel() { + requestAnimationFrame(() => { + const btn = $('detailActionsMenuBtn'), panel = $('detailActionsMenuPanel'); + if (!btn || !panel) return; + const rc = btn.getBoundingClientRect(); + const vh = window.innerHeight; + const below = vh - rc.bottom - 16; + const useBelow = below >= 200 || below >= rc.top - 16; + panel.style.left = Math.max(8, rc.right - panel.offsetWidth) + 'px'; + if (useBelow) { panel.style.top = rc.bottom + 5 + 'px'; panel.style.bottom = 'auto'; } + else { panel.style.bottom = vh - rc.top + 5 + 'px'; panel.style.top = 'auto'; } + }); + } + function detailActionText(id, detail) { if (id === 'copy-request') { const lines = ['Method: ' + detail.method, 'URL: ' + (detail.request?.url || ''), ...headerRows(detail.request?.headers).map((h) => h.k + ': ' + h.v)]; @@ -7048,6 +7324,7 @@ function wireNetworkDetailPane() { const pane = $('networkDetailPane'); pane.addEventListener('click', (e) => { + if (e.target.closest('[data-action="toggle-actions-menu"]')) { networkActionsMenuOpen = !networkActionsMenuOpen; renderNetworkDetail(); return; } if (e.target.closest('[data-action="toggle-zoom"]')) { toggleDetailZoom(); return; } const mockLink = e.target.closest('[data-open-mock-rule]'); if (mockLink) { openMockRuleFromNetwork(mockLink.dataset.openMockRule); return; } @@ -7082,6 +7359,11 @@ const actionBtn = e.target.closest('[data-detail-action]'); if (!actionBtn || !selectedTransactionDetail) return; const id = actionBtn.dataset.detailAction; + // Picking an item dismisses the menu, the way a menu is expected to behave — repainted here + // rather than per branch, because most branches (copy, cURL, clone-to-composer) don't + // re-render this pane themselves and would leave the popup hanging open over it. `id` is + // read first since the repaint detaches `actionBtn` from the tree. + if (networkActionsMenuOpen) { networkActionsMenuOpen = false; renderNetworkDetail(); } if (id === 'flag') { const d = selectedTransactionDetail; toggleEvidenceFlag('network', d.id, d.method + ' ' + d.host + d.path); renderNetworkDetail(); } else if (id === 'pin') pinNetworkBaseline(selectedTransactionId); else if (id === 'resend') resendCapturedRequest(); @@ -7186,10 +7468,36 @@ await show('mocks'); } async function copyToClipboard(text, label) { - try { - await navigator.clipboard.writeText(text); - toast(label + ' copied to clipboard.'); - } catch { + let copied = false; + if (navigator.clipboard && window.isSecureContext) { + try { + await navigator.clipboard.writeText(text); + copied = true; + } catch { + copied = false; + } + } + if (!copied) { + try { + const textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'fixed'; + textarea.style.left = '-9999px'; + textarea.style.top = '-9999px'; + textarea.style.opacity = '0'; + textarea.setAttribute('readonly', ''); + document.body.appendChild(textarea); + textarea.focus(); + textarea.select(); + copied = document.execCommand('copy'); + document.body.removeChild(textarea); + } catch { + copied = false; + } + } + if (copied) { + toast(label ? label + ' copied to clipboard.' : 'Copied to clipboard.'); + } else { toast('Clipboard access was denied.', 'error'); } } @@ -7534,10 +7842,18 @@ if (entry) copyToClipboard(entry.raw, 'Body'); return; } + const fsModeBtn = e.target.closest('[data-fs-mode]'); + if (fsModeBtn) { + renderCodeFullscreenBody(fsModeBtn.dataset.fsMode); + return; + } const fsBtn = e.target.closest('[data-body-fullscreen]'); if (fsBtn) { - const rawBlock = fsBtn.closest('.body-viewer')?.querySelector('.body-viewer-raw .code-block'); - openCodeFullscreen(rawBlock, fsBtn.dataset.bodyTitle); + const viewer = fsBtn.closest('.body-viewer'); + const rawBlock = viewer?.querySelector('.body-viewer-raw .code-block'); + // Open in whichever mode the pane is showing, so expanding never switches the view. + const mode = viewer?.querySelector('[data-body-mode].active')?.dataset.bodyMode || 'pretty'; + openBodyFullscreen(fsBtn.dataset.bodyFullscreen, fsBtn.dataset.bodyTitle, mode, rawBlock); return; } }); @@ -7814,6 +8130,7 @@ // existing composedPath-based outside-click closer instead of replacing it. if (e.key === 'Escape' && networkHostDropOpen) { networkHostDropOpen = false; renderNetworkHostDrop(); $('networkHostDropBtn')?.focus(); return; } if (e.key === 'Escape' && socketConnDropOpen) { socketConnDropOpen = false; renderSocketConnDrop(); $('socketConnDropBtn')?.focus(); return; } + if (e.key === 'Escape' && networkActionsMenuOpen) { networkActionsMenuOpen = false; renderNetworkDetail(); $('detailActionsMenuBtn')?.focus(); return; } if (typing || dialogOpen || e.metaKey || e.ctrlKey || e.altKey) return; if (e.key === '/') { const id = viewFilterInput[currentView]; if (id && $(id)) { e.preventDefault(); $(id).focus(); } } else if (e.key === '?') { e.preventDefault(); toggleShortcuts(); } diff --git a/sdk/server-ktor/src/main/resources/devconsole-web/index.html b/sdk/server-ktor/src/main/resources/devconsole-web/index.html index 1a8c1cc..f2070ff 100644 --- a/sdk/server-ktor/src/main/resources/devconsole-web/index.html +++ b/sdk/server-ktor/src/main/resources/devconsole-web/index.html @@ -57,6 +57,7 @@ + @@ -738,6 +739,12 @@