Skip to content

Limit roles to chosen companies, so a role's users reach only those companies' devices - #289

Merged
gccody merged 1 commit into
mainfrom
scoped-roles
Nov 11, 2026
Merged

gccody merged 1 commit into
mainfrom
scoped-roles

Conversation

@gccody

@gccody gccody commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

What

Every role now covers every company (the default) or only chosen companies, so a team of technicians can be given its own customers' devices and nothing else. The built-in Administrator role always covers every company.

  • Permissions are judged per device. A user holds a permission on a device when one of their roles grants it and covers the device's company. Holding a permission through one role and a company through another isn't enough (no permission-union × company-union leak). Devices in no company are reached only by roles over every company.
  • Instance-wide permissions (users.manage, roles.manage, settings.manage, authentication.manage, api_tokens.manage, audit.view, companies.manage, tags.manage, scripts.manage_shared, files.manage_shared, applications.manage, automation.manage, alerts.manage, custom_fields.manage) count only from roles over every company. A limited role can't be saved with one (400 instance_permission_on_limited_role), and one written into the database anyway grants nothing. Every permission is classified with a Reach next to its definition in rbac.rs, and GET /v1/permissions reports it.
  • Anything out of reach answers 404 exactly as a missing device or company does, and lists leave it out: the device wall and its event socket, device pages (notes, tags, metrics, thumbnail, system info, monitors, wakes), software search, the Updates and outdated-applications views, remote handoffs and sessions, terminals, the file browser, Wake-on-LAN, script runs and file deliveries and their history, jobs, alerts (list, counts, pages, acknowledgement, realtime), enrollment installers and deployment tokens, and the companies list.
  • Jobs from a limited role run only in reach: whole companies keep those in reach, tags resolve within reach, devices out of reach count as missing, and "every device" is refused (403 permission_denied) unless the role holding the payload's permission and jobs.run_wide covers every company. A job none of whose devices are in reach is hidden; its other devices are left off its pages.
  • API tokens inherit their user's reach (permission-limited tokens keep each permission's companies). SCIM/SSO group-to-role mapping is unchanged.
  • Role editors can't grant permissions in companies they don't reach (403 permission_escalation).
  • role.create / role.update audit events record all_companies and company_ids, plus previous_companies when they change.
  • Moving a device between companies, or deleting a company, re-checks open terminals and remote sessions, so they end if the device leaves the user's reach.
  • Maintenance windows (Put devices in maintenance, now, later or on a schedule, to hold back their alerts and scheduled automation #285): devices.maintenance is per company. A limited user sees and changes only windows on devices in reach, and starts maintenance on chosen devices, companies or tags in reach. "Every device" is refused; a device, company or window out of reach answers like a missing one; ending by tag ends only windows in reach. Maintenance policies, and install policies or jobs that set windows, need devices.maintenance in every company or within the job's reach.
  • Reports (Make fleet reports on demand and on a schedule, as pages that print as PDF and download as CSV, and email them #284): reports.manage is instance-wide (schedules run on any device and email anyone), and saving or running a schedule needs reports.view in every company. A user whose reports.view reaches only some companies runs reports on chosen companies or tags. The report keeps the maker's reach (within in its stored scope), so it covers only devices in reach. "Every device" is refused, and a company out of reach answers like a missing one. They see only reports they made, while what those covered stays in reach, and no schedules. New index idx_reports_requester serves their list.
  • Custom fields (Keep custom fields on devices and companies, with encrypted secrets, company values devices inherit, a device wall filter, and scripts that read and write them through the Agent #287): definitions stay instance-wide (custom_fields.manage). Device values are read and set within the device's company (devices.view / devices.edit), and company values within that company (companies.view / companies.manage). custom_fields.reveal is per company, so a secret is revealed only when the role holding it covers the device's or company's company. Bulk set refuses a device out of reach as missing, and the device wall filter matches only devices in reach. The Agent's script read and write path is unchanged: an Agent writes only its own device's run, and starting a run already needs reach.
  • Processes, services and registry (Show and change a device's processes and services from the website without a remote session #286, Browse and edit a Windows device's registry from the website without a remote session #288): every tools.* route goes through require_device, so a device out of reach answers like a missing one.

Website: the role editor has an "Every company / Only some companies" choice with a company picker, a Companies column, and disables instance-wide permissions (with an "Every company only" note) while limited. The job, deploy, report and maintenance dialogs hide "Every device" unless the user holds the permissions in every company (limited_permissions on GET /v1/account).

Docs: new docs/users-and-roles.md, README feature list and docs index, architecture.md, api.md, patch-management.md, custom-fields.md, remote-tools.md.

Design

  • server/src/access.rs holds the rules. Access maps each permission to the CompanySet (All or Only(ids)) of the roles that grant it, and Access::of(roles) builds it with instance permissions taken only from all-companies roles. Authorized carries it, with require_device (403 without the permission anywhere, 404 when missing or out of reach), require_company, require_everywhere, and devices_condition / companies_condition for SQL. Lists filter in their one query (an IN over agents.company_id, served by idx_agents_company), with no per-device round trips.
  • Schema: roles.all_companies and a role_companies table (cascade on role and company delete), edited directly in both 0001_initial.sql files.
  • Event socket: a limited user's socket keeps a per-socket filter. Snapshots hold only devices and companies in reach. Events about anything out of reach become {"type":"skipped","revision":n}, so the website's revision sequencing still holds. A device moving out of reach arrives as agent_deleted, and one moving in as agent_upsert. Alert and job events are filtered the same way, and alert counts are counted within reach.
  • Outdated applications: the fleet summary table counts every device, so for limited users the counts and pages are grouped from device_packages of devices in reach.

Judgment calls

  • Saving something that lends a permission to work running on any device needs that permission in every company: automation policies, alert rules that run scripts or remediation, and applications with custom installers. Turning such a rule off is always allowed.
  • Alert rules stay instance-wide, so every alerts.view holder sees the rules list. Only alerts are scoped.
  • Seeing other users' jobs comes from audit.view, which is instance-wide, so limited users see only their own jobs.
  • A limited user must pick a company for an Agent installer (400 company_required).
  • Wake responses leave out relays that are out of reach.
  • Tag lists show every tag, with device counts within reach.

How tested

  • New server/tests/company_scope.rs, with a limited technician against two companies plus a device in no company, on SQLite and PostgreSQL:
    • device list, companies and account
    • every device page: allowed in reach, and a 404 identical to a missing device's out of reach or in no company
    • notes and tag edits
    • moves in and out of reach
    • the two-role anti-leak case
    • instance-permission refusal, plus defense in depth when one is inserted directly
    • role scope validation and auditing, and company deletion
    • escalation through roles.manage
    • API tokens, both full and permission-limited
    • event socket filtering: snapshot, skipped, moves, company events
    • terminals, file browser, wake (single and batch), script runs, updates, packages and handoffs
    • jobs: all refused, and companies, tags and devices narrowed; jobs hidden after a move and devices pages filtered
    • alerts: list, counts, pages, acknowledgement, socket
    • installers and deployment tokens
    • outdated packages, their devices and the Updates fleet view
  • server/tests/query_plans.rs gains limited variants of the Updates fleet, outdated packages, device search and job device pages, checking they use indexes on both backends.
  • access.rs unit tests cover the anti-leak rule, NULL companies, instance permissions, administrators and tokens.
  • A maintenance test in company_scope.rs covers all refused, out-of-reach companies and devices answering as missing, starts by device, company and tag reaching only in-reach devices, others' windows hidden from list, get, end and edit, a device's windows, and ending by tag.
  • The reports test in company_scope.rs covers all refused, out-of-reach company = missing, company and tag reports counting only in-reach devices, hiding others' reports and schedules, hiding reports after the role's scope moves, and schedule saving refused with limited reports.view.
  • The custom fields test in company_scope.rs covers device and company values (read, set and reveal) in reach and as missing out of reach, with only in-reach reveals audited. It also covers bulk set with an outside device, the wall filter on text and secret fields, script read and write through a connected Agent (another device's Agent can't write into the run), split roles (view in one company, reveal in another) revealing nothing, and an all-companies role.
  • The remote tools test in company_scope.rs covers all 12 process, service and registry routes: they work in reach, answer as missing out of reach and in no company, and an all-companies role reaches every device.
  • realtime.rs: device moves now bump the access generation, so the "everyday work" test was changed to match.
  • cargo clippy --all-targets -D warnings, cargo fmt --check, and cargo test on SQLite and PostgreSQL 16. npm run verify for the website.

@gccody
gccody enabled auto-merge (squash) November 11, 2026 23:25
@gccody
gccody merged commit 72a7e07 into main Nov 11, 2026
9 checks passed
@gccody
gccody deleted the scoped-roles branch November 11, 2026 23:37
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.

1 participant