-
Notifications
You must be signed in to change notification settings - Fork 45
[PM-41690] fix: CXF import crash on negative timestamps #1362
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
+205
−1
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bc06636
fix: CXF import crash on negative timestamps
crosenth 4f5cef5
Merge branch 'main' into main
crosenth 8806755
Update crates/bitwarden-exporters/src/cxf/import.rs
crosenth 0e7e4c1
Update tests to match review suggestions
crosenth 9f9c360
Add collection and timestamp fallback tests per review
crosenth 90a550e
Merge branch 'main' into main
crosenth b11dc00
Update crates/bitwarden-exporters/src/cxf/import.rs
crosenth 80523e9
Rename and strengthen borrowed Cow test per review
crosenth 49fcb53
Merge branch 'main' into main
harr1424 d0c8ac0
Linted
crosenth 2a9a00d
Merge branch 'main' into main
harr1424 9dedad6
Merge branch 'main' into main
crosenth 451545b
Merge branch 'main' into main
harr1424 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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| mod dashlane_import_test; | ||
| mod negative_timestamp_test; | ||
| mod one_password_import_test; | ||
| mod sample_import_test; |
144 changes: 144 additions & 0 deletions
144
crates/bitwarden-exporters/src/cxf/tests/negative_timestamp_test.rs
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,144 @@ | ||
| //! Tests for handling negative timestamps in CXF import. | ||
| //! | ||
| //! Some credential managers (e.g., Google Password Manager) export timestamps | ||
| //! as the Windows FILETIME epoch (-11644473600) when no real date exists. | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use chrono::Utc; | ||
|
|
||
| use crate::cxf::import::{parse_cxf, sanitize_timestamps}; | ||
|
|
||
| #[test] | ||
| fn test_sanitize_negative_creation_at() { | ||
| let input = r#"{"id":"test","items":[{"id":"1","creationAt":-11644473600,"modifiedAt":1759783057,"title":"Test","credentials":[]}]}"#; | ||
| let result = sanitize_timestamps(input); | ||
| assert!(result.contains(r#""creationAt":null"#)); | ||
| assert!(result.contains(r#""modifiedAt":1759783057"#)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sanitize_negative_modified_at() { | ||
| let input = r#"{"id":"test","items":[{"id":"1","creationAt":1759783057,"modifiedAt":-11644473600,"title":"Test","credentials":[]}]}"#; | ||
| let result = sanitize_timestamps(input); | ||
| assert!(result.contains(r#""creationAt":1759783057"#)); | ||
| assert!(result.contains(r#""modifiedAt":null"#)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sanitize_both_negative() { | ||
| let input = r#"{"id":"test","items":[{"id":"1","creationAt":-11644473600,"modifiedAt":-11644473600,"title":"Test","credentials":[]}]}"#; | ||
| let result = sanitize_timestamps(input); | ||
| assert!(result.contains(r#""creationAt":null"#)); | ||
| assert!(result.contains(r#""modifiedAt":null"#)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sanitize_valid_timestamps_unchanged() { | ||
| let input = r#"{"id":"test","items":[{"id":"1","creationAt":1759783057,"modifiedAt":1759783057,"title":"Test","credentials":[]}]}"#; | ||
| let result = sanitize_timestamps(input); | ||
| assert!(result.contains(r#""creationAt":1759783057"#)); | ||
| assert!(result.contains(r#""modifiedAt":1759783057"#)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sanitize_valid_timestamps_unchanged_returns_borrowed() { | ||
| let input = r#"{"id":"test","items":[{"id":"1","creationAt":1759783057,"modifiedAt":1759783057,"title":"Test","credentials":[]}]}"#; | ||
| let result = sanitize_timestamps(input); | ||
| assert!(matches!(result, std::borrow::Cow::Borrowed(_))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sanitize_negative_timestamps_in_collections() { | ||
| let input = r#"{"id":"test","items":[],"collections":[{"id":"1","creationAt":-11644473600,"modifiedAt":-11644473600,"title":"Test Collection"}]}"#; | ||
| let result = sanitize_timestamps(input); | ||
| assert!(result.contains(r#""creationAt":null"#)); | ||
| assert!(result.contains(r#""modifiedAt":null"#)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sanitize_negative_timestamps_in_sub_collections() { | ||
| let input = r#"{"id":"test","items":[],"collections":[{"id":"1","creationAt":1759783057,"modifiedAt":1759783057,"title":"Parent","subCollections":[{"id":"2","creationAt":-11644473600,"modifiedAt":-11644473600,"title":"Child"}]}]}"#; | ||
| let result = sanitize_timestamps(input); | ||
| // Parent timestamps should be unchanged | ||
| assert!(result.contains(r#""creationAt":1759783057"#)); | ||
| // Child timestamps should be nulled | ||
| assert!(result.contains(r#""creationAt":null"#)); | ||
| assert!(result.contains(r#""modifiedAt":null"#)); | ||
| } | ||
|
|
||
|
harr1424 marked this conversation as resolved.
|
||
| #[test] | ||
| fn test_parse_cxf_with_negative_timestamps_does_not_error() { | ||
| let input = r#"{ | ||
| "id": "DZSXp7iBQY-Fg-OofakQtQ", | ||
| "username": "user@example.com", | ||
| "email": "user@example.com", | ||
| "fullName": "Test User", | ||
| "collections": [], | ||
| "items": [{ | ||
| "id": "9OF-QjVDQo2Wp2xWPw6ZhA", | ||
| "creationAt": -11644473600, | ||
| "modifiedAt": -11644473600, | ||
| "title": "Test Entry", | ||
| "credentials": [{ | ||
| "type": "basic-auth", | ||
| "username": { | ||
| "id": "-eZX0Gw-TzOsBFwt67N7ZA", | ||
| "fieldType": "string", | ||
| "value": "testuser" | ||
| }, | ||
| "password": { | ||
| "id": "wgu3wTcXSYawrGMWMtaANg", | ||
| "fieldType": "concealed-string", | ||
| "value": "testpass" | ||
| }, | ||
| "urls": ["https://example.com"] | ||
| }] | ||
| }] | ||
| }"#; | ||
| let result = parse_cxf(input.to_string()); | ||
| assert!( | ||
| result.is_ok(), | ||
| "parse_cxf should not error on negative timestamps: {:?}", | ||
| result.err() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_cxf_negative_timestamps_fallback_to_current_time() { | ||
| let input = r#"{ | ||
| "id": "DZSXp7iBQY-Fg-OofakQtQ", | ||
| "username": "user@example.com", | ||
| "email": "user@example.com", | ||
| "fullName": "Test User", | ||
| "collections": [], | ||
| "items": [{ | ||
| "id": "9OF-QjVDQo2Wp2xWPw6ZhA", | ||
| "creationAt": -11644473600, | ||
| "modifiedAt": -11644473600, | ||
| "title": "Test Entry", | ||
| "credentials": [{ | ||
| "type": "basic-auth", | ||
| "username": { | ||
| "id": "-eZX0Gw-TzOsBFwt67N7ZA", | ||
| "fieldType": "string", | ||
| "value": "testuser" | ||
| }, | ||
| "password": { | ||
| "id": "wgu3wTcXSYawrGMWMtaANg", | ||
| "fieldType": "concealed-string", | ||
| "value": "testpass" | ||
| }, | ||
| "urls": ["https://example.com"] | ||
| }] | ||
| }] | ||
| }"#; | ||
| let result = parse_cxf(input.to_string()).unwrap(); | ||
|
|
||
| // When timestamps are negative (clamped to null), convert_date falls | ||
| // back to Utc::now(). Verify the resulting dates are approximately now. | ||
| let cipher = &result[0]; | ||
| assert!(cipher.creation_date > Utc::now() - chrono::Duration::seconds(5)); | ||
| assert!(cipher.revision_date > Utc::now() - chrono::Duration::seconds(5)); | ||
| } | ||
| } | ||
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.