Skip to main content

device_compartment Validator Documentation

device_compartment Validator Documentation
FHIR Compartment-Based Access Control for Device-Linked Resources


Intent​

The device_compartment validator restricts access to resources explicitly linked to the authenticated device’s FHIR compartment. It ensures automated or headless devices (e.g., lab instruments, IoT sensors) can only interact with data they generate or manage.

Key Use Cases:

  • Lab equipment submitting DiagnosticReport results.
  • Wearable devices recording Observation data (e.g., heart rate).
  • Imaging machines updating Media resources. (ImagingStudy is excluded from the Device compartment, so it cannot be reached this way.)

Key Features​

  1. Role Enforcement:
    • Validates that the client has the Device role. Requests from non-device roles (e.g., Practitioner) are denied.
  2. Compartment Membership Check:
    • Confirms the requested resource type belongs to the FHIR Device compartment (e.g., Observation, DeviceUseStatement, DeviceRequest). Note that DeviceMetric is not in it — it is on the compartment's exclusion list, so pairing it with this validator denies every request.
  3. Reference Validation:
    • For create/update operations, ensures the resource references the device’s ID (e.g., Observation.device = Device/<id>). The write check accepts device, subject or performer — but the compartment reads Observation by subject, device only, so anchoring a write on performer alone creates a row the device cannot then read. Anchor on device.

Validation Logic​

StepActionError If Failed
1Check the caller's role is Device.403 Forbidden (Invalid role)
2Verify the resource type is in the Device compartment and not excluded from it.403 Forbidden (Invalid compartment)
3On read and search, verify the returned row references the caller in one of that type's compartment fields. If it does not, and org-aware access is enabled, fall through to the organization check below.403 Forbidden (Not in compartment)
4On create and update, verify the submitted payload references the caller as Device/<id> in one of device, subject, performer.403 Forbidden (Reference mismatch)
5On delete, fetch the stored resource and verify it references the caller.403 Forbidden

Example Configurations​

1. Read Access to Observation​

{
"client_role": "Device",
"entity_name": "Observation",
"operation": "read",
"validator": "device_compartment"
}

Explanation: A device can read an Observation that references it through Observation.device or Observation.subject — the two fields the Device compartment reads Observation by. Rows that reference some other device are filtered out of search results rather than returned.

2. Record that the device is in use​

{
"client_role": "Device",
"entity_name": "DeviceUseStatement",
"operation": "create",
"validator": "device_compartment"
}

Requirements: the body must reference the device in device, subject or performer. One is enough, and "device": { "reference": "Device/<device_id>" } is the natural one here — it is also the field the Device compartment reads DeviceUseStatement by, so a row anchored that way stays readable afterwards.


Organization-Aware Access​

By default the validator grants access only to resources in the device's own FHIR compartment. With org-aware mode enabled, a device may additionally reach resources belonging to its owning organization that do not reference the device directly. This replaces the workaround of setting such rules to allowed, which over-grants badly.

Reads and writes are enabled separately, by two different flags:

  • org_aware covers read and search. On its own it never widens a write.
  • org_aware_write covers create and update, and only when org_aware is set as well. A device that could modify records it cannot read would be incoherent, so the write flag is ignored on its own — and a warning is logged saying so.
  • delete is not covered by either flag. A device can only delete a resource that references it directly. Enabling org_aware_write does not change this.
Org-scoped access disables itself if a device can write its own organization

A device's organizations are read from Device.owner, from Device.location when resolve_location is on, and from Organization.partOf when inheritance_levels is above zero. If your RBAC rules let the Device role create or update any of those resources, a device could point itself at a different organization and then read or write there.

Rather than trust the rules to be written carefully, both org_aware and org_aware_write turn themselves off in that case. A device then reaches only its own compartment, never its organization's. The medical_device template is affected out of the box, because it grants Device / Device / update.

To use org-scoped access, remove the device's write access to Device, and to Location and Organization if you have enabled resolve_location or inheritance_levels. Setting those rules to forbidden is enough. Narrowing them to device_compartment is not, because a device would still be able to change its own location.

rbac.default_access must also be Forbidden. With Allowed, every combination you have not written an explicit rule for is granted, including a device writing its own Device record, so the boundary cannot hold and both flags stay off.

The Access scope setting makes these changes for you and reports anything that would switch the flags off before you save.

Three further properties are worth stating plainly:

  • Writes are checked on both sides. An update must name a resource already inside the device's organizations, and leave it inside them — a device cannot capture another organization's record, nor move its own out. A create that carries an id for an existing resource is treated as the overwrite it is, and is held to the same ownership check.
  • Fail-closed. If the Device resource cannot be resolved, access is denied. It never falls back to permitting the request.
  • Second chance, not a replacement. The direct compartment check runs first; the organization check is consulted only when that check did not already grant. Enabling either flag cannot narrow access a device already has.

A device's active organizations are derived from:

  • Primary: Device.owner → Organization reference.
  • Secondary (optional): Device.location → that Location.managingOrganization, when resolve_location is enabled.
  • Inheritance (optional): child organizations, discovered downward, up to inheritance_levels.
Check Device.owner exists before enabling org_aware

org_aware is only as good as the owner reference it resolves, and a device can be registered successfully and still have none. FHIR Device.owner is written only for a project whose patient custodianship is set to organization (ADR-020 mode 1). A project in mode 2 registers devices normally and they carry no owner.

A device registered under mode 2 is not backfilled if the project later switches to mode 1 — it stays unanchored for its lifetime.

Combined with the fail-closed rule above, an unanchored device resolves to no organization and every org-aware request it makes is denied, with nothing in the configuration looking wrong. Read the Device resources back and confirm each carries an owner before turning this on. Successful registration is not evidence of it.

Configuration​

Set under rbac.validators.device_compartment in the project's settings.json:

{
"rbac": {
"validators": {
"device_compartment": {
"org_aware": true,
"resolve_location": true,
"inheritance_levels": 1
}
}
}
}
ParameterTypeDefaultDescription
org_awareboolfalseEnable org-aware read and search. When false, behaviour is exactly as before — this is backward compatible.
org_aware_writeboolfalseEnable org-aware create and update. Requires org_aware — set on its own it is ignored and a warning is logged. Never covers delete.
resolve_locationbooltrueAlso resolve Device.location → Location.managingOrganization as a secondary organization source.
inheritance_levelsint0Downward child-organization traversal levels. 0 means none, and the value is capped at 5 however high you set it.

The RBAC rule itself does not change. The validator handles both direct-compartment and org-aware access from this configuration:

{ "client_role": "Device", "entity_name": "Patient", "operation": "read", "validator": "device_compartment" }

Resource → organization field map​

CategoryResource typesOrganization link
DirectPatientmanagingOrganization
Organizationthe resource's own id
LocationmanagingOrganization
HealthcareServiceprovidedBy
Device, DeviceDefinitionowner
PractitionerRoleorganization
ResearchStudysponsor
Accountowner
Patient-linkedObservation, DiagnosticReport, Condition, Procedure, MedicationRequest, MedicationAdministration, Encounter, CarePlan, AllergyIntolerance, Immunization, Specimen, ImagingStudy, ServiceRequest, DocumentReference, ClinicalImpression, Media, QuestionnaireResponse, FamilyMemberHistory, NutritionOrder, Consent, RelatedPerson, Goal, Coverage, ResearchSubject, ChargeItem, Invoice, CommunicationRequest, Claim, ClaimResponseresolved via subject, patient, beneficiary or individual → that Patient's managingOrganization
Device-linkedDeviceMetricresolved via source → that Device's owner

Resource types in no category fall through to the standard compartment rules, which is to say they are denied. A DeviceMetric with no source is denied.

Inheritance direction​

Device inheritance runs downward: a device owned by a parent organization can read data in its child departments. This is the opposite of practitioner role inheritance, which runs upward — a practitioner in a child organization gains the parent's resources. If you configure both, configure them separately and expect them to reach in opposite directions.


FHIR References​

  1. Device Compartment Definition:
    FHIR R4 Device Compartment (Official HL7 documentation).
  2. Resource Examples:

Authentication Notes​

  • Headless Devices: Supports OAuth 2.0 Device Code Flow for authentication.
  • Cloud Services: Not yet supported (future roadmap item).

Use Case: Lab Equipment Integration​

A hematology analyzer uses this validator to:

  1. Submit lab results: Creates Observation resources with device set to its Device ID.
  2. Record its own usage: Creates DeviceUseStatement resources naming it in device.

Error Handling:

  • A device attempting to access another device’s Observation data receives a 403 Forbidden response.