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 break record deserialisation #666

Open
wants to merge 1 commit into
base: release/sdk/java/core/v16.6.6
Choose a base branch
from

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
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.

2 participants