-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(context-memory): enforce read-path visibility on memory endpoints #28289
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
pmbrull
merged 1 commit into
open-metadata:main
from
pmbrull:pmbrull/context-memory-visibility
May 20, 2026
+400
−11
Merged
Changes from all commits
Commits
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
157 changes: 157 additions & 0 deletions
157
...ice/src/main/java/org/openmetadata/service/resources/context/ContextMemoryVisibility.java
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,157 @@ | ||
| /* | ||
| * Copyright 2024 Collate | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.openmetadata.service.resources.context; | ||
|
|
||
| import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; | ||
|
|
||
| import jakarta.ws.rs.ForbiddenException; | ||
| import jakarta.ws.rs.core.SecurityContext; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import org.openmetadata.schema.entity.context.ContextMemory; | ||
| import org.openmetadata.schema.entity.context.MemoryVisibility; | ||
| import org.openmetadata.schema.entity.teams.User; | ||
| import org.openmetadata.schema.type.EntityReference; | ||
| import org.openmetadata.schema.type.Include; | ||
| import org.openmetadata.service.Entity; | ||
| import org.openmetadata.service.security.DefaultAuthorizer; | ||
| import org.openmetadata.service.security.policyevaluator.SubjectContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Visibility rules for {@link ContextMemory}. Every read on {@code /v1/contextCenter/memories} runs | ||
| * through this check so a non-admin user cannot read another user's PRIVATE memory via the public | ||
| * API. Visibility is independent of the OSS policy/authorizer model because it is driven by the | ||
| * per-memory {@code shareConfig} (visibility + sharedWith) rather than role/policy. | ||
| */ | ||
| public final class ContextMemoryVisibility { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(ContextMemoryVisibility.class); | ||
|
|
||
| private ContextMemoryVisibility() {} | ||
|
|
||
| public static boolean isVisibleToUser(ContextMemory memory, String userName, boolean isAdmin) { | ||
| if (isAdmin) { | ||
| return true; | ||
| } | ||
| if (isOwnedBy(memory, userName)) { | ||
| return true; | ||
| } | ||
| if (memory.getShareConfig() == null) { | ||
| return false; | ||
| } | ||
| MemoryVisibility visibility = memory.getShareConfig().getVisibility(); | ||
| if (visibility == MemoryVisibility.ENTITY) { | ||
| return true; | ||
| } | ||
| if (visibility == MemoryVisibility.SHARED) { | ||
| return isInSharedWithList(memory, userName); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static void enforceVisibility(ContextMemory memory, String userName, boolean isAdmin) { | ||
| if (!isVisibleToUser(memory, userName, isAdmin)) { | ||
| throw new ForbiddenException(getVisibilityDeniedMessage(memory)); | ||
| } | ||
| } | ||
|
|
||
| public static void enforceVisibility(ContextMemory memory, SecurityContext securityContext) { | ||
| if (memory == null || securityContext == null || securityContext.getUserPrincipal() == null) { | ||
| return; | ||
| } | ||
| SubjectContext subject = DefaultAuthorizer.getSubjectContext(securityContext); | ||
| enforceVisibility(memory, securityContext.getUserPrincipal().getName(), subject.isAdmin()); | ||
| } | ||
|
pmbrull marked this conversation as resolved.
|
||
|
|
||
| public static List<ContextMemory> filterByVisibility( | ||
| List<ContextMemory> memories, String userName, boolean isAdmin) { | ||
| return memories.stream().filter(m -> isVisibleToUser(m, userName, isAdmin)).toList(); | ||
| } | ||
|
|
||
| public static List<ContextMemory> filterByVisibility( | ||
| List<ContextMemory> memories, SecurityContext securityContext) { | ||
| if (memories == null || memories.isEmpty()) { | ||
| return memories; | ||
| } | ||
| if (securityContext == null || securityContext.getUserPrincipal() == null) { | ||
| return memories; | ||
| } | ||
| SubjectContext subject = DefaultAuthorizer.getSubjectContext(securityContext); | ||
| return filterByVisibility( | ||
| memories, securityContext.getUserPrincipal().getName(), subject.isAdmin()); | ||
| } | ||
|
|
||
| public static boolean isOwnedBy(ContextMemory memory, String userName) { | ||
| if (memory.getOwners() == null || memory.getOwners().isEmpty() || userName == null) { | ||
| return false; | ||
| } | ||
| return memory.getOwners().stream() | ||
| .anyMatch(o -> userName.equals(o.getName()) || userName.equals(o.getFullyQualifiedName())); | ||
| } | ||
|
|
||
| private static boolean isInSharedWithList(ContextMemory memory, String userName) { | ||
| if (memory.getShareConfig() == null || memory.getShareConfig().getSharedWith() == null) { | ||
| return false; | ||
| } | ||
| Set<String> principalIds = resolvePrincipalIdentifiers(userName); | ||
| return memory.getShareConfig().getSharedWith().stream() | ||
| .anyMatch( | ||
| sp -> | ||
| sp.getPrincipal() != null | ||
| && (principalIds.contains(sp.getPrincipal().getName()) | ||
| || principalIds.contains(sp.getPrincipal().getFullyQualifiedName()))); | ||
| } | ||
|
|
||
| private static Set<String> resolvePrincipalIdentifiers(String userName) { | ||
| Set<String> ids = new HashSet<>(); | ||
| ids.add(userName); | ||
| try { | ||
| User user = | ||
| Entity.getEntityByName(Entity.USER, userName, "teams,domains", Include.NON_DELETED); | ||
| addRefNames(ids, user.getTeams()); | ||
| addRefNames(ids, user.getDomains()); | ||
| } catch (Exception e) { | ||
| LOG.debug("Could not resolve teams/domains for user '{}'", userName, e); | ||
| } | ||
| return ids; | ||
| } | ||
|
pmbrull marked this conversation as resolved.
|
||
|
|
||
| private static void addRefNames(Set<String> ids, List<EntityReference> refs) { | ||
| for (EntityReference ref : listOrEmpty(refs)) { | ||
| if (ref.getName() != null) { | ||
| ids.add(ref.getName()); | ||
| } | ||
| if (ref.getFullyQualifiedName() != null) { | ||
| ids.add(ref.getFullyQualifiedName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static String getVisibilityDeniedMessage(ContextMemory memory) { | ||
| if (memory.getShareConfig() == null) { | ||
| return "Not authorized to access this memory."; | ||
| } | ||
| MemoryVisibility visibility = memory.getShareConfig().getVisibility(); | ||
| if (visibility == null || visibility == MemoryVisibility.PRIVATE) { | ||
| return "Memory with visibility PRIVATE is only accessible to its owner."; | ||
| } | ||
| if (visibility == MemoryVisibility.SHARED) { | ||
| return "Memory with visibility SHARED is only accessible to explicitly shared users."; | ||
| } | ||
| return "Not authorized to access this memory."; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
listendpoint fetches a page oflimitParamresults from the database, then filters out non-visible memories. This causes two problems:limit=10and 4 of 10 results are filtered, the client receives only 6 items despite more visible items existing in subsequent pages.new ResultList<>(visible)which setspagingtonull, so the client has noafter/beforecursor to continue pagination.This means non-admin users may see incomplete data or be unable to paginate at all when many memories in a page are not visible to them.
Consider pushing visibility filtering into the database query (e.g., a predicate on the
ListFilter) or implementing a loop that fetches additional pages untillimitParamvisible items are collected. At minimum, preserve the original paging cursors when returning a filtered result so clients can still advance.Preserve paging cursors when returning filtered results so clients can still paginate. This is a minimal fix; a proper solution would push filtering into the query layer.:
Was this helpful? React with 👍 / 👎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in fe1a8b9 — switched the
listendpoint to mutate the originalResultListviasetData(filtered)so paging cursors (before/after),total,errorsandwarningsare preserved instead of being nulled bynew ResultList<>(visible).The "short page" sub-issue (e.g.
limit=10returning 6 visible) is a real but separate problem — fixing it cleanly needs the visibility predicate pushed into the SQLListFilter(or a loop-and-refill at the resource). Leaving that as a follow-up; preserving paging is the minimum fix for this PR so cursor pagination is no longer broken for non-admin callers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The referenced commit
fe1a8b9f8adoesn't appear to be in this branch — only6fa00661is present — and the code at line 154 still readsreturn new ResultList<>(visible). So the paging-cursor fix hasn't landed yet.The approach you described (mutating the original
ResultListviasetData(filtered)) is the right minimal fix: it preservespaging.before/paging.after,total,errors, andwarningswhile replacing onlydatawith the visibility-filtered list. That change is a one-liner swap of line 154:Would you like me to apply that now?