Gate each search domain on its own permission

Global search is gated on assets.view, and that was the whole check.
Behind it the route queried twelve domains: applications, the knowledge
base, guides, the staff directory, assets, measuring tools, USB devices,
printed parts, custom fields, notifications, hostnames, and vendor and
model reference data, plus addresses and subnets for an IP-shaped query.
None of them consulted their own permission.

So anyone who could look up an asset number could also read the staff
directory out of the HR database, the USB checkout history and who holds
which drive, and the network inventory. The SEARCH_DOMAINS settings look
like they bound this but they are site feature toggles, not
authorization.

Each domain now answers only to a caller who holds its permission.
Assets, custom fields and vendor/model stay on assets.view because all
three emit asset rows routed to asset pages.

caller_holds rather than require_permission: refusing the whole request
because one domain is off-limits would make search useless to anyone
holding less than everything. A domain the caller cannot see contributes
nothing and the rest still answers.

This is what makes assets.view safe to grant the guest role again. It
left the grantable set in 3474028 because of the leak above, and that
took the shop floor's asset lookup with it - a signed-out visitor at a
machine could no longer search for the asset number in front of them.
Now the grant reaches assets and nothing else.

The first version of the leak test passed with the gates removed. The
staff directory is an external HR database and is unreachable under
test, so the domain silently returned nothing and the assertion proved
nothing. It patches _employee_rows now, and fails when the gate goes.

Scope: product
(cherry picked from commit 0a01ddcf25)
This commit is contained in:
cproudlock
2026-08-31 08:34:04 -04:00
parent dd52666d6b
commit 4d0baaff77
5 changed files with 160 additions and 23 deletions

View File

@@ -18,7 +18,7 @@ from shopdb.core.models import (
from shopdb.core.api.settings import get_cached_settings
from shopdb.core.services.spellfix import Vocabulary
from shopdb.utils.responses import success_response
from shopdb.utils.authz import require_permission
from shopdb.utils.authz import require_permission, caller_holds
logger = logging.getLogger(__name__)
@@ -1233,22 +1233,42 @@ def global_search():
results = []
search_term = f'%{query}%'
# Run all search domains
results.extend(_search_applications(query, search_term))
results.extend(_search_knowledgebase(query, search_term))
results.extend(_search_guides(query, search_term))
results.extend(_search_employees(query, search_term))
results.extend(_search_assets(query, search_term))
results.extend(_search_measuringtools(query, search_term))
results.extend(_search_usbdevices(query, search_term))
results.extend(_search_printeditems(query, search_term))
results.extend(_search_customfields(query, search_term))
results.extend(_search_notifications(query, search_term))
results.extend(_search_hostnames(query, search_term))
results.extend(_search_vendor_model_type(query, search_term))
# Every domain answers only to a caller who holds ITS permission.
#
# The route is gated on assets.view alone, and that used to be the whole
# check: one permission then queried twelve domains, so it read out of the
# staff directory, the USB checkout history, the notification log and the
# network inventory to anybody who could look up an asset number. The
# SEARCH_DOMAINS settings are feature toggles a site sets, not authorization,
# and were never doing this job.
#
# caller_holds rather than require_permission: a search that refuses the
# whole request because one domain is off-limits is useless. Each domain
# simply contributes nothing.
domains = [
('assets.view', _search_assets),
# Emit asset results routed to an asset page, so they belong to the
# same grant as the assets themselves.
('assets.view', _search_customfields),
('assets.view', _search_vendor_model_type),
('applications.view', _search_applications),
('kb.view', _search_knowledgebase),
('guides.view', _search_guides),
('employees.view', _search_employees),
('measuringtools.view', _search_measuringtools),
('usb.view', _search_usbdevices),
('printedparts.view', _search_printeditems),
('notifications.view', _search_notifications),
('computers.view', _search_hostnames),
]
for permission, search in domains:
if caller_holds(permission):
results.extend(search(query, search_term))
# IP-specific searches
if classification['is_ip']:
# IP-specific searches. An address and the subnet it sits in are network
# inventory, and knowing what answers on 10.20.x is a different question
# from looking up an asset number.
if classification['is_ip'] and caller_holds('network.view'):
results.extend(_search_by_ip(query, search_term))
results.extend(_search_subnets(query))

View File

@@ -162,9 +162,6 @@ def full_permission_catalog(include_disabled=False):
# Each one has been read against what its routes actually return.
#
# Deliberately absent, with the reason, so nobody re-adds them by reflex:
# assets.view - gates the global search, which queries the staff directory
# and the USB, notification and network domains with no
# per-domain check of its own
# settings.view - every setting row, and secret masking is a substring test
# audit.view - usernames, addresses, and the before/after of every change
# users.view - gates nothing; list_users is require_role('admin')
@@ -174,6 +171,11 @@ CORE_GUEST_GRANTABLE = {
# Catalogue metadata. Safe only because the bytes moved to
# applications.download, which is not grantable.
'applications.view',
# Assets, and the global search over them. Safe only because search now
# gates each domain on its own permission: holding this reaches assets and
# nothing else, where it used to reach the staff directory and the USB,
# notification and network domains too.
'assets.view',
}