Skip to content

Repair multiline keyring secrets without data loss - #16626

Open
sattary wants to merge 5 commits into
omacom:quattrofrom
sattary:feature/keyring-ast-repair
Open

sattary wants to merge 5 commits into
omacom:quattrofrom
sattary:feature/keyring-ast-repair

Conversation

@sattary

@sattary sattary commented Oct 25, 2026 •

Copy link
Copy Markdown

Fixes #5105, #11159, #12563. Supersedes #11697.

Summary

GNOME Keyring drops cleartext keyrings on boot when clients store unescaped multiline secrets (e.g. Proton VPN SSO, OAuth tokens). PR #11697 proposed deleting the file; this PR repairs it in-place instead:

keyring-repair:
  1. Scan for bare continuation lines; if none found, leave file untouched byte-for-byte
  2. Recognize valid keyring properties (lock-on-idle, lock-after) to prevent false failures
  3. Split file strictly at LF boundaries to preserve Unicode separators (U+2028)
  4. Escape leading spaces as \s per GLib GKeyFile specification
  5. Validate section hierarchy ([keyring] first, sequential item/attribute IDs, unique headers)
  6. Reject ambiguous bracket lines inside damaged items
  7. Reverse-partition tail metadata (ctime, mtime) to prevent payload truncation
  8. Preserve file line endings (CRLF vs LF) and fold unescaped secrets into GLib escapes
  9. Atomic commit with mode 0600, fsync, and os.replace
  • Adds bin/omarchy-keyring-repair running on daemon startup via gnome-keyring-daemon.service.d/10-keyring-repair.conf.
  • Migration migrations/1792900000.sh masks daemon with --runtime (in /run), stops daemon, runs repair, halts with code 1 if helper is unavailable, and restarts.

Evidence

test/shell.d/keyring-repair-test.sh:

ok - valid cleartext keyring is unmodified
ok - multiline secrets folded with zero data loss while preserving valid items
ok - recovered secret bytes match via GLib GKeyFile parser
ok - recovered secret bytes match via live gnome-keyring-daemon and secret-tool
ok - ambiguous corruption halts safely without replacing file
ok - ambiguous section boundaries rejected without shortening secrets
ok - encrypted keyrings remain untouched
ok - missing keyring directory handled cleanly
ok - failed repair returns nonzero exit code
ok - CRLF line endings and payload fidelity verified via GLib
ok - embedded metadata patterns in secret payload preserved without false truncation
ok - ambiguous bracket boundary halts safely without modifying file
ok - valid provisioned keyring with lock-on-idle is untouched
ok - unicode line separators survive repair without gaining extra newlines
ok - leading secret spaces survive repair without disappearing

Merge Danger

Door: two-way (unmodified files left byte-for-byte identical; binary keyrings skipped; ambiguous boundaries reject without file modifications).
Blast Radius: bounded (~/.local/share/keyrings/*.keyring).

@greptile-apps

greptile-apps Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5 · The migration should not merge until a failed mask stops repair.RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The PR reaches cleartext keyring files and the user-session keyring startup path.

The migration should not merge until a failed mask stops repair.

Findings

  1. P1 Failed masks allow credential loss ▶

Summary

Adds a helper that folds damaged multiline keyring secrets into escaped values and replaces repaired files atomically.

  • Cleartext keyrings keep recoverable multiline secrets as readable values.
  • The keyring repair runs during migration and before daemon starts.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Migration] --> B[Mask daemon and socket]
  B -->|Success or failure| C[Stop daemon and socket]
  C -->|Stop succeeds| D[Read and repair keyrings]
  D --> E[Replace repaired files]
  E --> F[Unmask and restore active units]
  C -->|Stop fails| F
Loading

Reviews (18) · Last reviewed commit: "Wrap keyring repair command in bash entr..." · Reviewed by Greptile

Comment thread bin/omarchy-keyring-repair Resolved
Comment thread bin/omarchy-keyring-repair Resolved
Comment thread bin/omarchy-keyring-repair Resolved
Comment thread bin/omarchy-keyring-repair Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from c868200 to 9093474 Compare October 25, 2026 15:51
Comment thread bin/omarchy-keyring-repair Resolved
Comment thread bin/omarchy-keyring-repair Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from 9093474 to 194a121 Compare October 25, 2026 16:00
Comment thread bin/omarchy-keyring-repair Resolved
Comment thread test/shell.d/keyring-repair-test.sh Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch 2 times, most recently from 5afa272 to b87ef21 Compare October 29, 2026 15:19
Comment thread bin/omarchy-keyring-repair Resolved
Comment thread bin/omarchy-keyring-repair Resolved
@greptile-apps

greptile-apps Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

@sattary
sattary force-pushed the feature/keyring-ast-repair branch from b87ef21 to b185944 Compare October 29, 2026 15:26
Comment thread bin/omarchy-keyring-repair Resolved
Comment thread migrations/1792900000.sh Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from b185944 to d86072a Compare October 29, 2026 15:36
Comment thread test/shell.d/keyring-repair-test.sh Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from d86072a to d998a43 Compare October 29, 2026 15:42
Comment thread migrations/1792900000.sh Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from d998a43 to 749a021 Compare October 29, 2026 15:47
@DipanshuAmbilkar03

Copy link
Copy Markdown

default/systemd/user/gnome-keyring-daemon.service.d/10-keyring-repair.conf:2: ExecStartPre=/usr/bin/omarchy-keyring-repair fails the unit whenever the helper exits non-zero, and the helper deliberately returns 1 for any unrecoverable/ambiguous keyring (and on read/commit errors). Once the migration in this PR has failed on such a keyring, every later gnome-keyring-daemon start fails too, so the user loses the secret service / SSH agent entirely — a bigger outage than the corrupt keyring, and the only way back is finding and removing this drop-in.

The helper already reports to stderr, so ExecStartPre=-/usr/bin/omarchy-keyring-repair would let the daemon start with the unrepairable keyring while the migration/notification carries the hard failure.

@sattary
sattary force-pushed the feature/keyring-ast-repair branch from 749a021 to 9600ffc Compare November 1, 2026 12:03
Comment thread test/shell.d/keyring-repair-test.sh Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from 9600ffc to 899ad69 Compare November 1, 2026 12:13
Comment thread migrations/1792900000.sh Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from 899ad69 to afe7197 Compare November 1, 2026 12:57
Comment thread test/shell.d/keyring-repair-test.sh Resolved
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from afe7197 to 6d59055 Compare November 1, 2026 13:17
@omarchybot omarchybot added bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review labels Nov 2, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at 4786d4f against quattro by Claude Opus 5.5, with a second opinion from Codex Medium and muse-wonder (pi, thinking high). The reviewers can read this session's notes, so where they agree with me that agreement is not independent. The two defects fixed below were raised by the reviewers and checked on a worker before changing anything.

The repair works. On a worker built from the Omarchy ISO (gnome-keyring 1:51.1-1), I started from the keyring install/user/default-keyring.sh writes and stored a single-line gh-style token next to a multi-line JSON secret with a PEM block. After a daemon restart without the repair, both secrets were gone. With omarchy-keyring-repair run before the restart, both came back byte-for-byte through secret-tool. That includes backslashes, a tab, leading and trailing spaces, a trailing newline and an embedded ctime=123 line. The migration did the same end to end, left no runtime masks behind and restarted the socket and service. A second run left the file byte-identical. With the drop-in installed by hand, a keyring corrupted again was repaired on the next daemon start. With an ambiguous file, the - prefix still let the daemon start and the file was left untouched. ./test/cli and keyring-repair-test.sh pass on the worker. Escaping is the right fix because gnome-keyring reads secret back with g_key_file_get_string (gkm-secret-textual.c:353).

Pushed 4786d4f to fix two defects, each with a test that fails without its fix:

  • lock-timeout= was missing from KEYFILE_PROPERTY_RE. gnome-keyring writes it into [keyring] whenever a lock timeout is set (gkm-secret-textual.c:418,422), so such a keyring was refused outright. Because the migration exits 1 on a refusal, it also stayed pending.
  • A secret with a line such as [9] followed by item keys was split into a second, forged item, and the real secret was cut short, with exit 0. I reproduced this through the daemon. gnome-keyring writes an item's attribute groups straight after the item, so an attribute group that follows a different item now marks the file as ambiguous.

Still open, for the maintainer:

  • The drop-in never reaches an installed system. omarchy-settings installs user units one file at a time (pkgbuilds/omarchy-settings/PKGBUILD in omarchy-pkgs), and nothing installs gnome-keyring-daemon.service.d/10-keyring-repair.conf into /usr/lib/systemd/user/. Until it does, this PR repairs once, at migration time. The next multi-line secret breaks the keyring again, because nothing here fixes the writer.
  • An unrepairable keyring blocks every later migration. A refusal makes the migration exit 1, and omarchy-migrate stops the queue there. That matches agents/skills/migrations.md. But a file with legacy [N:aclM] groups, or any ambiguous file, won't become repairable on a rerun, so for that user the queue stays stuck.
  • Size. Both reviewers judged a 437-line parser large for this problem. The writer fix in gnome-keyring: escape multiline secrets in textual keyrings omarchy-pkgs#248 prevents new damage. This PR is the only open change that recovers keyrings that are already broken without losing their secrets. Repair a broken passwordless default keyring before session apps use it #11697 moves the whole file aside, so healthy secrets disappear from the Secret Service too. For that reason I'd take this one over Repair a broken passwordless default keyring before session apps use it #11697, the older of the two.
  • Minor gaps. A continuation line that happens to look like a valid key (name=…) is not detected, and that secret stays truncated, as upstream left it. If an interrupted migration is rerun, it treats its own runtime masks as the user's, so the units stay masked until reboot. Users who already accepted the "new keyring" prompt keep default pointing at the new keyring.

Dismissed after checking: a claim that trailing spaces are lost (the daemon round-trip kept them), and a suggestion that the drop-in should fail the unit (failing open is right; the daemon must start).

This now waits on the maintainer: whether to take an in-place repair alongside or instead of the writer fix, and how the drop-in gets packaged.

Comment thread bin/omarchy-keyring-repair Resolved
@omarchybot omarchybot removed the verified Omarchy Triage has verified that this issue is ready for final review label Nov 3, 2026
@lbssousa

Copy link
Copy Markdown

#17502 (Replace GNOME Keyring with oo7) now depends on this PR. Its migration runs omarchy-keyring-repair before the package swap, because a multi-line secret written by gnome-keyring (#5105) stops oo7-daemon from starting at all. If that command is missing, the migration restores gnome-keyring and stays pending. So #17502 can't merge before this one.

Everything else #17502 needs is now on edge. oo7 0.7.1-1 is in edge's extra on x86_64 and aarch64, and #17502 and omacom/omarchy-pkgs#2696 were rebased on today's quattro and master without conflicts. This PR is the last thing it waits on.

@omarchybot

Copy link
Copy Markdown
Collaborator

Re-read head 6b7c81ea9e8a05c7e1dad2dddf4fefd9e5fe5fc4, including the new section-specific property checks, the migration and startup hook, the previous review, and the linked issue reports. GPT-6 in Codex (effort not exposed) classified this as a bug fix because it recovers secrets damaged by GNOME Keyring's existing writer. The configured classification reviewer, deepseek-v4p1-flash (effort not reported), classified it as an enhancement. The classifier supplies only a class, so it gave no reason for the difference.

Classification is therefore unresolved. The queue workflow stops verification at this point: the second model has only classified the change, and tests have not run on this head in this pass. The earlier revision's verification does not cover this head. This waits for the classification disagreement to be resolved before review and verification continue.

Comment thread bin/omarchy-keyring-repair Resolved
@omarchybot

Copy link
Copy Markdown
Collaborator

Re-read head 8394642fd02c266a0d4f59f13ab8468d376c80bc, including the parser refactor, additional rejection tests, migration cleanup changes, PR discussion and linked issue reports. GPT-6 in Codex (effort not exposed) classified this as a bug fix because it repairs secrets damaged by GNOME Keyring's existing multiline writer. The classification reviewer, deepseek-v4p1-flash (effort not reported), returned enhancement; the classifier returns no explanation for that choice.

The classification remains unresolved at this head. The queue workflow requires this pass to stop here, so no class label was added and no tests or code-review second opinion ran on this revision. Earlier verification covers an older head. This waits for the classification disagreement to be resolved before verification continues.

sattary and others added 5 commits November 28, 2026 20:53
GNOME Keyring drops cleartext keyrings on startup when Secret Service
clients store unescaped multiline values (omacom#5105, omacom#11159, omacom#12563).

Supersede PR omacom#11697 by parsing and folding unescaped continuation lines
into GLib keyfile escaped sequences in-place rather than deleting
valid credentials. Provide an atomic Python stdlib repair CLI, a
systemd user ExecStartPre drop-in, an isolated runtime-masked
migration, and round-trip verification tests against GLib and live
gnome-keyring-daemon.
gnome-keyring writes lock-timeout= into [keyring] whenever a lock timeout is set (gkm-secret-textual.c:418,422), and the whitelist counted it as a bare continuation line, so any such keyring was refused even when its damage was a plain multiline secret.

A secret whose payload holds a line like [9] followed by item keys was split into a second, forged item and the real secret cut short, with exit 0. gnome-keyring writes an item's attribute groups straight after it (generate_item calls generate_attributes), so an attribute group that follows a different item now marks the file ambiguous and leaves it untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Codex Medium <noreply@openai.com>
Do not match keyring-level properties (lock-timeout, lock-on-idle)
inside item sections so damaged secrets containing these strings fold
and recover properly rather than being skipped.
Reject empty bracket lines inside secrets, remove dead keyfile regexes,
simplify section parsing, guard offline systemd user buses and stale
runtime masks in the migration, update the migration timestamp, and
add regression tests for empty brackets, legacy ACLs, and foreign files.
Satisfy the AGENTS.md #!/bin/bash shebang requirement by executing /usr/bin/python3 -I via heredoc from bin/omarchy-keyring-repair while keeping command metadata intact, and invoke the command directly in test/shell.d/keyring-repair-test.sh.
@sattary
sattary force-pushed the feature/keyring-ast-repair branch from 8394642 to 91ff3e3 Compare November 28, 2026 17:25
@omarchybot

Copy link
Copy Markdown
Collaborator

I reviewed the Bash entry point and its test updates at 91ff3e397b19f5a1a0581ea5bf07df4a8a3b1921 against the linked keyring reports. GPT-6 High in Codex classified this as a bug fix because it recovers existing keyrings that GNOME Keyring can no longer read. The classification reviewer, deepseek-v4p1-flash (effort unreported), returned enhancement.

Verification is paused until the classification is resolved. The earlier worker results cover an older revision, and no worker tests ran on this commit.

Comment thread migrations/1795886619.sh
Comment on lines +27 to +29
if systemctl --user mask --runtime "$unit" >/dev/null 2>&1; then
masked_units+=("$unit")
fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Failed masks allow credential loss

If systemctl --user mask --runtime fails, the migration still stops the units and runs repair. Stopping the daemon does not prevent an app from starting it again. The helper can then replace a keyring that the daemon has just updated, losing newly saved credentials.

Require both units to be masked successfully, or already persistently masked, before running repair.

@lbssousa

lbssousa commented Dec 3, 2026

Copy link
Copy Markdown

The open Greptile finding (a failed mask doesn't stop the repair) holds at 91ff3e39. With a stubbed systemctl that fails mask --runtime for either unit, the migration still stops both, runs omarchy-keyring-repair and exits 0.

The patch below makes both units a precondition. The cleanup trap moves above the mask loop, so a service already masked is unmasked when masking the socket fails.

diff --git i/migrations/1795886619.sh w/migrations/1795886619.sh
index 179fcb981..22f06e041 100644
--- i/migrations/1795886619.sh
+++ w/migrations/1795886619.sh
@@ -18,17 +18,7 @@ if systemctl --user show-environment >/dev/null 2>&1; then
     socket_was_active=1
   fi
 
-  # Track units not persistently masked so an interrupted run's masked-runtime
-  # state still gets cleaned up on retry.
   masked_units=()
-  for unit in "$service" "$socket"; do
-    unit_state=$(systemctl --user is-enabled "$unit" 2>/dev/null || true)
-    if [[ $unit_state != "masked" ]]; then
-      if systemctl --user mask --runtime "$unit" >/dev/null 2>&1; then
-        masked_units+=("$unit")
-      fi
-    fi
-  done
 
   cleanup() {
     for unit in "${masked_units[@]}"; do
@@ -49,6 +39,21 @@ if systemctl --user show-environment >/dev/null 2>&1; then
   trap 'exit 1' INT TERM
   trap cleanup EXIT
 
+  # Stopping alone doesn't keep an app from activating the daemon again, and the
+  # repair would then overwrite what it saves, so both units must be masked.
+  # Track units not persistently masked so an interrupted run's masked-runtime
+  # state still gets cleaned up on retry.
+  for unit in "$service" "$socket"; do
+    unit_state=$(systemctl --user is-enabled "$unit" 2>/dev/null || true)
+    if [[ $unit_state != "masked" ]]; then
+      if ! systemctl --user mask --runtime "$unit" >/dev/null 2>&1; then
+        echo "Failed to mask $unit before keyring repair" >&2
+        exit 1
+      fi
+      masked_units+=("$unit")
+    fi
+  done
+
   if ! systemctl --user stop "$service" "$socket"; then
     echo "Failed to stop $service or $socket before keyring repair" >&2
     exit 1

Checked with the same stub: either mask failing now exits 1 without running the repair and undoes any mask it made; with both masks succeeding, or both units already masked, the repair runs as before. test/shell.d/keyring-repair-test.sh passes. I didn't add a test, since agents/skills/migrations.md asks for none on migrations. Not run against a real user manager or in a VM, and shellcheck wasn't run.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants