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

Height::from_str accepts invalid heights #752

Closed
Tracked by #807
benluelo opened this issue Jul 8, 2023 · 6 comments · Fixed by #758
Closed
Tracked by #807

Height::from_str accepts invalid heights #752

benluelo opened this issue Jul 8, 2023 · 6 comments · Fixed by #758
Labels
A: bug Admin: something isn't working
Milestone

Comments

@benluelo
Copy link

benluelo commented Jul 8, 2023

Bug Summary

Height::from_str accepts invalid heights. The implementation should use split_once instead of split:
https://github.com/cosmos/ibc-rs/blob/main/crates/ibc/src/core/ics02_client/height.rs#L174-L197

Details

let invalid_height_string = "1-1-1";

let roundtrip = invalid_height_string
    .parse::<Height>()
    .unwrap()
    .to_string();

assert_eq!(invalid_height_string, roundtrip);

Version

latest main.

@DaviRain-Su
Copy link
Contributor

I believe you are intentionally creating errors, or in other words, you think there might be incorrect usage in the code.

@KaiserKarel
Copy link

I believe you are intentionally creating errors, or in other words, you think there might be incorrect usage in the code.

yeah, which is why parse returns an error; it should only return Ok if the provided string is valid; otherwise it is unusable for untrusted inputs, which parse is ToStr is intended for.

@benluelo
Copy link
Author

Also, the use of indexing causes a panic if there is no - in the string:

"1".parse::<ibc::Height>();
"".parse::<ibc::Height>();
thread 'height' panicked at 'index out of bounds: the len is 1 but the index is 1'

@DaviRain-Su
Copy link
Contributor

I want to know where this input can be generated.

@benluelo
Copy link
Author

This is public api in a published library. Where the input is generated is irrelevant. TryFrom is not supposed to panic on invalid input - it returns a Result for a reason, as was previously stated. Honestly, this could be seen as a security vulnerability; a caller would expect any conversion errors to be returned in an Err, and would therefore think it would be safe to call in security sensitive code.

@Farhad-Shabani
Copy link
Member

@benluelo much appreciated for bringing this up. This is very true!

@Farhad-Shabani Farhad-Shabani added this to the v0.43.0 milestone Jul 11, 2023
@github-project-automation github-project-automation bot moved this from 📥 To Do to ✅ Done in ibc-rs Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A: bug Admin: something isn't working
Projects
Status: Done
4 participants