Skip to content

feat(web): global company switcher in the sidebar - #425

Merged
BaaRai merged 3 commits into
BaaRai:mainfrom
joanrodriguez:feat/company-switcher
Nov 22, 2026
Merged

BaaRai merged 3 commits into
BaaRai:mainfrom
joanrodriguez:feat/company-switcher

Conversation

@joanrodriguez

@joanrodriguez joanrodriguez commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The panel cannot be scoped to one target. companies is the ownership layer, but a company could only be attached while creating a task. A second engagement mixed into the dashboard, the findings list, traffic and the asset tabs.

Change

A global company switcher in the sidebar header, under the logo, built like a workspace switcher: search, an explicit All companies entry, the asset count per company, and a link to the companies tab. The choice persists per browser.

Endpoints that filter server-side

View Endpoint Parameter
Findings, flat list and dashboard panels GET /api/exploration/findings (paged and bare array) company=<id>
Findings, grouped view and each group's page GET /api/exploration/findings/groups, GET /api/exploration/findings?task_id= company=<id>
Findings, asset tree GET /api/exploration/findings/asset-tree company=<id> (kept while the picked node is cleared)
Findings, stat cards GET /api/exploration/findings/stats company=<id>
Findings, filtered export GET /api/exploration/findings/export?scope=filtered company=<id>
Tasks GET /api/tasks company=<id>
Traffic GET /api/traffic company=<id>, resolved to the hosts the company owns
Assets GET /api/assets (plain and DSL path) company_id=<id>

Not filtered: GET /api/stats (the dashboard's global counters), the global count on GET /api/traffic (the traffic stat card), and GET /api/assets/counts.

The browser only displays what these endpoints return. asset_scope=c:<id> still works and means the same as company=<id>.

Attribution rule

  • A finding belongs to company C when one of its live assets has company_id = C.
  • A finding with no live asset (empty asset_ids, or only deleted asset ids) belongs to C only when its task has a task_scope row of kind company for C.
  • Any other finding shows under All companies only.
  • An unknown or non-numeric company id returns nothing.
  • With a company selected, the stat cards leave out archived (cold) tasks. Their aggregates carry no asset attribution.

Assets DSL

company_id is ANDed as a SQL clause next to the compiled DSL. It is no longer pasted into the DSL text, so ) or a top-level OR cannot escape it.

Testing

  • go test -p 1 on every package against Postgres 16. gofmt and go vet clean.
  • TestFindingCompanyScopeFilter (rewritten): own asset, task-scoped unanchored finding, deleted asset, company without assets, nonexistent id, non-numeric id, a company node outside the selected company, the asset tree and the stat cards.
  • TestListAssetsCompanyFilterSurvivesDSLOr: the exact payload company_id=A) OR (company_id=B and a plain top-level OR. It fails on the previous head.
  • TestQueryDSLForCompanyOrDoesNotEscape: the same at the store level.
  • TestListTasksFiltersByCompany: company A, company B, non-numeric id, all companies.
  • TestPageHostsAllowlist: exact host allowlist, MatchNone, no filter.
  • Web: tsc --noEmit, test:i18n, test:polling, test:replay, test:upload, test:bash, build:static. biome lint reports no new errors on the touched files.

🤖 Generated with Claude Code

https://claude.ai/code/session_01BWbhPFZifkVU75uaD5NYj5

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes.

  1. Findings with no anchored asset appear under every company scope. applyAssetScope sets IncludeUnanchored = true, and TestFindingCompanyScopeFilter expects a company that owns no assets, and even a nonexistent company id, to see them. Pick an attribution (exclude them from company views, or keep them only in "all companies") and pin it.
  2. The task list is filtered in the browser (tasks/page.tsx), not on the server. With paging the counts and the loaded page are wrong. Filter tasks server-side and keep the client to the display.
  3. The switcher is announced as narrowing the dashboard, the asset tabs and the traffic list. Please state which endpoints actually filter server-side today; groups, the stat cards and the asset tree are not in this diff.
  4. Add a test that a DSL expression with a top-level OR cannot escape the company_id filter (server/assets.go wraps it as (company_id=N) AND (dsl)).
  5. CI: gofmt on traffic/traffic.go, missing i18n keys for the new strings, and the PR conflicts with main in server/server.go. Rebase and run the web checks.

@BaaRai

BaaRai commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks for this, the switcher itself is a good addition and the sidebar component reads well. I checked the company scope on the current head and it does not hold in the places below, so I am asking for changes before this can go in.

How the responses below were obtained: the HTTP handlers of this PR were called directly (not through the router or a logged-in session) against a fresh database holding two companies (ids 1 and 2, one root_domain asset each), a third company with no asset (id 3), one task scoped to company 1 and one scoped to company 2 (a task_scope row of kind company each), and four findings: one on company 1's asset, one on company 2's asset, and two that belong to the company 2 task and have no live asset (one with an empty asset_ids, one whose only asset id no longer exists). Line numbers are those of the head of this PR at the time of writing.

(a) The company filter on the DSL path of GET /api/assets can be escaped

  • Request: GET /api/assets?company_id=1&dsl=company_id=1)%20OR%20(company_id=2
    Fully percent-encoded, the same request is GET /api/assets?company_id=1&dsl=company_id%3D1%29%20OR%20%28company_id%3D2. Both decode to the DSL company_id=1) OR (company_id=2.
  • Response: 200, count=2, total=2, and assets holds the asset of company 1 and the asset of company 2. For comparison, GET /api/assets?company_id=1&type=root_domain returns total=1, and GET /api/assets?company_id=1&dsl=company_id%3D2 returns total=0.
  • Expected: with company_id=1, no asset of another company is returned, whatever the DSL contains.
  • Root cause: server/assets.go:371-377 puts the scope into the DSL text. Line 375 rewrites the parameter to (company_id=N) AND ( + the user DSL + ), and only then is the text validated and run (server/assets.go:378-388). With the payload the text is (company_id=1) AND (company_id=1) OR (company_id=2): the ) of the payload closes the wrapper's group, the text is balanced so it passes validation (the response is a 200, not a 400), and the OR (company_id=2) part is no longer inside the company condition (the response holds company 2's asset).
  • Fix: do not build the scope in the DSL text. Either parse the user DSL and AND the company leaf into the AST, or validate the user DSL on its own and apply the company filter outside the text, the way type and task_id are already passed next to the DSL (server/assets.go:385-388).
  • Regression test: please add one with this exact payload: company_id=<A> with dsl=company_id=<A>) OR (company_id=<B> must never return an asset of B. It can sit next to TestQueryDSLInScopeOrDoesNotEscape in db/asset_dsl_or_scope_test.go.

(b) Findings with no live asset are shown under every company

  • Request: GET /api/exploration/findings?page=1&limit=50&asset_scope=c%3A1
  • Response: 200, total=3: the finding on company 1's asset, plus the two findings that belong to the task scoped to company 2 and have no live asset. The same two findings come back for asset_scope=c%3A3 (a company with no asset and no task, total=2) and for asset_scope=c%3A999000444 (no such company, total=2). asset_scope=c%3Aabc returns total=0, which is right.
  • Root cause: db/finding_assets.go:669-695 sets IncludeUnanchored (line 678) for every numeric company id, without checking that the company exists, and db/findings.go:255-271 then adds "asset_ids is empty, or none of its asset ids exists" to the condition with no reference to the company.
  • Expected rule: a finding with no live asset is visible under company C only if its task has a task_scope row with kind = 'company' and company_id = C (the rows TaskCompanyIDsContext already reads, db/task_context.go:352-356). A company id that does not exist, or is not a number, yields nothing. A finding whose task has no company scope stays visible under "All companies" only.
  • Fix: carry the company id instead of the boolean, and add the task condition above to the unanchored branch of the query.
  • Test that pins it: TestFindingCompanyScopeFilter (db/finding_assets_test.go:250) passes on this head while asserting the current behaviour: it expects the unanchored finding under the other company (:299), under a company without assets (:300) and under a nonexistent company id (:301). Please rewrite it with these cases: an unanchored finding of a task scoped to B is visible under B and not under A; a company without assets sees only the unanchored findings of tasks scoped to it; a nonexistent id and a non-numeric id return nothing; a finding whose asset row was deleted behaves like one with an empty asset_ids.
  • The PR description says a company id that does not exist returns empty on every endpoint, while the PR's own test TestFindingCompanyScopeFilter expects a nonexistent company id to see the unanchored finding (db/finding_assets_test.go:301). The changelog entry says such findings "stay visible under their company" (CHANGELOG.md:9). The description and the changelog both need to state the rule that is implemented.

(c) Views that are not filtered on the server

1. The grouped findings view never sends asset_scope.

  • File: web/src/app/(main)/function/findings/page.tsx. Confirmed from the code: the grouped request does not pass assetScope (the active companyScope). This holds for the api.findingGroups call in refreshGroups (:466-502) and for the per-group loader (:504-533, dependency list at :563), while the flat list call (:396) passes assetScope as activeAssetScope and the asset tree call (:437) passes it as companyScope. This is from the page code; I did not drive the page in a browser.
  • Server side, observed: GET /api/exploration/findings/groups?page=1&limit=10 returns finding_total=4; with &asset_scope=c%3A1 it returns finding_total=3. The endpoint honours the parameter, the page does not send it.
  • Expected: with a company selected, the grouped view covers the same findings as the flat view.
  • Fix: pass the scope in both calls and add it to both dependency lists.

2. The asset tree ignores asset_scope.

  • Request: GET /api/exploration/findings/asset-tree?asset_scope=c%3A1
  • Response: 200, finding_total=4, and the nodes include company 2 and its asset (c:2, a:2). The response is identical to the one without the parameter.
  • Root cause: buildFindingAssetTree clears the scope on entry (db/finding_assets.go:156-157), which also discards the company the page sends for the tree (findings/page.tsx:437).
  • Expected: with a company selected, the tree holds only that company's nodes and counts.
  • Fix: keep the company restriction when building the tree and clear only the picked node, for example by carrying the company in its own filter field and query parameter instead of in asset_scope.

3. The stat cards are not scoped.

  • Request: GET /api/exploration/findings/stats
  • Response: total=4, high=4, with or without asset_scope=c%3A1, while the scoped list (item (b)) returns 3.
  • Root cause: the page sends no parameter (web/src/lib/api.ts:607), the handler reads none (server/server.go:2347-2348), and FindingStats() takes no filter (db/findings.go:642).
  • Expected: with a company selected, the cards count the same findings as the list below them.
  • Fix: accept the scope on the stats endpoint, send it from the page, and count with the same filter as the list.

(d) Formatting and missing translations

  • gofmt: gofmt fails on traffic/traffic.go: gofmt -l on the Go files of the PR lists it. The comments of the existing PageQuery fields were not realigned after Hosts and MatchNone were added (traffic/traffic.go:1059-1071). Fix: gofmt -w traffic/traffic.go.
  • i18n: the new company-switcher keys are missing from tr.json. npm run test:i18n (node scripts/check-i18n.mjs) exits 1 with "Missing tr.json entries (7)". The keys, all in web/src/app/(main)/_components/sidebar/company-switcher.tsx: "Switch company" (:79), "All companies" (:95, :115), "Every target in view" (:98), "Search company…" (:109), "Failed to load companies" (:111), "No company found" (:111), "Manage companies" (:143). Fix: add the seven entries to tr.json. The rebase onto main brings zh-CN.json, which will need the same seven keys.

The PR is currently conflicting, so there is no CI run on this head; the two points above come from running gofmt and the i18n check on the head directly, not from a CI result.

Rebase on main required.

joanrodriguez and others added 3 commits November 22, 2026 09:29
The panel had no way to scope it to one target. Companies exist as the
ownership layer, but only the task form offered one, so a second engagement
mixed into every list.

Sidebar header, under the logo: a company picker that defaults to
"All companies" and persists per browser. Every asset-backed view follows
it — findings (all three views plus the asset tree and export), the
dashboard's finding panels, the task list, the traffic list and the assets
tabs.

Server side the switch rides on the existing ownership column: findings and
traffic take a company, and the assets list takes company_id on both the
plain and the DSL path. Findings with no live asset anchor stay visible
under their company, because they belong to no other one; the strict
subtree filter would hide them from every view.

Tests: TestFindingCompanyScopeFilter (own assets, another company, a
company with none, a nonexistent id) and TestPageHostsAllowlist. Full
./db/ ./traffic/ ./server/ pass.

Co-Authored-By: Claude Code <[email protected]>
Claude-Session: https://claude.ai/code/session_01BWbhPDnkSEFNzBXG4wDvTj
- Findings with no live asset belong to a company only through their
  task's company scope; unknown or non-numeric ids match nothing.
- Filter tasks by company on the server (GET /api/tasks?company=).
- Scope the grouped findings view, the asset tree and the stat cards.
- AND company_id outside the compiled asset DSL so OR cannot escape it.
- Add the 7 company switcher keys to tr.json and zh-CN.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BWbhPFZifkVU75uaD5NYj5
@joanrodriguez

joanrodriguez commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. Rebased on main; all 5 points are addressed.

  1. Unanchored findings. I implemented your rule. A finding with no live asset shows under company C only when its task has a task_scope row of kind company for C. Otherwise it shows under "All companies" only. Unknown and non-numeric ids return nothing. The boolean is gone; the filter carries the company id (FindingFilter.Company, db/findings.go, db/finding_assets.go). TestFindingCompanyScopeFilter is rewritten with your cases: task-scoped to B is visible under B and not A, a company without assets sees only its tasks' unanchored findings, nonexistent and non-numeric ids return nothing, and a deleted asset row behaves like empty asset_ids. The changelog states the same rule.
  2. Task list. GET /api/tasks?company=<id> filters on the server (listTasks, server/server.go). tasks/page.tsx sends the id and only displays. Test: TestListTasksFiltersByCompany.
  3. Server-side endpoints. I added the filter to all 3 views you named. The grouped view and each group's page send company. The asset tree takes company as its own parameter and keeps it while it clears the picked node. Stats take company through FindingStatsForCompany with the list's rule. TestFindingCompanyScopeFilter also checks the tree and the stats under a company. The PR description now lists every endpoint that filters, and the 3 that don't.
  4. DSL escape. company_id is no longer written into the DSL text. CountDSLForCompany / QueryDSLForCompany AND it as a SQL clause next to the compiled DSL (db/asset_dsl.go, server/assets.go). TestListAssetsCompanyFilterSurvivesDSLOr sends your exact payload and a plain top-level OR. It fails on the old head with "returned company B's asset". TestQueryDSLForCompanyOrDoesNotEscape in db/asset_dsl_or_scope_test.go covers the store.
  5. CI. Ran gofmt on traffic/traffic.go. Added the 7 keys to tr.json and zh-CN.json. Rebased on main and resolved server/server.go onto parseTrafficPageQuery. Locally: go test -p 1 on every package, go vet, tsc, every web test script and build:static pass.

@BaaRai ready for another look.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — all five review items verified on 9734b4b against a fresh PostgreSQL 16 database:

  • Unanchored findings: scoped strictly by task_scope (kind = 'company'); never shown under other companies, nonexistent IDs, or non-numeric IDs (verified across paginated findings, bare array findings, and deleted-asset findings).
  • Server-side task filter: GET /api/tasks?company=<id> filters server-side and returns empty for unknown/non-numeric IDs.
  • Server-side filtering across views: findings groups, finding stat cards, finding asset tree, paginated assets, and traffic host allowlist all filter server-side with matching counts.
  • DSL top-level OR safety: company_id is ANDed outside the compiled DSL in CountDSLForCompany/QueryDSLForCompany, so top-level OR queries cannot escape the active company.
  • Formatting / i18n / merge: gofmt clean, test:i18n (5/5) and tsc --noEmit pass, and clean merge against main.

Two minor follow-ups (non-blocking):

  • GET /api/assets/counts does not yet filter by company_id, so asset tab badges and the dashboard's "Asset nodes" card still show global counts.
  • On the dashboard, only findings narrow by the active company (tasks, traffic, and assetCounts remain global).

@BaaRai
BaaRai merged commit 76bd0ab into BaaRai:main Nov 22, 2026
5 checks passed
@BaaRai

BaaRai commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Thanks @joanrodriguez. Merged. The company scope holds on the server across findings, tasks, groups, stats, the asset tree, assets and traffic; the two remaining global counts are noted as follow-ups.

cybersicilian added a commit to tenpointdata/ARTEX that referenced this pull request Nov 24, 2026
…d audit schema (#31)

* E5: engagement authorization artifact in the target gate, hash-chained audit schema

Migration 0016: audit_chain (append-only, SHA-256 linked, row triggers on
intercept_pending/artex_users/engagement_authorizations), artex_users,
engagement_authorizations (versioned, immutable rows).

The artifact (scope, time window, technique classes) is consulted inside the
existing target gate: run() and ungated() check it first, and target-less calls
go through authorizeCall from CheckTargets/CheckUngated. Refusals are decided
intercept rows. Card c-669406.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* E5: authorization and audit-chain endpoints, chain verification with tamper tests

GET/POST /api/tasks/{id}/authorization, POST .../revoke;
GET /api/intercept/history/chain and /chain/verify. Verification recomputes
every hash, checks seq continuity and the head row, and compares each
intercept history row with its newest chain entry. Card c-669406.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* E5: governance endpoints read the task id from the path, HTTP test

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* E5: RBAC (admin/operator/reviewer/client_read) over the existing JWT, users API, docs

Console users in artex_users; tokens carry uid/uv and are checked against the
row on every request (fails closed). The legacy ARTEX login stays admin.
requireAuth enforces a declarative route policy (server/rbac_policy.go) matched
on the mux pattern; refusals answer 403 and are appended to the audit chain.
Logins are audited. Fix: the authorization check no longer claims the call's
trace entry unless it refuses (kept exact intercept correlation).
docs/GOVERNANCE.md documents roles, the artifact and the chain. Card c-669406.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* E5: review fixes — spawn_task inherits the artifact, snapshot/UTC verify, tighter reads

- spawn_task: the child inherits the caller's current artifact (read from the
  call's scope); a revoked/expired artifact refuses the spawn, and the gate
  keeps window/revocation on spawn_task.
- Local tools return before the artifact lookup (never refused, no query).
- Verify runs in one repeatable-read UTC snapshot; the row trigger pins UTC.
- Reviewers lose raw traffic exchange/blob/HAR/stream and conversation messages.
- change-password is limited to the ARTEX session; actor names reserved;
  control characters stripped from audited usernames.
- Docs and CHANGELOG. Card c-669406.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* HANDOFF: E5 state, triage, decisions, follow-up cards

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* HANDOFF: renumber triage

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* HANDOFF: deltas stamp

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* db: chain audit row entries at commit (migration 0018) so the head lock no longer stalls gate writes (c-ba9f64)

The intercept_pending, artex_users and engagement_authorizations audit
triggers become DEFERRABLE INITIALLY DEFERRED constraint triggers. The
audit_chain_head lock is now taken only during COMMIT, after every other
lock, so a task archive no longer stalls gate decisions, logins and
rbac.denied entries, and write-then-lock transactions cannot deadlock on
the head. Concurrency tests reproduce both failures without 0018.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* HANDOFF: deltas stamp (c-ba9f64)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* E5: bind client_read users to their companies (c-5b5421)

client_read was global: a client account for company A could read every
client's findings, reports and exports. Bindings now live in
artex_user_companies (migration 0019, audited at commit like the other
governance tables) and are managed at GET/PUT /api/users/{id}/companies.

Every route rbacPolicy opens to client_read has a binder (client_scope.go)
that runs after the role check: findings routes are narrowed to the bound
company through the BaaRai#425 company filter, ids and tasks outside the binding
answer 404, an unbound client gets 403, and a route without a binder is
refused. Task ownership and the unanchored-finding fallback count only
operator-made company scope rows, so an agent cannot widen what a client
sees. The client report and stats no longer carry global asset/traffic
counts. Live retests across all clients moved to the read permission.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs: client binding (c-5b5421) in GOVERNANCE, CHANGELOG, HANDOFF

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* HANDOFF: deltas stamp (c-5b5421)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* web(E5): session role/permissions context, governance API client, role-filtered sidebar, username login (c-869050)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* web(E5): Users page, task Authorization tab, audit Chain tab with Verify; client_read-safe sidebar (c-869050)

- /system/users (admin): table with inline role/enabled, set password, delete,
  client_read company bindings, add-user dialog.
- Task detail Authorization tab: current version + status badge, version
  history table, admin-only new-version form and revoke.
- Approval Records: Approvals | Chain tabs; chain table with filters, paging,
  entry sheet, Verify (head_seq/head_hash, broken_at, rows_mismatched).
- client_read sessions skip the company switcher and the global retest poll.
- verify.sh runs the governance unit tests and the i18n check; tr/zh-CN strings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* HANDOFF: E5 web UI state, triage, decisions, follow-up cards (c-869050)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* HANDOFF: shorten c-869050 current-state entry

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* HANDOFF: deltas stamp (c-869050)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants