-
Notifications
You must be signed in to change notification settings - Fork 11
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
Catching invalid format for CBOR.decode #14
Comments
I guess already used |
For what is worth, these are the occurrences...
Script: require "cbor"
require 'securerandom'
errors = {}
1_000_000.times do |i|
begin
CBOR.decode(SecureRandom.random_bytes(64))
rescue => ex
errors[ex.class.name] ||= 0
errors[ex.class.name] += 1
end
end
pp errors |
I believe it is useful to know whether the CBOR item ended prematurely (EOFError), some other kind of malformedness occurred, or a type-specific error occurred (obviously on a Tag, e.g., URI::InvalidURIError). What is your motivation for wanting to turn this into Kernighan's car? |
BTW, |
But the number of exceptions you found actually points to a genuine bug: An invalid tag (e.g., a URI tag with an invalid URI) is not presented to the application as such, but causes an exception that denies the application access to the entire piece of CBOR. This is great for purists, but not the best strategy for interoperability. |
Hi @cabo, Just trying to understand what could be the best way to gracefully handle wrong/corrupt cbor data on at the application level when data fed by the user agent. I guess that by doing begin
CBOR.decode(input)
rescue CBOR::MalformedFormatError, EOFError, TypeError
# handle error
end one would already catch > 99% of the cases, so that may be enough. The thing is that from the perspective of the caller, It crossed my mind that it might be desirable from the perspective of the caller to be able to do something like: begin
CBOR.decode(input)
rescue CBOR::MalformedFormatError
# handle error
end or begin
CBOR.decode(input)
rescue CBOR::DecodingError
# handle error
end or in case it makes more sense to have several different possible decoding errors, all those could inherit from a CBOR-generic begin
CBOR.decode(input)
rescue CBOR::Error
# handle error
end |
CBOR.decode
can raise several error types when fed with invalid cbor input.returns
Do you think it's possible for the CBOR decoder to either provide some sort fo
CBOR.valid?(input)
method, or forCBOR.decode(input)
to always return the same exception when input is detected to be invalid CBOR?This was raised in cedarcode/cose-ruby#40.
Thank you in advance!
The text was updated successfully, but these errors were encountered: