Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ version = "0.5.14"
anyhow = "1.0"
async-bb8-diesel = "0.3"
async-trait = "0.1.92"
aws-lc-rs = "1.18"
base64 = "0.23"
bb8 = "0.9"
chrono = "0.4"
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,23 @@ The following endpoints are injected by the `inject_endpoints` macro:
| DELETE | /oauth/client/{client_id}/secret/{secret_id} | Delete an existing secret from an OAuth client |
| POST | /oauth/client/{client_id}/redirect_uri | Add a redirect URI to an OAuth cleint |
| DELETE | /oauth/client/{client_id}/redirect_uri/{redirect_uri_id} | Delete an existing redirect URI from an OAuth client |
| GET | /oauth/client/{client_id}/service-account/permission | View the permissions an OAuth client holds as a service account |
| POST | /oauth/client/{client_id}/service-account/permission | Grant a single permission to an OAuth client's service account |
| DELETE | /oauth/client/{client_id}/service-account/permission | Remove a single permission from an OAuth client's service account |
| GET | /oauth/client/{client_id}/service-account/token | View the unexpired, unrevoked tokens issued to an OAuth client's service account |
| DELETE | /oauth/client/{client_id}/service-account/token/{token_id} | Revoke a token issued to an OAuth client's service account |
| POST | /oauth/client/{client_id}/service-account/key | Register an Ed25519 public key that an OAuth client's assertions are verified with |
| DELETE | /oauth/client/{client_id}/service-account/key/{key_id} | Remove a public key from an OAuth client |
| GET | /login/oauth/{provider}/code/authorize | Start an OAuth authorization_code flow |
| GET | /login/oauth/{provider}/code/callback | Internal url for handling return calls from external OAuth providers |
| POST | /login/oauth/{provider}/code/token | Complete an authorization_code flow by exchanging an authorization code for an access token |
| GET | /login/oauth/{provider}/device | Start an OAuth device_code flow |
| POST | /login/oauth/{provider}/device/exchange | Complete a device_code flow by exchanging a request for an access token |
| POST | /oauth/token | Issue an access token to an OAuth client's service account through a client_credentials grant authenticated with a `private_key_jwt` assertion |
| GET | /.well-known/openid-configuration | Retrive OpenID configuartion information. Specifically the jwks url |
| GET | /.well-known/jwks.json | Retrieve JWKS for verifying access tokens |
| GET | /self | View information about the calling user |
| GET | /api-user/{identifier} | View information about a specific user |
| POST | /api-user | Create a new user |
| POST | /api-user/{identifier} | Update information on an existing user |
| GET | /api-user/{identifier}/token | View all API tokens for an existing user |
| POST | /api-user/{identifier}/token | Create a new API token for an existing user |
| GET | /api-user/{identifier}/token/{token_identifier} | View an existing API token |
Expand All @@ -97,7 +103,8 @@ The following endpoints are injected by the `inject_endpoints` macro:
| POST | /api-user-provider/{identifier}/link-token | TBD |
| GET | /group | View all groups |
| POST | /group | Create a new group |
| PUT | /group/{group_id} | Update an existing group |
| POST | /group/{group_id}/permission | Add a single permission to an existing group |
| DELETE | /group/{group_id}/permission | Remove a single permission from an existing group |
| DELETE | /group/{group_id} | Delete an existing group |
| GET | /mapper | View all mappers |
| POST | /mapper | Create a new mapper |
Expand Down
4 changes: 3 additions & 1 deletion remix-auth-vapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ export type GetUserResponse<Permission extends string = string> = {
}

/**
* Access token claims, per RFC 9068. Note that `scp` is a single
* Access token claims, per RFC 9068 §2.2. Note that `scp` is a single
* space-delimited string on the wire, not an array.
*/
export type VApiAccessTokenClaims = {
iss: string
aud: string
sub: string
prv: string
/** The client the token was issued through. For an OAuth login this is the OAuth client id. */
client_id?: string | null
scp: string
exp: number
nbf: number
Expand Down
59 changes: 51 additions & 8 deletions v-api-permission-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,7 @@ fn system_permission_tokens() -> TokenStream {
CreateUserApiProviderLinkToken,
#[v_api(scope(to = "group:info:w", from = "group:info:w"))]
CreateGroup,
#[v_api(
contract(kind = drop, variant = GetGroupsJoined),
scope(to = "group:info:r")
)]
#[v_api(scope(to = "group:info:r"))]
GetGroup(newtype_uuid::TypedUuid<v_model::AccessGroupId>),
#[v_api(
expand(kind = iter, variant = GetGroup, source = actor, field = groups)
Expand Down Expand Up @@ -953,6 +950,8 @@ fn permission_storage_trait_tokens(
) -> proc_macro2::TokenStream {
let contract_tokens = permission_storage_contract_tokens(permission_type, contract_settings);
let expand_tokens = permission_storage_expand_tokens(permission_type, expand_settings.clone());
let expand_value_tokens =
permission_storage_expand_value_tokens(permission_type, &expand_settings);
let implies_tokens = permission_storage_implies_tokens(
permission_type,
implies_settings,
Expand All @@ -964,11 +963,49 @@ fn permission_storage_trait_tokens(
impl v_model::permissions::PermissionStorage for #permission_type {
#contract_tokens
#expand_tokens
#expand_value_tokens
#implies_tokens
}
}
}

/// Generates `expand_value`, which splits a permission that carries a set of values into one
/// permission per value.
///
/// Only iter expansions without an external source qualify. These are permissions who hold all of
/// the direct values required for expansion. Expansions that depend on an external source are left
/// alone.
fn permission_storage_expand_value_tokens(
permission_type: &Ident,
expand_settings: &[(Variant, ExpandSettings)],
) -> proc_macro2::TokenStream {
let branches = expand_settings
.iter()
.filter(|(_, setting)| matches!(setting.kind, ExpandKind::Iter) && setting.source.is_none())
.map(|(variant, setting)| {
let set_ident = &variant.ident;
let value_ident = &setting.variant;
quote! {
#permission_type::#set_ident(values) => Some(
values
.iter()
.map(|value| #permission_type::#value_ident(value.clone()))
.collect()
),
}
})
.collect::<Vec<_>>();

quote! {
fn expand_value(&self) -> Option<Vec<Self>> {
match self {
#(#branches)*
_ => None,
}
}
}
}

fn permission_storage_contract_tokens(
permission_type: &Ident,
contract_settings: Vec<(Variant, ContractSettings)>,
Expand Down Expand Up @@ -1190,8 +1227,10 @@ fn permission_storage_expand_tokens(

quote! {
#permission_type::#variant_ident => {
for f0 in &#source.#field {
expanded.push(#permission_type::#target_variant(f0.clone()))
if let Some(#source) = #source {
for f0 in &#source.#field {
expanded.push(#permission_type::#target_variant(f0.clone()))
}
}
}
}
Expand All @@ -1218,7 +1257,11 @@ fn permission_storage_expand_tokens(
ExternalSource::Actor => {
let source = source.to_ident();
quote! {
#permission_type::#variant_ident => expanded.push(#permission_type::#target_variant(#source.#field.clone())),
#permission_type::#variant_ident => {
if let Some(#source) = #source {
expanded.push(#permission_type::#target_variant(#source.#field.clone()))
}
}
}
},
ExternalSource::Extension => {
Expand Down Expand Up @@ -1251,7 +1294,7 @@ fn permission_storage_expand_tokens(
quote! {
fn expand(
collection: &v_model::Permissions<Self>,
actor: &v_model::ApiUser<Self>,
actor: Option<&v_model::ApiUser<Self>>,
actor_permissions: Option<&v_model::Permissions<Self>>,
extensions: &v_model::ArcMap,
) -> v_model::Permissions<Self> {
Expand Down
107 changes: 16 additions & 91 deletions v-api-permission-derive/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,112 +585,37 @@ fn test_can_grant_all_empty_targets() {
assert!(caller.can_grant_all(&targets));
}

/// Construct an `ApiUser` that holds the dynamic `GetGroupsJoined` permission
/// and is currently a member of `group_id`.
fn joined_actor(
user_id: TypedUuid<UserId>,
group_id: TypedUuid<v_model::AccessGroupId>,
) -> v_model::ApiUser<VPermission> {
use chrono::Utc;

let mut groups = BTreeSet::new();
groups.insert(group_id);

v_model::ApiUser::<VPermission> {
id: user_id,
permissions: vec![VPermission::GetGroupsJoined].into(),
groups,
created_at: Utc::now(),
updated_at: Utc::now(),
deleted_at: None,
}
}

/// Exploit for the broken `contract(kind = drop, ...)` handling.
///
/// `GetGroup(id)` is annotated `contract(kind = drop, variant = GetGroupsJoined)`
/// and is the inverse of `GetGroupsJoined`'s
/// `expand(kind = iter, variant = GetGroup, source = actor, field = groups)`.
///
/// v-api round-trips permissions through `expand` -> `contract` on every login
/// of an existing user (`register_api_user` -> `add_permissions_to_user` ->
/// `save_api_user`). `contract()` MUST be the exact inverse of `expand()` so
/// that permissions derived from dynamic group membership are never persisted
/// as static grants.
///
/// Because the `ContractKind::Drop` arm emits no match branch, the concrete
/// `GetGroup(id)` produced by expansion falls through to the catch-all and is
/// stored verbatim. After an admin removes the user from the group, this stale
/// direct grant still authorizes `get_group` (and member enumeration) forever.
///
/// This assertion currently FAILS, demonstrating the broken revocation. Once
/// the Drop arm discards the concrete variant, it will hold.
/// `contract` keeps a permission over one specific group exactly as it was granted, and keeps the
/// dynamic `GetGroupsJoined` as well. Neither is ever rewritten into the other. Granting
/// visibility of a single group must never widen into visibility of every group the holder has
/// joined, and nothing in the write paths contracts an expanded set, so there is no expansion
/// derived `GetGroup` that would need discarding.
#[test]
fn test_contract_drops_dynamic_get_group_grant() {
use std::collections::HashMap;
fn test_contract_keeps_group_permissions_as_written() {
use v_model::{AccessGroupId, Permissions};

let user_id = TypedUuid::<UserId>::new_v4();
let group_id = TypedUuid::<AccessGroupId>::new_v4();
let actor = joined_actor(user_id, group_id);

// The user's stored permissions contain only the dynamic GetGroupsJoined.
let stored: Permissions<VPermission> = vec![VPermission::GetGroupsJoined].into();

// On login, stored permissions are expanded against current group membership.
let expanded = VPermission::expand(&stored, &actor, Some(&actor.permissions), &HashMap::new());
assert!(
expanded
.iter()
.any(|p| matches!(p, VPermission::GetGroup(g) if *g == group_id)),
"precondition: expand() should produce a concrete GetGroup for the joined group, \
got {:?}",
expanded,
);
let stored: Permissions<VPermission> = vec![
VPermission::GetGroup(group_id),
VPermission::GetGroupsJoined,
]
.into();

// Before persisting, the expanded set is contracted. The concrete
// GetGroup(group_id) must NOT survive — it is purely derived from dynamic
// group membership and would otherwise become a permanent direct grant.
let contracted = VPermission::contract(&expanded);
let contracted = VPermission::contract(&stored);

assert!(
!contracted
contracted
.iter()
.any(|p| matches!(p, VPermission::GetGroup(g) if *g == group_id)),
"broken revocation: contract() persisted a concrete GetGroup({:?}) derived from group \
membership. This stale direct grant survives removal from the group, so the user \
keeps get_group access forever. contracted = {:?}",
group_id,
"a single group grant was not stored as written: {:?}",
contracted,
);
}

/// Companion property: `contract()` must restore the dynamic `GetGroupsJoined`
/// so that the permission survives the login round trip. The current Drop arm
/// neither drops the concrete variant nor re-inserts the target, so
/// `GetGroupsJoined` is silently lost from the persisted set.
///
/// This assertion currently FAILS.
#[test]
fn test_contract_restores_get_groups_joined() {
use std::collections::HashMap;
use v_model::{AccessGroupId, Permissions};

let user_id = TypedUuid::<UserId>::new_v4();
let group_id = TypedUuid::<AccessGroupId>::new_v4();
let actor = joined_actor(user_id, group_id);

let stored: Permissions<VPermission> = vec![VPermission::GetGroupsJoined].into();
let expanded = VPermission::expand(&stored, &actor, Some(&actor.permissions), &HashMap::new());
let contracted = VPermission::contract(&expanded);

assert!(
contracted
.iter()
.any(|p| matches!(p, VPermission::GetGroupsJoined)),
"contract() failed to restore the dynamic GetGroupsJoined permission, so it is lost on \
the next login and the user's group visibility is no longer tied to membership. \
contracted = {:?}",
"the dynamic joined groups permission was lost: {:?}",
contracted,
);
assert_eq!(2, contracted.iter().count());
}
1 change: 1 addition & 0 deletions v-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ yup-oauth2 = { workspace = true }

[dev-dependencies]
async-trait = { workspace = true }
aws-lc-rs = { workspace = true }
mockall = { workspace = true }
rsa = { workspace = true, features = ["pem"] }
tracing-subscriber = { workspace = true }
Expand Down
Loading
Loading