Repository navigation
OpenConceptLab/ocl_online#730 | A REST edit that sends no access keeps its row's current access; search hits load through the database's access check - #1048
Conversation
…s its row's current access; search hits load through the database's access check - A PUT or PATCH of a source or collection (HEAD or version) that sends no public_access reads its row under a lock, in the transaction that saves it, and keeps that row's access. A concurrent write that made the row private, or public, isn't overwritten, and the concepts and mappings of a source keep its access too. - Where a search filters its hits by access (lists, the repo search and $match), the hits load through the access check the lists use, read from the database: an index document that's stale, such as a row made private before it was reindexed, is no hit. It keeps search's rule for the items a requester created. A page can then hold fewer hits than its limit, while num_found stays the index's count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTLYUUSTSy1TKzRmkd
… its source to be viewable too; a public org stored as Edit stays a hit - A source made private commits before its save signal makes its concepts and mappings private, so their rows can still be public for a while: a hit for one also needs its source to be viewable, in the same query. Creators keep theirs, as before. $match loads its hits the same way. - Organization.get_viewable reads public_can_view's values (View and the retired Edit), as the index does, so a public org stored as Edit is still a hit. - The REST edit test checks the source's mappings too, and compares the row itself, not ids from two tables. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTLYUUSTSy1TKzRmkd
Codex review, pass 1 (codex-cli 0.169.0, commit 18f9972)Verdict: 3 findings: 1 P1, 2 P2.
Replies (858313d; the stale-search and make-private tests pass, 30;
findings.json{
"reviewer": "codex-cli 0.169.0",
"perspective": "defects",
"mode": "full",
"commit": "18f99729",
"verdict": "findings",
"prior": [],
"findings": [
{
"id": "F1.1",
"severity": "P1",
"tag": null,
"file": "core/common/mixins.py",
"line": 910,
"claim": "Search can still return concepts and mappings from a source that the database already marks private.",
"scenario": "An explicit REST privacy edit commits the source row before its post-save signal finishes updating the child rows. During that interval, an anonymous search can return a concept or mapping because both its database row and its stale index document still say View. The source's own URL already denies access.",
"action": "Check current parent-source visibility in the same batched query as child visibility, preserving the existing creator exception where it applies. Add a regression test that makes the source private while leaving the child rows and index documents unchanged.",
"lines_delta": null,
"confidence": "high"
},
{
"id": "F1.2",
"severity": "P2",
"tag": null,
"file": "core/capabilities/tests/test_make_private.py",
"line": 390,
"claim": "The concurrency test can fail on unrelated collection edits and does not verify the mapping behavior it claims to cover.",
"scenario": "Source and Collection use separate primary-key sequences. If their fixtures have the same numeric ID, the collection subtest that makes the collection private also asserts that the source's concept became private. The preceding source subtest left that concept public, so the test fails despite a correct collection edit. The fixture also creates no mapping, so a mapping-only regression passes.",
"action": "Compare the model instance with self.source and create and refresh a mapping alongside the concept when asserting child access.",
"lines_delta": null,
"confidence": "high"
},
{
"id": "F1.3",
"severity": "P2",
"tag": null,
"file": "core/common/mixins.py",
"line": 310,
"claim": "Compressed searches report the filtered result count in num_found even though the stated behavior is to retain the index count.",
"scenario": "A source search has one indexed hit that the database check drops. The paginated response reports num_found of 1 and num_returned of 0. The same search with Compress set to true reports num_found of 0 because that branch takes the length of the filtered hits.",
"action": "Use the index count for compressed searches too, or explicitly document the compressed response as an exception. Add a stale-hit assertion for num_found.",
"lines_delta": null,
"confidence": "high"
}
]
} |
…nd mapping hits by their model; org hits are checked in the requester's own scope too - A concept's collection-versions search has collection hits, though it searches as a concept view does: the source check now applies by the hits' model, not the view's, so that search answers again instead of failing. - An org search in the requester's own scope reads the index's member list, so a former member could still find a private org there: org hits load through Organization.get_viewable() in every scope. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTLYUUSTSy1TKzRmkd
snyaggarwal
left a comment
There was a problem hiding this comment.
Approving. The REST-edit lock and the database check on search hits both look right to me. The check only narrows what the index's filter already let through, so where it's more permissive than the index (creators, members), results can't widen. Every document model that reaches the check has a get_viewable. $match is the same.
Codex pass 1 (F1.1 to F1.3) already covered the window after a source is made private, the test's id comparison and Compress's num_found, so I have nothing to add there.
Inline, all optional: four nits, two docstring wording suggestions, one pre-existing.
| queue_schema_update_task = obj.is_validation_necessary() | ||
| try: | ||
| with transaction.atomic() if keep_private else nullcontext(): | ||
| with transaction.atomic() if lock else nullcontext(): |
There was a problem hiding this comment.
Nit (optional): every REST edit that sends no access now runs inside transaction.atomic(), so CelerySignalProcessor.handle_save defers the index enqueue with robust=True. If that enqueue fails (broker down), the error is now logged and swallowed and the edit returns 200 with the old search document. Under autocommit, the error used to reach the caller. That's the stale-document source the PR body names, now on the most common edit path. The new hit check covers access, but a renamed repo just won't be findable by its new name until something reindexes it. Either note in the docstring that this is accepted, or surface a failed enqueue after the block.
| # no key update, so a concurrent child write's foreign-key check on the row isn't blocked | ||
| original_repo = (stored_rows.select_for_update(no_key=True) if keep_private else stored_rows).first() | ||
| if keep_private and not original_repo.public_can_view: | ||
| original_repo = (stored_rows.select_for_update(no_key=True) if lock else stored_rows).first() |
There was a problem hiding this comment.
Pre-existing (not from this change, not blocking): .first() returns None if the row was hard-deleted between the view's lookup and this read, and original_repo.public_access / .released below then raises AttributeError, which is a 500. The lock doesn't change that. A None check returning an error (or raising 404) would.
| viewable = viewable.filter(parent_id__in=parents.values('id')) | ||
| if user.is_authenticated and not user.is_staff and ( | ||
| self.is_concept_container_document_model() or self.is_source_child_document_model()): | ||
| viewable = hits.filter(models.Q(id__in=viewable.values('id')) | models.Q(created_by_id=user.id)) |
There was a problem hiding this comment.
Nit (optional): the creator branch keeps any hit the requester created, so it also skips the is_active=True part of get_viewable_versions_criteria and the parent-source check. A deleted source whose document hasn't been reindexed yet (or one of its concepts, whose is_active the save signal turns off) is still a hit for its creator, while everyone else loses it. Requiring the row to be active on that branch closes it. It's worth checking that no search here opts into inactive hits first:
| viewable = hits.filter(models.Q(id__in=viewable.values('id')) | models.Q(created_by_id=user.id)) | |
| viewable = hits.filter( | |
| models.Q(id__in=viewable.values('id')) | models.Q(created_by_id=user.id, is_active=True)) |
| return cls.objects.filter(public_access=ACCESS_TYPE_VIEW) | ||
|
|
||
| @classmethod | ||
| def get_viewable(cls, queryset, user): |
There was a problem hiding this comment.
Nit (optional): get_viewable and get_visible_to (just below) are now two org visibility checks that disagree. This one counts the retired Edit as public, while get_visible_to (through get_public) counts only View. So an org stored as Edit is a search hit but missing from the database-backed org lists. Having get_visible_to delegate to get_viewable, or having get_public use PUBLIC_ACCESS_VALUES, would leave one rule. Fine as a follow-up.
| self.assertNotIn(self.source.mnemonic, self.search_ids(url, self.outsider)) | ||
|
|
||
| def test_match_drops_a_concept_made_private_without_a_reindex(self): | ||
| self.outsider.groups.add(Group.objects.get(name=PREVIEW_GROUP_NAME)) |
There was a problem hiding this comment.
Nit (optional): this grants $match access by adding the outsider to a named access group. That copies existing debt tracked in OpenConceptLab/ocl_online#532 into a new file. If there's no neutral way yet to give a test user Mapper access, a shared helper in core/common/tests.py would at least keep the name in one place. Otherwise, note this use on that ticket.
| def made_private_first(model, method, access=ACCESS_TYPE_NONE): | ||
| """ | ||
| `model`'s `method`, run after a concurrent write that makes its row (the first argument) private commits, or | ||
| gives it `access`, with its concepts' and mappings' (as the save signal does). |
There was a problem hiding this comment.
Nit (optional): this reads oddly ("with its concepts' and mappings'").
| gives it `access`, with its concepts' and mappings' (as the save signal does). | |
| gives it `access` and, for a source, its concepts and mappings too (as the save signal does). |
| meanwhile stays private, with its concepts and mappings, and a row made public stays public without the edit | ||
| being asked make_private. |
There was a problem hiding this comment.
Nit (optional): "being asked make_private" is hard to parse.
| meanwhile stays private, with its concepts and mappings, and a row made public stays public without the edit | |
| being asked make_private. | |
| meanwhile stays private, with its concepts and mappings, and a row made public stays public, without the edit | |
| going through the make_private check. |
Codex review, pass 2 (codex-cli 0.169.0, commit 858313d)Verdict: 4 findings: 3 P1, 1 P2. Earlier: F1.2 resolved; F1.1 marked regressed (F2.1 below); F1.3 unresolved (accepted in pass 1).
Replies (45fba55; stale-search and make-private tests pass, 32;
findings.json{
"reviewer": "codex-cli 0.169.0",
"perspective": "defects",
"mode": "verify",
"commit": "858313d1",
"verdict": "findings",
"prior": [
{"id": "F1.1", "status": "regressed"},
{"id": "F1.2", "status": "resolved"},
{"id": "F1.3", "status": "unresolved"}
],
"findings": [
{
"id": "F2.1",
"severity": "P1",
"tag": null,
"file": "core/common/views.py",
"line": 1352,
"claim": "Searching a concept’s collection versions raises a FieldError.",
"scenario": "An anonymous reader adds `?q=...` to a public concept’s `collection-versions/` URL. The view searches CollectionDocument but inherits ConceptFacetedSearch, so the new source check tries to select `parent_id` from the Collection queryset. Collection exposes `parent_id` as a Python property and Django cannot use it in `values()`.",
"action": "Choose the source-parent check from the hit queryset’s model. Apply repository visibility to Collection hits and add a test for a searched collection-version listing.",
"lines_delta": null,
"confidence": "high"
},
{
"id": "F2.2",
"severity": "P1",
"tag": null,
"file": "core/common/views.py",
"line": 1061,
"claim": "A former member can still find a private organization through their own organization search.",
"scenario": "A user is removed from a private organization while its search document still lists them as a member. `/user/orgs/?q=...` uses that indexed membership and skips the database check in the requester’s own scope. The search returns the private organization even though the plain list excludes it.",
"action": "Intersect organization search hits with current database membership and visibility for the user named by the URL. Add a test that removes membership without refreshing the index.",
"lines_delta": null,
"confidence": "high"
},
{
"id": "F2.3",
"severity": "P1",
"tag": null,
"file": "core/graphql/queries.py",
"line": 522,
"claim": "Global GraphQL concept search still returns hits from sources already marked private.",
"scenario": "A source’s HEAD is made private while its concept rows and search documents still say public. An authenticated outsider runs a global GraphQL text search. The database queryset checks the concept’s copied access and returns the hit while the source itself denies access.",
"action": "Apply the live source visibility check to the global GraphQL queryset in bulk and add a test that changes only the source’s access.",
"lines_delta": null,
"confidence": "high"
},
{
"id": "F2.4",
"severity": "P2",
"tag": null,
"file": "core/concepts/views.py",
"line": 1152,
"claim": "$match can return hits from a target release that becomes private during the search.",
"scenario": "An outsider starts a match against a public release. After target resolution and before hit loading, the release is made private while its HEAD stays public. The matching concept rows still point to HEAD, so the database check passes and the response returns hits from the now-private release.",
"action": "Carry the resolved target release IDs into hit loading and recheck their visibility in bulk. Test a release access change between target resolution and hit loading.",
"lines_delta": null,
"confidence": "high"
}
]
} |
|
@snyaggarwal thanks for the approval. Please hold the merge for now: the third Codex pass on 45fba55 found two more cases in the search check (a source or collection CSV export whose only hit is dropped falls back to the plain list, and facets still name a source whose hits were dropped). I'm fixing both and will post exactly what changed since your approval so you can re-check. |
Codex review, pass 3 (codex-cli 0.169.0, commit 45fba55)Verdict: 3 findings: 2 P1, 1 P2. Earlier: F1.1, F1.2, F2.1, F2.2 resolved; F1.3, F2.3, F2.4 unresolved (accepted in passes 1 and 2).
This PR merged before this pass came back, so the fixes are in the follow-up, #1050 (@snyaggarwal: that replaces my hold note's ask; please review #1050 instead). Replies:
findings.json{
"reviewer": "codex-cli 0.169.0",
"perspective": "defects",
"mode": "verify",
"commit": "45fba556",
"verdict": "findings",
"prior": [
{
"id": "F1.1",
"status": "resolved"
},
{
"id": "F1.2",
"status": "resolved"
},
{
"id": "F1.3",
"status": "unresolved"
},
{
"id": "F2.1",
"status": "resolved"
},
{
"id": "F2.2",
"status": "resolved"
},
{
"id": "F2.3",
"status": "unresolved"
},
{
"id": "F2.4",
"status": "unresolved"
}
],
"findings": [
{
"id": "F3.1",
"severity": "P1",
"tag": null,
"file": "core/common/views.py",
"line": 1317,
"claim": "A source or collection CSV search can export a release the database visibility check dropped.",
"scenario": "An anonymous CSV search with allVersions=true and version=v1 finds a single stale hit for a release whose row still says View after its HEAD was made private. The database check drops the hit. The CSV row builder treats the empty queryset as missing and falls back to the plain list, which checks the release's own access and exports it.",
"action": "In the source and collection CSV row builders, fall back only when the queryset is None. Add a regression test that checks the contents of an export after its only hit is dropped.",
"lines_delta": null,
"confidence": "high"
},
{
"id": "F3.2",
"severity": "P1",
"tag": null,
"file": "core/common/views.py",
"line": 1062,
"claim": "Search facets still disclose the names of private sources whose hits were dropped.",
"scenario": "When a source is made private before its concept documents are reindexed, an anonymous global concept search with facets drops those concepts from the results. The facets still use the stale Elasticsearch documents and return the source's mnemonic and owner.",
"action": "Apply the live access check to named source and owner facets, and add a stale-index test for the facet response. Keep num_found based on the index as documented.",
"lines_delta": null,
"confidence": "high"
},
{
"id": "F3.3",
"severity": "P2",
"tag": null,
"file": "core/common/models.py",
"line": 1423,
"claim": "A REST edit that omits public_access now suppresses a search-index enqueue failure that previously reached the caller.",
"scenario": "A name-only PATCH commits while the indexing broker is unavailable. The new transaction makes CelerySignalProcessor register the enqueue with robust=True, so the request can return 200 while the search document keeps the old name. The same save previously surfaced the enqueue error under autocommit.",
"action": "Keep the row lock and preserve enqueue-error reporting for REST edits that previously ran under autocommit. Cover the name-only PATCH with a transaction test that makes the enqueue fail.",
"lines_delta": null,
"confidence": "high"
}
]
} |
For OpenConceptLab/ocl_online#730 (items 1 and 2; item 3 isn't code). References it without closing it.
1. A REST edit that sends no access keeps its row's current access
A PUT or PATCH of a source or collection, HEAD or version, that sends no
public_accesssaved the access it read at the start of the request. If a concurrent write made the row private in between, the edit made it viewable again, and a source passed that on to its concepts and mappings.Now such an edit calls
persist_changes(..., keep_access=True). It reads the row under theselect_for_update(no_key=True)lock that #1009 added for FHIR writes, in the transaction that saves it, and keeps that row's access, whatever it is. So a concurrent publish isn't undone either, and the edit isn't askedmake_privatefor a change it didn't make. An edit that sendspublic_accessworks as before. FHIR updates keepkeep_private=True, since they may copy a private release's access onto HEAD.2. Search hits load through the database's access check
Search loaded its hits from the database by id, trusting the index's
public_can_view. A document can be stale: a large source made private keeps public concept and mapping documents until its reindex task finishes, and a failed index enqueue inside a transaction leaves the old document. Such a hit was returned to anyone.Now, wherever a search filters its hits by access, the hits load through the same access check the plain lists use, read from the database:
get_viewable_versions_criteria();collection-versions/search, whose hits are collections, gets the collection check):get_viewable()(apply_user_criteria), which moves fromConcepttoSourceChildMixin, and their source must be viewable too. A source made private commits before its save signal makes its concepts and mappings private, so for a while their rows still say public;Organization.get_viewable(), public (View, or the retired Edit, as the index reads it) or the requester's own, in every scope, since an org search in the requester's own scope reads the index's member list;$match: the same as concepts, when it appliesget_visible_repo_criteria().Like the search's own filter, the check keeps the repositories, concepts and mappings the requester created. Where a search applies no access filter (staff, the requester's own user or org, a collection's members in the requester's scope), nothing changes: the URL's permission check governs, as in the lists.
Cost: one
.filter()on the page's existingpk__inquery, so no query per hit. The repo search, which already loads its hits one by one, gets one check per model (sources, collections).Counts:
num_foundstays the index's count, so until the reindex it can count a hit the page drops;num_returnedis what the page holds, which can be fewer thanlimit. Facet counts come from the index too. ACompressresponse'snum_foundis the length of the list it returns, as before.Tests
UpdateToPrivateTest: a PUT or PATCH of a HEAD or version keeps the access a concurrent write gave its row, in both directions, with a source's concepts, and isn't askedmake_private; and it reads the rowFOR NO KEY UPDATEinside the transaction that saves it, only when it sends no access.tests_stale_search_access.py(es): rows made private in the database without a reindex are no hit for the public or an outsider in the org, source, collection, repo, concept and mapping searches, another user's org list, or$match; members, staff and creators still find them.collection-versions/list answers.TEST_ES=off, parallel) and 706 (es), OK.pylint -j0 core/(4.0.9): clean.Reviewer: @snyaggarwal.
🤖 Generated with Claude Code
https://claude.ai/code/session_01AaZ4qTLYUUSTSy1TKzRmkd