Repository navigation
fix(auth): bind data-rights access to stored request identity #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
seonghobae
merged 12 commits into
main
from
fix/data-rights-access-binding-20260817-1113
Aug 17, 2026
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a91e2ab
test(auth): bind data-rights access to stored request owner
seonghobae 075424e
fix(auth): derive data-rights access from stored request
seonghobae d1aa7d0
feat(auth): expose authoritative data-rights authorization
seonghobae e6aae3c
fix(auth): name bound data-rights scope construction errors
seonghobae cbfd57e
test(auth): authorize exact stored data-rights identity in-unit
seonghobae 928c607
fix(auth): document stored data-rights trust boundary
seonghobae 4588413
chore(auth): preserve result authorization while resolving main drift
seonghobae f237bd0
Merge branch 'main' into fix/data-rights-access-binding-20260817-1113
github-actions[bot] ae10783
test(auth-docs): require full stored-request boundary
seonghobae 46defcc
docs(auth): define stored-request authorization terms
seonghobae ec5ca93
fix(ci): document authorization contract test crate
seonghobae 3b4b6a5
Merge protected main into data-rights authorization binding
seonghobae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Data-rights authorization boundary | ||
|
|
||
| - Maturity: **PARTIAL** — the domain composition exists, while hosted route/repository integration and external identity-proof execution remain outside this slice. | ||
| - Ownership: `ContextualWisdomLab/psychometrics-commons` | ||
| - Governing contracts: PRD §11; TRD §10, §11, and §13; `docs/architecture/SECURITY_AND_DATA.md` | ||
|
|
||
| ## Purpose | ||
|
|
||
| A data export or deletion request is sensitive product state. `DataRightsRequest` is the authoritative product record for the request's tenant, participant owner, and opaque `request_ref`. Authorization must therefore bind to that stored record rather than to copies of those identifiers supplied in a URL, request body, or authenticated actor context. | ||
|
|
||
| `src/data_rights_authorization.rs::authorize_data_rights_request` is the composition boundary. It derives a participant-owned `ResourceScope` only from the stored `DataRightsRequest` and evaluates the existing `ManageOwnDataRights` permission. `src/lib.rs` exposes that module as product API surface; it does not transfer ownership to Keyverse or to a transport adapter. | ||
|
|
||
| ## Trust boundary | ||
|
|
||
| ```text | ||
| authenticated actor context | ||
| | | ||
| v | ||
| stored DataRightsRequest ------------------+ | ||
| (tenant_ref, participant_ref, request_ref) | | ||
| | | | ||
| +---------- authoritative identity --+ | ||
| v | ||
| participant-owned ResourceScope | ||
| | | ||
| v | ||
| ManageOwnDataRights | ||
| | | ||
| allow only exact owner/tenant | ||
| ``` | ||
|
|
||
| The actor proves who is asking; the stored request proves which product resource is being asked about. The server never substitutes the actor's own tenant, participant, or path parameter for a stored request attribute. If any required binding is missing, malformed, cross-tenant, or owned by another participant, authorization denies access. | ||
|
|
||
| ## Unchanged architecture | ||
|
|
||
| This change does **not** add a new permission, credential system, data-rights lifecycle state, database object, cross-service database access, or identity-provider responsibility. Keyverse remains the authentication/federation owner; Psychometrics Commons remains the owner of product authorization and the data-rights aggregate. Because ownership, dependency direction, state machine, and persistence schema are unchanged, no superseding ADR is required. The implementation narrows an existing TRD §11 authorization invariant at the adapter-facing boundary. | ||
|
|
||
| ## Required evidence | ||
|
|
||
| - `tests/data_rights_authorization_binding.rs` proves exact stored-owner success and fail-closed cross-tenant, cross-participant, and missing-identity cases. | ||
| - `src/data_rights_authorization.rs` unit tests prove malformed stored identity cannot construct an authorization scope. | ||
| - `tests/data_rights_authorization_documentation.rs` prevents the trust-boundary mapping from disappearing silently. | ||
| - Hosted HTTP/repository adapters must call this stored-record composition before reading, exporting, deleting, or disclosing request state; transport-level negative tests remain required before GA. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| //! Authorization for one participant-owned data-rights request using stored identity. | ||
| //! | ||
| //! A [`DataRightsRequest`] is the authoritative stored record for its tenant, participant | ||
| //! owner, and request identifier. That identifier is *opaque*: callers treat it as an | ||
| //! issued label and must not infer meaning or sequence from it. API or persistence boundary | ||
| //! code (often called an *adapter*) must use those stored values instead of rebuilding the | ||
| //! authorization target from URL fields, request bodies, or the authenticated actor. | ||
| //! | ||
| //! Authorization evaluates a *resource scope*: the tenant, participant owner, resource | ||
| //! kind, and exact request identifier that together describe what is being accessed. Using | ||
| //! the stored request prevents a *confused deputy* defect, where trusted server code is | ||
| //! tricked into acting on a different request because caller-controlled identity fields were | ||
| //! substituted for the stored ones. Invalid or incomplete bindings *fail closed*: access is | ||
| //! denied rather than guessed or defaulted. | ||
| //! | ||
| //! The architecture boundary and unchanged ownership are documented in | ||
| //! `docs/architecture/DATA_RIGHTS_AUTHORIZATION.md`. This module adds no credential, | ||
| //! permission, lifecycle, or database ownership; it composes the existing product-owned | ||
| //! `ManageOwnDataRights` permission with the stored data-rights request. | ||
|
|
||
| use crate::authorization::{ | ||
| authorize, AuthorizationContext, AuthorizationError, ProductPermission, ResourceKind, | ||
| ResourceScope, | ||
| }; | ||
| use crate::data_rights::DataRightsRequest; | ||
|
|
||
| /// Authorize the authenticated participant to manage one stored data-rights request. | ||
| /// | ||
| /// The stored request is the domain record (sometimes called an *aggregate*) that owns the | ||
| /// tenant, participant, and request identifiers used here. The function builds the | ||
| /// authorization target only from that record, then evaluates [`ProductPermission::ManageOwnDataRights`]. | ||
| /// It does not trust copies of those identifiers supplied by a caller. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`AuthorizationError`] when the authenticated tenant or participant does not own | ||
| /// the stored request, participant identity is missing, an identifier is invalid, or another | ||
| /// authorization invariant cannot be proven. In all such cases access is denied instead of | ||
| /// falling back to a guessed/default identity. | ||
| pub fn authorize_data_rights_request( | ||
| actor: &AuthorizationContext, | ||
| request: &DataRightsRequest, | ||
| ) -> Result<(), AuthorizationError> { | ||
| authorize_bound_data_rights_request( | ||
| actor, | ||
| request.tenant_ref(), | ||
| request.participant_ref(), | ||
| request.request_ref(), | ||
| ) | ||
| } | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
|
|
||
| #[allow(clippy::question_mark)] | ||
| fn authorize_bound_data_rights_request( | ||
| actor: &AuthorizationContext, | ||
| tenant_ref: &str, | ||
| participant_ref: &str, | ||
| request_ref: &str, | ||
| ) -> Result<(), AuthorizationError> { | ||
| let resource = match ResourceScope::participant_owned( | ||
| ResourceKind::DataRightsRequest, | ||
| tenant_ref, | ||
| participant_ref, | ||
| request_ref, | ||
| ) { | ||
| Ok(resource) => resource, | ||
| Err(error) => return Err(error), | ||
| }; | ||
| authorize(actor, &resource, ProductPermission::ManageOwnDataRights) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::authorize_bound_data_rights_request; | ||
| use crate::authorization::{AuthorizationContext, AuthorizationError, ProductRole}; | ||
|
|
||
| fn actor() -> AuthorizationContext { | ||
| AuthorizationContext::new( | ||
| "tenant_alpha", | ||
| "subject_alpha", | ||
| Some("participant_alpha"), | ||
| &[ProductRole::Participant], | ||
| ) | ||
| .unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn bound_request_rejects_invalid_resource_references() { | ||
| let actor = actor(); | ||
|
|
||
| for (tenant_ref, participant_ref, request_ref) in [ | ||
| ("", "participant_alpha", "data_rights_request_alpha"), | ||
| ("tenant_alpha", "", "data_rights_request_alpha"), | ||
| ("tenant_alpha", "participant_alpha", ""), | ||
| ] { | ||
| assert_eq!( | ||
| authorize_bound_data_rights_request( | ||
| &actor, | ||
| tenant_ref, | ||
| participant_ref, | ||
| request_ref, | ||
| ), | ||
| Err(AuthorizationError::InvalidReference) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn bound_request_authorizes_the_exact_stored_identity() { | ||
| let actor = actor(); | ||
| assert_eq!( | ||
| authorize_bound_data_rights_request( | ||
| &actor, | ||
| "tenant_alpha", | ||
| "participant_alpha", | ||
| "data_rights_request_alpha", | ||
| ), | ||
| Ok(()) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //! Regression tests for data-rights access bound to the stored request identity. | ||
|
|
||
| use psychometrics_commons_runtime::authorization::{ | ||
| AuthorizationContext, AuthorizationError, ProductRole, | ||
| }; | ||
| use psychometrics_commons_runtime::data_rights::{DataRightsRequest, DataRightsRequestKind}; | ||
| use psychometrics_commons_runtime::data_rights_authorization::authorize_data_rights_request; | ||
|
|
||
| fn request() -> DataRightsRequest { | ||
| DataRightsRequest::new( | ||
| "data_rights_request_alpha", | ||
| "tenant_alpha", | ||
| "participant_alpha", | ||
| DataRightsRequestKind::Export, | ||
| "participant_export_scope_v1", | ||
| 1_786_240_000_000, | ||
| ) | ||
| .unwrap() | ||
| } | ||
|
|
||
| fn actor(tenant_ref: &str, participant_ref: Option<&str>) -> AuthorizationContext { | ||
| AuthorizationContext::new( | ||
| tenant_ref, | ||
| "subject_alpha", | ||
| participant_ref, | ||
| &[ProductRole::Participant], | ||
| ) | ||
| .unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn data_rights_access_uses_tenant_owner_and_identity_from_the_stored_request() { | ||
| let actor = actor("tenant_alpha", Some("participant_alpha")); | ||
|
|
||
| assert_eq!(authorize_data_rights_request(&actor, &request()), Ok(())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn data_rights_access_rejects_cross_tenant_actor_context() { | ||
| let actor = actor("tenant_beta", Some("participant_alpha")); | ||
|
|
||
| assert_eq!( | ||
| authorize_data_rights_request(&actor, &request()), | ||
| Err(AuthorizationError::CrossTenantDenied) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn data_rights_access_rejects_a_different_authenticated_participant() { | ||
| let actor = actor("tenant_alpha", Some("participant_beta")); | ||
|
|
||
| assert_eq!( | ||
| authorize_data_rights_request(&actor, &request()), | ||
| Err(AuthorizationError::OwnerMismatch) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn data_rights_access_requires_an_operational_participant_identity() { | ||
| let actor = actor("tenant_alpha", None); | ||
|
|
||
| assert_eq!( | ||
| authorize_data_rights_request(&actor, &request()), | ||
| Err(AuthorizationError::ParticipantIdentityRequired) | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const AUTHORIZATION_ARCHITECTURE: &str = | ||
| include_str!("../docs/architecture/DATA_RIGHTS_AUTHORIZATION.md"); | ||
|
|
||
| #[test] | ||
| fn stored_data_rights_authorization_boundary_stays_documented() { | ||
| for required in [ | ||
| "PARTIAL", | ||
| "TRD §11", | ||
| "DataRightsRequest", | ||
| "src/data_rights_authorization.rs::authorize_data_rights_request", | ||
| "ManageOwnDataRights", | ||
| "stored request", | ||
| "cross-tenant", | ||
| "no superseding ADR is required", | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
| ] { | ||
| assert!( | ||
| AUTHORIZATION_ARCHITECTURE.contains(required), | ||
| "data-rights authorization architecture must retain {required:?}" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.