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
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,12 @@ public ResultList<ResourcePermission> getPermissionForPolicies(
@Parameter(description = "List of policy of ids", schema = @Schema(type = "UUID"))
@QueryParam("ids")
List<UUID> ids) {
// User must have read access to policies
OperationContext operationContext =
new OperationContext(Entity.POLICY, MetadataOperation.VIEW_ALL);
// User must have read access to each policy. OperationContext is stateful - it drops
// operations as they are satisfied - so each decision needs its own, matching the pattern
// documented in ViewPermissionFilter.
for (UUID id : ids) {
OperationContext operationContext =
new OperationContext(Entity.POLICY, MetadataOperation.VIEW_ALL);
ResourceContext<?> resourceContext = new ResourceContext(Entity.POLICY, id, null);
authorizer.authorize(securityContext, operationContext, resourceContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
@Slf4j
public class OperationContext {
@Getter @NonNull private final String resource;
List<MetadataOperation> operations; // All requested operations
// Working list of requested operations. Policy evaluation drains granted operations from this
// list via iterator.remove() to track what remains ungranted; an empty list means everything was
// allowed. Because the drain is destructive, this list MUST NOT be reused across authorization
// decisions without calling {@link #resetOperations()} first.
List<MetadataOperation> operations;
// Immutable snapshot of the originally requested operations, used by {@link #resetOperations()}
// to restore the working list so a reused context evaluates each decision against the full set.
private List<MetadataOperation> requestedOperations;
@Getter private JsonPatch patch;

public OperationContext(@NonNull String resource, MetadataOperation... operations) {
this.resource = resource;
this.operations = new ArrayList<>();
this.operations.addAll(List.of(operations));
this.requestedOperations = List.of(operations);
this.operations = new ArrayList<>(this.requestedOperations);
}

public OperationContext(@NonNull String resource, JsonPatch patch) {
Expand All @@ -37,13 +44,30 @@ public List<MetadataOperation> getOperations(ResourceContextInterface resourceCo
return operations;
}
if (patch != null) { // Lazy initialize operations for PATCH
operations = new ArrayList<>();
operations.addAll(JsonPatchUtils.getMetadataOperations(resourceContext, patch));
requestedOperations =
List.copyOf(JsonPatchUtils.getMetadataOperations(resourceContext, patch));
operations = new ArrayList<>(requestedOperations);
LOG.debug("Lazy initializing operations to {}", Arrays.toString(operations.toArray()));
}
return operations;
}

/**
* Restores the working operations list to the originally requested operations.
*
* <p>Policy evaluation drains granted operations from {@link #operations} to track what remains
* ungranted, and an empty list makes {@link PolicyEvaluator#hasPermission} skip deny evaluation
* and pass its final check vacuously. Reusing one OperationContext across decisions without a
* reset therefore lets the first success drain the list and every later decision authorize
* without evaluating any deny rule - an authorization bypass. {@link
* PolicyEvaluator#hasPermission} calls this before every evaluation so a reused context is safe.
*/
void resetOperations() {
if (requestedOperations != null) {
operations = new ArrayList<>(requestedOperations);
}
}

public static List<MetadataOperation> getAllOperations(MetadataOperation... exclude) {
return getOperations("", exclude);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public static void hasPermission(
@NonNull SubjectContext subjectContext,
@NonNull ResourceContextInterface resourceContext,
@NonNull OperationContext operationContext) {
// Restore the full set of requested operations so a reused OperationContext evaluates this
// decision against the complete list, not one drained by a previous decision. Without this,
// a first success empties the list and every later decision skips deny evaluation and passes
// the final check vacuously - an authorization bypass.
operationContext.resetOperations();

// First run through all the DENY policies
evaluateDenySubjectPolicies(subjectContext, resourceContext, operationContext);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package org.openmetadata.service.security.policyevaluator;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.openmetadata.schema.EntityInterface;
import org.openmetadata.schema.entity.policies.accessControl.Rule;
import org.openmetadata.schema.entity.teams.User;
import org.openmetadata.schema.type.EntityReference;
import org.openmetadata.schema.type.MetadataOperation;
import org.openmetadata.schema.type.TagLabel;
import org.openmetadata.service.Entity;
import org.openmetadata.service.security.AuthorizationException;
import org.openmetadata.service.security.policyevaluator.SubjectContext.PolicyContext;

/**
* Reproduces the OperationContext reuse bypass: {@link PolicyEvaluator#hasPermission} drains the
* requested operations list in place as it grants them, and an empty list makes it skip deny
* evaluation and pass its final check vacuously. Reusing one OperationContext across decisions
* therefore lets a first success authorize every later decision without evaluating any deny rule -
* an authorization bypass reachable on GET /v1/permissions/policies?ids=.
*/
class ContextReuseDenyBypassTest {

@Test
void freshOperationContextEnforcesConditionalDeny() {
// Control case: a fresh OperationContext per decision. The owned resource is allowed (deny
// condition !isOwner() is false); the non-owned resource is denied (condition is true).
assertDoesNotThrow(() -> authorizeWithFreshContext(true));
AuthorizationException denied =
assertThrows(AuthorizationException.class, () -> authorizeWithFreshContext(false));
assertTrue(denied.getMessage().contains("caller"), denied.getMessage());
}

@Test
void reusedOperationContextBypassesConditionalDeny() {
// The first (owned) call drains the operations list. Without a reset the second (non-owned)
// call would skip deny evaluation and authorize vacuously; with the reset it must throw.
AuthorizationException denied =
assertThrows(
AuthorizationException.class, () -> authorizeReusingSameContext(List.of(true, false)));
assertTrue(denied.getMessage().contains("caller"), denied.getMessage());
}

@Test
void reusedOperationContextEnforcesDenyOnEveryDeniedId() {
// Only denied ids: the first decision itself must throw, proving the deny is not skipped.
assertThrows(
AuthorizationException.class,
() -> authorizeReusingSameContext(List.of(false, false, false)));
}

@Test
void freshContextAllowsWhenNoDenyApplies() {
// Sanity: a reused context among owned-only resources must not throw.
assertDoesNotThrow(() -> authorizeReusingSameContext(List.of(true, true, true)));
}

@Test
void resetOperationsRestoresDrainedList() {
// The reset must restore the working list to the originally requested operations after a
// prior evaluation drained it, so a reused context evaluates the full set again.
OperationContext operationContext =
new OperationContext(Entity.POLICY, MetadataOperation.VIEW_ALL);
assertEquals(List.of(MetadataOperation.VIEW_ALL), operationContext.getOperations(null));
operationContext.getOperations(null).clear(); // simulate the drain from a prior decision
assertTrue(operationContext.getOperations(null).isEmpty());
operationContext.resetOperations();
assertEquals(List.of(MetadataOperation.VIEW_ALL), operationContext.getOperations(null));
}

private void authorizeWithFreshContext(boolean owned) {
User caller = new User().withName("caller");
SubjectContext subject = new SubjectContext(caller, null);
try (MockedStatic<SubjectCache> mockedCache = Mockito.mockStatic(SubjectCache.class)) {
stubPolicies(mockedCache, caller.getName());
OperationContext operationContext =
new OperationContext(Entity.POLICY, MetadataOperation.VIEW_ALL);
PolicyEvaluator.hasPermission(subject, policyResource(owned, caller), operationContext);
}
}

private void authorizeReusingSameContext(List<Boolean> ownedFlags) {
User caller = new User().withName("caller");
SubjectContext subject = new SubjectContext(caller, null);
try (MockedStatic<SubjectCache> mockedCache = Mockito.mockStatic(SubjectCache.class)) {
stubPolicies(mockedCache, caller.getName());
OperationContext operationContext =
new OperationContext(Entity.POLICY, MetadataOperation.VIEW_ALL); // created ONCE
for (Boolean owned : ownedFlags) {
PolicyEvaluator.hasPermission(subject, policyResource(owned, caller), operationContext);
}
}
}

private static void stubPolicies(MockedStatic<SubjectCache> mockedCache, String userName) {
mockedCache.when(() -> SubjectCache.getPolicies(userName)).thenReturn(policies());
mockedCache.when(() -> SubjectCache.getVisitedTeams(userName)).thenReturn(new ArrayList<>());
}

private static List<PolicyContext> policies() {
CompiledRule allowRule =
new CompiledRule(
new Rule()
.withName("allow-view-all")
.withResources(List.of(Entity.POLICY))
.withOperations(List.of(MetadataOperation.VIEW_ALL))
.withEffect(Rule.Effect.ALLOW));
CompiledRule denyRule =
new CompiledRule(
new Rule()
.withName("deny-not-owner")
.withResources(List.of(Entity.POLICY))
.withOperations(List.of(MetadataOperation.VIEW_ALL))
.withEffect(Rule.Effect.DENY)
.withCondition("!isOwner()"));
PolicyContext policyContext =
new PolicyContext(
Entity.ROLE, "callerRole", "callerRole", "callerPolicy", List.of(allowRule, denyRule));
return new ArrayList<>(List.of(policyContext));
}

private static ResourceContextInterface policyResource(boolean owned, User caller) {
EntityReference owner = owned ? userRef(caller.getName()) : userRef("someone-else");
return new ResourceContextInterface() {
@Override
public String getResource() {
return Entity.POLICY;
}

@Override
public List<EntityReference> getOwners() {
return List.of(owner);
}

@Override
public List<TagLabel> getTags() {
return null;
}

@Override
public EntityInterface<?> getEntity() {
return null;
}

@Override
public List<EntityReference> getDomains() {
return null;
}

@Override
public boolean isEntityTypeLevel() {
return false;
}
};
}

private static EntityReference userRef(String name) {
return new EntityReference().withType(Entity.USER).withName(name).withFullyQualifiedName(name);
}
}
Loading