Closed RussBaz closed 7 months ago
OK, it turns out that the key can indeed sometimes contain \0
sequence, and it breaks Postgres… Any recommendations on how to possibly store such a data?
The classical approach would be Base64 encoding:
extension SymmetricKey: LosslessStringConvertible {
public init?(_ description: String) {
guard let data = Data(base64Encoded: description) else {
return nil
}
self.init(data: data)
}
public var description: String {
self.withUnsafeBytes { Data($0).base64EncodedString() }
}
init(_ key: String, size: SymmetricKeySize = .bits192) throws {
guard let data = Data(base64Encoded: key) else {
throw CryptoKitError.incorrectParameterSize
}
guard data.count size.bitCount >> 3 else {
throw CryptoKitError.incorrectKeySize
}
self.init(data: data)
}
}
The alternative would be to store it directly as [UInt8]
(using .array(of: .uint8)
in the migration) instead of doing String
conversions of any kind (which are relatively expensive even for simple hexadecimal representation, much less Base64).
Describe the bug
Occasionally, my Vapor server will write the following error in the log (manually formatted):
and I do not know how to reproduce it or what causes it.
This query is executed from an inside a transaction from a helper function:
To Reproduce
I need help debugging this issue, as I do not know how to reproduce the error.
Also, I have not tried this yet with a more modern version of Postgres or on Apple Silicon.
In case you think this is related to the input data, here is the cleaned up definition of the OTP struct and the model that is being inserted:
Expected behavior
I did not expect this error to be shown at all.
Environment
PostgreSQL 14.10 on aarch64-apple-darwin20.6.0, compiled by Apple clang version 12.0.5 (clang-1205.0.22.9), 64-bit
Additional context
It is more likely to occur when I quickly call the same endpoint multiple times.