Skip to content
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

Fix parsing unknown fields breaks record deserialisation #666

Merged

Conversation

borrow-checker
Copy link

This PR fixes an issue where deserialising a record with unknown fields breaks deserialisation of all records. With this change, an error message is displayed and the record is skipped.


  • fix records parsing failure on unknown fields (now skips)
  • cleaned up decryptRecord()

@borrow-checker borrow-checker added the bug Something isn't working label Sep 25, 2024
@borrow-checker borrow-checker self-assigned this Sep 25, 2024
Comment on lines +304 to +328
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as KeeperFile

if (!fileKey.contentEquals(other.fileKey)) return false
if (fileUid != other.fileUid) return false
if (data != other.data) return false
if (url != other.url) return false
if (thumbnailUrl != other.thumbnailUrl) return false

return true
}

override fun hashCode(): Int {
var result = fileKey.contentHashCode()
result = 31 * result + fileUid.hashCode()
result = 31 * result + data.hashCode()
result = 31 * result + url.hashCode()
result = 31 * result + (thumbnailUrl?.hashCode() ?: 0)
return result
}
}
Copy link
Author

Choose a reason for hiding this comment

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

We have a bytearray in this data class which means we need to implement our own equals and hashcode

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, we don't do equality checks on this class

Copy link
Author

Choose a reason for hiding this comment

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

That hashcode is more important here - if this ever goes in a hashed data structure without it it won't work

if (decryptedRecord != null) {
records.add(decryptedRecord)
}
decryptRecord(it, recordKey).onSuccess { keeperRecord -> records.add(keeperRecord) }
Copy link
Author

Choose a reason for hiding this comment

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

this is the magic part where if there's a failure it skips gracefully (i.e. onSuccess only gets called if we ge a successful Result)

Comment on lines +804 to +814
val decryptedRecordFiles = encryptedRecord.files?.map { recordFile ->
val fileKey = decrypt(recordFile.fileKey, recordKey)
val decryptedFile = decrypt(recordFile.data, fileKey)
KeeperFile(
fileKey,
recordFile.fileUid,
Json.decodeFromString(bytesToString(decryptedFile)),
recordFile.url,
recordFile.thumbnailUrl
)
} ?: emptyList()
Copy link
Author

Choose a reason for hiding this comment

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

functional > imperative (plus why allocate when you don't need to?)

)
} ?: emptyList()

val recordData: KeeperRecordData = try {
Copy link
Author

Choose a reason for hiding this comment

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

Restructuring this ensures that there are only two non exception pathways out - either you get the record data you want, or you don't, and both are ok.

@idimov-keeper idimov-keeper changed the base branch from master to release/sdk/java/core/v16.6.6 September 25, 2024 23:58
@borrow-checker borrow-checker changed the title Fix parsing unknown fields break record deserialisation Fix parsing unknown fields breaks record deserialisation Oct 2, 2024
…nto fix/parsing-unknown-fields

# Conflicts:
#	sdk/java/core/src/main/kotlin/com/keepersecurity/secretsManager/core/SecretsManager.kt
@borrow-checker borrow-checker merged commit a90f1b7 into release/sdk/java/core/v16.6.6 Oct 8, 2024
@borrow-checker borrow-checker deleted the fix/parsing-unknown-fields branch October 8, 2024 12:13
@maksimu
Copy link
Collaborator

maksimu commented Oct 8, 2024 via email

@mike-jumper
Copy link

There is no jira ticket for this. Can’t release to prod w/o a ticket

How apt that this PR is #666. 😈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants