Skip to content

[PAM-466] (B10) Vault: keep restricted ciphers out of upsert and URL matching - #24281

Merged
abergs merged 1 commit into
mainfrom
pam/vault-restricted-cipher-guards
Oct 30, 2026
Merged

abergs merged 1 commit into
mainfrom
pam/vault-restricted-cipher-guards

Conversation

@abergs

@abergs abergs commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

🎟️ Tracking

PAM-466 (Vault review: PAM-295), bundle B10 of the clients pam/uat -> main extraction, tracked under PAM-173.

📔 Objective

Brings over the last CipherService changes on pam/uat. All three files are Vault-owned, and there are no new strings. A restricted cipher is a PAM-gated item that the client stores without its secret fields (partialData set). The two guards keep that stored copy restricted.

  • upsert keeps a stored restricted copy. If the stored copy of an item is restricted and the incoming copy is full, upsert keeps the stored copy and logs a debug count. Full sync (replace) stays authoritative, so the next full sync still writes what the server sends. Push sync is the only caller on main that reaches this guard: archive, unarchive and folder delete already pass restricted copies through.
  • upsert skips the write when it keeps every item. updateEncryptedCipherState always clears the decrypted cache before it writes. In the extension, an unchanged write fires no storage change, so nothing rebuilt the cache and autofill found no items. upsert now returns early in that case, the same way replace skips identical writes. This part is new in this PR. It was also pushed to pam/uat.
  • filterCiphersForUrl excludes restricted ciphers. Its callers already read streams without restricted ciphers, so this is a second guard for autofill. A view with partial false still matches.
  • Comments: doc comments on ciphers$, filterCiphersForUrl and upsert. The cipherListViewsIncludingRestricted$ comment now says it serves every vault list, not only the web one.
  • Tests: cipher.service.spec covers both guards: kept, replaced and mixed upserts, the skipped write, and a restricted login with a matching URI. Some existing describe and test titles now say "restricted" instead of "partial".

Known trade-off: during an active lease, a push update for a restricted item keeps the stored copy. So an admin rename, move or soft delete of that item shows only after the next full sync. The same applies when an item stops being gated, because the server sends no push for a removed rule.

@abergs abergs added ai-review Request a Claude code review t:feature Change Type - Feature Development labels Oct 29, 2026
Comment on lines +1099 to +1105
// When every item is kept, skip the write: an unchanged record clears the cache but emits
// nothing in the extension, the case replace() also guards against.
userId ||= await firstValueFrom(this.stateProvider.activeUserId$);
const stored = (await firstValueFrom(this.encryptedCiphersState(userId).state$)) ?? {};
if (ciphers.length > 0 && ciphers.every((c) => keepsStored(stored[c.id as CipherId], c))) {
logKept(ciphers.length);
return stored;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed from pam/uat in this PR (also pushed to pam/uat as d2aa1dd)

Problem: updateEncryptedCipherState always clears the decrypted cache before it writes. When the guard below keeps every incoming item, upsert wrote a record identical to the stored one. In the extension, state$ emits only on chrome.storage.onChanged, and Chrome fires no event for an unchanged value. So nothing rebuilt the cache. Autofill, the inline menu and the context menu then found no items for any site until the next real change or full sync.

When it happens:

  1. An extension user has an active lease on a restricted item.
  2. An admin edits the item, or the access connector rotates it, and the server sends a push update.
  3. Push sync fetches the item, gets the full copy, and calls upsert.
  4. The guard keeps the stored restricted copy, and the write changes nothing.

Fix: read the stored copies first. If every incoming item would be kept, log the count and return without clearing the cache or writing. replace() already skips identical writes for the same reason. A mixed call (some items kept, some written) still writes, so the cache rebuilds as before.

Test: "skips the write and keeps the cache when every item is kept" checks that neither clearCache nor the state write runs. It fails without the fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a cleaner way to do this, or if we want to drop the console debug calls.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a tech debt ticket for Vault to clean these up here: https://bitwarden.atlassian.net/browse/PM-46064

We should be fine to merge as is for now though.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Bitwarden Claude Code Review

Overall Assessment: APPROVE

This PR adds two guards to CipherService. upsert now keeps a stored restricted cipher when the incoming copy is full, and returns early without calling clearCache or writing to state when it keeps every item. filterCiphersForUrl now drops restricted ciphers through CipherViewLikeUtils.isRestricted. I checked the non-spec upsert callers (push sync in core-sync.service.ts, folder delete in folder.service.ts, archive and unarchive in default-cipher-archive.service.ts) and the new spec cases for kept, replaced, mixed and skipped writes, and found nothing that needs changing.

@abergs
abergs marked this pull request as ready for review October 29, 2026 20:14
@abergs
abergs requested a review from a team as a code owner October 29, 2026 20:14
@abergs
abergs requested review from jaasen-livefront and shane-melton and removed request for jaasen-livefront October 29, 2026 20:14

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shane-melton Let me know if you don't like these comments

@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.06%. Comparing base (26e0f46) to head (ca5762f).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
libs/common/src/vault/services/cipher.service.ts 90.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24281      +/-   ##
==========================================
+ Coverage   61.05%   61.06%   +0.01%     
==========================================
  Files        4872     4872              
  Lines      156182   156201      +19     
  Branches    25000    25006       +6     
==========================================
+ Hits        95352    95382      +30     
+ Misses      54297    54279      -18     
- Partials     6533     6540       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +1099 to +1105
// When every item is kept, skip the write: an unchanged record clears the cache but emits
// nothing in the extension, the case replace() also guards against.
userId ||= await firstValueFrom(this.stateProvider.activeUserId$);
const stored = (await firstValueFrom(this.encryptedCiphersState(userId).state$)) ?? {};
if (ciphers.length > 0 && ciphers.every((c) => keepsStored(stored[c.id as CipherId], c))) {
logKept(ciphers.length);
return stored;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a tech debt ticket for Vault to clean these up here: https://bitwarden.atlassian.net/browse/PM-46064

We should be fine to merge as is for now though.

@abergs
abergs merged commit fd33a80 into main Oct 30, 2026
219 of 224 checks passed
@abergs
abergs deleted the pam/vault-restricted-cipher-guards branch October 30, 2026 16:35
abergs added a commit that referenced this pull request Oct 30, 2026
Take the syncUpsertCipher tests in default-sync.service.spec.ts from
pam/uat (PAM-318). Core sync hands every fetched cipher to
CipherService.upsert, and B10 (#24281) keeps a stored restricted copy
there. Keep main's LogoutReason import from ../../auth/logout
(PM-46016, #24275).
abergs added a commit that referenced this pull request Nov 2, 2026
…#24313)

* [PAM-472] (B11) Platform: sync push tests for restricted local copies

Take the syncUpsertCipher tests in default-sync.service.spec.ts from
pam/uat (PAM-318). Core sync hands every fetched cipher to
CipherService.upsert, and B10 (#24281) keeps a stored restricted copy
there. Keep main's LogoutReason import from ../../auth/logout
(PM-46016, #24275).

* [PAM-472] Say restricted, not partial, in the sync push test titles

The block's first test says "restricted local copy" for the state that
the next three call "partial". Use the one word that Vault's
RestrictedCipherPolicy uses on main.

* [PAM-472] Name the no-local-copy case in plain words

"Lease-widened read" is a PAM-only term. Say what the client cannot
tell apart: a cipher shared with the user, or one a lease made
readable.

* [PAM-472] Give the logged-out sync push test a local copy to fetch
@djsmith85 djsmith85 added browser Browser Extension desktop Desktop Application cli CLI Application web labels Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Request a Claude code review browser Browser Extension cli CLI Application desktop Desktop Application t:feature Change Type - Feature Development web

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants