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

Validate port-options #22087

Merged
merged 2 commits into from
Oct 12, 2022
Merged

Validate port-options #22087

merged 2 commits into from
Oct 12, 2022

Conversation

amadeuszpawlik
Copy link
Contributor

@amadeuszpawlik amadeuszpawlik commented May 27, 2021

Validate port-options, so that invalid values are rejected early in the startup.
Ports are uint16_ts, which effectively limits a port's value to <=65535. As discussed in #24116 and #24344, port "0" is considered invalid too.
Proposed in #21893 (comment)

The SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut) now returns a bool that indicates whether the port value was set and within the allowed range. This is an improvement that can be used not only for port validation of options at startup, but also in rpc calls, etc,

@amadeuszpawlik amadeuszpawlik changed the title Sanitize ports Sanitize port numbers May 27, 2021
@amadeuszpawlik
Copy link
Contributor Author

Should this be done with -zmqpub... options as well?

@jonatack
Copy link
Contributor

I'd suggest searching for and looking at a previous pull request proposal to do this. Consider also unit testing and Doxygen docs.

@jonatack
Copy link
Contributor

Consider also the various places in the codebase where users can input port numbers.

@DrahtBot
Copy link
Contributor

DrahtBot commented May 27, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25136 (Torcontrol opt check by amadeuszpawlik)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@amadeuszpawlik
Copy link
Contributor Author

amadeuszpawlik commented May 29, 2021

Added doxygen descriptions
Added/updated test for SplitHostPort
@jonatack could you clarify

Consider also the various places in the codebase where users can input port numbers.

Have I missed something, like a config?

@amadeuszpawlik amadeuszpawlik changed the title WIP Sanitize port numbers Sanitize port numbers May 31, 2021
@amadeuszpawlik amadeuszpawlik marked this pull request as ready for review May 31, 2021 05:07
Copy link
Contributor

@klementtan klementtan left a comment

Choose a reason for hiding this comment

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

You might wanna add the validation at addpeeraddress too. Currently, the port number will overflow if it is invalid.

@amadeuszpawlik
Copy link
Contributor Author

Thanks for your input, @klementtan !
I addressed your comment in a1c5914

Copy link
Contributor

@klementtan klementtan left a comment

Choose a reason for hiding this comment

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

Thanks for updating it!

I think you might have missed out the scenario where the user enters -1 as the port. On a1c5914

$ ./src/bitcoin-cli -signet addpeeraddress "1.1.1.3" -1
{
  "success": true
}

You might wanna refactor the logic in strencodings.cpp so that the port validation code could be reused in other places(ie addpeeraddress)

src/util/strencodings.cpp Outdated Show resolved Hide resolved
@amadeuszpawlik
Copy link
Contributor Author

@klementtan

think you might have missed out the scenario where the user enters -1 as the port.

You are absolutely right, fixed that. I also added a couple negative port numbers in the tests, thanks.

@laanwj
Copy link
Member

laanwj commented Aug 2, 2021

Concept ACK. I would personally call this "validate" (as in input validation), not "santize". In the source base we use "sanitize" only in the context of removing invalid characters from strings in logging.

src/util/strencodings.h Outdated Show resolved Hide resolved
@amadeuszpawlik amadeuszpawlik changed the title Sanitize port numbers Validate port numbers Aug 27, 2021
@amadeuszpawlik
Copy link
Contributor Author

amadeuszpawlik commented Aug 27, 2021

@laanwj

Concept ACK. I would personally call this "validate" (as in input validation), not "santize". In the source base we use "sanitize" only in the context of removing invalid characters from strings in logging.

I have changed the wording, thanks 👍

luke-jr pushed a commit to luke-jr/bitcoin that referenced this pull request May 21, 2022
Check `port` options for invalid values (ports are parsed as uint16, so
in practice values >65535 are invalid; port 0 is undefined and therefore
considered invalid too). This allows for an early rejection of faulty
values and an supplying an informative message to the user.

Splits tests in `feature_proxy.py` to cover both invalid `hostname`
and `port` values.

Github-Pull: bitcoin#22087
Rebased-From: 1b6f8d3
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 28, 2022
ada8358 Sanitize port in `addpeeraddress()` (amadeuszpawlik)

Pull request description:

  In connection to bitcoin#22087, it has been [pointed out](bitcoin#22087 (review)) that `addpeeraddress` needs to get its port-value sanitized.

ACKs for top commit:
  fanquake:
    ACK ada8358

Tree-SHA512: 48771cd4f6940aa7840fa23488565c09dea86bd5ec5a5a1fc0374afb4857aebcd2a1f51e2d4cb7348460e0ad9793dc5d2962df457084ed2b8d8142cae650003f
src/init.cpp Show resolved Hide resolved
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Reviewed 1dae86b, and it looks almost ok, but I am concerned different SplitHostPort hostOut values when port is invalid could potentially be bad for callers. Or even if it doesn't break anything, it seems better just to add a additional return value not change hostOut values in this PR.

It would also be good to note the change of behavior in the PR description or release notes that -port and -rpcport values that previously worked and were considered valid can now result in errors. For example port values with leading whitespace or trailing non-digit characters.

I do think code in second commit is a little awkward because it is parsing and validating address and port options in different place where they are used. But probably this is fine, since in the future this will probably be cleaned up by introducing options structs and moving new code here to a function like ApplyArgsManOptions that does parsing and error-checking all in one place.

src/util/strencodings.cpp Outdated Show resolved Hide resolved
src/util/strencodings.cpp Outdated Show resolved Hide resolved
src/test/netbase_tests.cpp Outdated Show resolved Hide resolved
src/init.cpp Outdated Show resolved Hide resolved
@fanquake
Copy link
Member

fanquake commented Oct 2, 2022

@amadeuszpawlik any chance you're still around / interested in responding to the latest review feedback?

@amadeuszpawlik
Copy link
Contributor Author

amadeuszpawlik commented Oct 3, 2022

@amadeuszpawlik any chance you're still around / interested in responding to the latest review feedback?

@fanquake Yes, definitely. Will have a go at it shortly 👍

@amadeuszpawlik amadeuszpawlik force-pushed the sanitize_ports branch 4 times, most recently from 72870e1 to 7a7eb90 Compare October 3, 2022 18:41
src/util/strencodings.cpp Outdated Show resolved Hide resolved
@amadeuszpawlik amadeuszpawlik force-pushed the sanitize_ports branch 2 times, most recently from 9c2031d to de27526 Compare October 5, 2022 17:05
Forward the validation of the port from `ParseUInt16(...)`.
Consider port 0 as invalid.
Add suitable test for the `SplitHostPort` function.
Add doxygen description to the `SplitHostPort` function.
Check `port` options for invalid values (ports are parsed as uint16, so
in practice values >65535 are invalid; port 0 is undefined and therefore
considered invalid too). This allows for an early rejection of faulty
values and an supplying an informative message to the user.

Splits tests in `feature_proxy.py` to cover both invalid `hostname`
and `port` values.

Adds a release-note as previously valid `-port` and `-rpcport` values
can now result in errors.
@amadeuszpawlik
Copy link
Contributor Author

Thanks @ryanofsky for taking your time. I have now resolved all your comments and added a release-note.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK 0452678. Just suggested changes since last review: reverting some SplitHostPort changes, adding release notes, avoiding 'GetArgs[0]` problem.

@luke-jr
Copy link
Member

luke-jr commented Oct 6, 2022

utACK 0452678

@luke-jr
Copy link
Member

luke-jr commented Oct 6, 2022

(In the future, though, please don't rebase every time you make changes - only when needed)

@fanquake
Copy link
Member

@ryanofsky just fyi. You've re-ACK the previous commit hash in your latest comment

@ryanofsky
Copy link
Contributor

@ryanofsky just fyi. You've re-ACK the previous commit hash in your latest comment

Thank you, fixed now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants