Skip to content

Fix 2-day timestamp offset and preserve unknown XML elements - #9

Open
guilherme-n-l wants to merge 1 commit into
shadone:developfrom
guilherme-n-l:fix/epoch-and-unknown-xml
Open

guilherme-n-l wants to merge 1 commit into
shadone:developfrom
guilherme-n-l:fix/epoch-and-unknown-xml

Conversation

@guilherme-n-l

Copy link
Copy Markdown

Summary

This fixes two data-integrity bugs that show up when a vault is shared with other KeePass clients:

  1. Every KDBX 4 timestamp was off by 2 days. Fixes #EPOCH_ISSUE
  2. Unknown XML elements were dropped on save. Fixes #UNKNOWN_XML_ISSUE

Each fix is its own commit. A third, small commit works around a Swift 6.3 compiler crash so the test target builds.

1. Proleptic Gregorian .NET epoch (fix: use the proleptic Gregorian .NET epoch for KDBX 4 dates)

Date.dotNetEpoch was built with DateComponents + Calendar(identifier: .gregorian). Foundation's Gregorian calendar uses the Julian calendar before the 1582 cutover, so the epoch came out as -62_135_769_600 instead of -62_135_596_800, which is 2 days early. KDBXKit's own read and write cancelled out. Interop did not: KeePassXC dates were read 2 days early, our dates showed 2 days in the future in KeePassXC, and a 3.1 → 4.1 migration shifted every timestamp by +2 days.

  • dotNetEpoch is now the literal Date(timeIntervalSince1970: -62_135_596_800), with a doc comment explaining the cutover. It is internal (was private) so tests can check it.
  • New DotNetEpochTests checks the constant and absolute known values in both directions: 1970-01-01, 2000-02-29T12:34:56Z and 2025-06-01.
  • XMLDocumentReader_empty asserted the shifted value. It now expects CurE3w4AAAA= = 2025-05-25T10:38:02Z (1_748_169_482).

Checked end to end against keepassxc-cli 2.7.6. I created a vault and an entry with kdbx at 17:58:30Z, then ran keepassxc-cli export -f csv:

KeePassXC shows Created
develop (e9b8839) 2026-10-06T17:58:30Z (+2 days)
this branch 2026-10-04T17:57:28Z (correct)

2. Preserve unknown XML elements (fix: preserve unknown XML elements across read and write)

The reader logged unrecognized elements to parserWarnings and discarded them. Elements written by other clients, plugins or newer format revisions were lost on the next save.

  • New public KDBX.UnknownElement (Sendable, Equatable). It holds the name, the attributes, and children as .element / .text.
  • unknownElements: [KDBX.UnknownElement] on KDBX (top-level KeePassFile children), Meta, Root, Group and Entry (history entries included). The new init parameters default to [], so existing call sites compile unchanged. Meta.unknownElements doesn't bump settingsChanged.
  • The reader keeps each unrecognized child on its parent and still records a warning, now worded Preserved unknown element <path>.
  • The writer emits them after the known children of the same parent, in their original order, with attributes, text and nested elements unchanged. The eager and streaming writers share XMLDocumentWriter, so both keep them.
  • Unknown elements nested deeper (inside Times, AutoType, CustomData, …) are still dropped with a warning, as before. This keeps the change focused.
  • A Protected="True" value inside an unknown element is written back as the same ciphertext. KeePass, KeePassXC and this reader skip unknown elements without consuming inner-stream keystream, so other protected values are unaffected. The interop test below covers this.
  • docs/spec/kdbx-xml.md (section 1.2, Appendix C) and CLAUDE.md are updated to describe the new behaviour.

Tests:

  • UnknownElementTests covers:
    • parsing at all five levels, including history entries, attributes, escaped text and nested children;
    • the warnings;
    • where the writer places the elements and in what order;
    • XML re-parse equality;
    • a full KDBX save through both KDBXWriter.write and KDBXWriter.streamingWrite, with a protected password after the unknown subtree.
  • KeePassXCInteropTests.ourOutput_withUnknownElements_readableByKeePassXC puts an unknown element carrying a Protected="True" value at every level of kpxc-rich, saves, and checks that keepassxc-cli ls -R and show -s still work and decrypt the Unicode entry's password.

2b. Stable mixed content (fix: keep unknown elements stable across repeated saves)

An unknown element that mixes text and child elements gained the writer's indentation on every save. When reading such an element, its text children are now trimmed and whitespace-only ones dropped, so read → write → read is stable; text-only elements are kept byte for byte. UnknownElementStabilityTests saves and re-reads one three times.

3. Compiler-crash workaround (test: avoid a Swift 6.3 compiler crash in StaticReaderAPITests)

Swift 6.3.3 aborts compiling StaticReaderAPITests.swift (SIL ownership verifier, "Found ownership error?!", signal 6). The trigger is catch let error as KDBXReader.Error around a typed-throws KDBXReader.parse. The cast is redundant, so the commit uses a plain catch. Behaviour is unchanged. Drop this commit if you'd rather not carry it.

Testing

  • Linux, Swift 6.3.3: swift build and swift test. 479 tests in 75 suites passed. Baseline develop with only the crash workaround: 473 tests in 73 suites. This branch adds 2 + 3 + 1 tests, and the DotNetEpochTests value test is parameterized over 3 cases.
  • The KeePassXC interop suite ran against keepassxc-cli 2.7.6: all 15 tests passed, including the new one. To run it I temporarily pointed KeePassXCInteropTests.cliPath at /usr/bin/keepassxc-cli. That change is not committed.
  • swiftformat --lint (0.53.10, the Mintfile version): 0 of the 15 Swift files touched need formatting. On develop, KDFProfileCompatTests.swift already fails lint; this PR doesn't touch it.

Generated by Claude Code

The 3.0 rejection test wraps a typed-throws `KDBXReader.parse` call in
`do { } catch let error as KDBXReader.Error { }`. Swift 6.3.3 aborts
while compiling that pattern (SIL ownership verifier: "Found ownership
error?!", signal 6), so the whole test target fails to build.

Because `parse` throws `KDBXReader.Error`, the bound `error` in a plain
`catch` already has that type, and the cast was redundant. Use `catch`
instead. Behaviour is unchanged, and the test target builds again on
both 6.1 and 6.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HdzqH7rejgXYHCaGaUAzNj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant