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

services/horizon: Validate transaction hash IDs as 64 lowecase hex chars #2394

Merged
merged 9 commits into from
Mar 18, 2020

Conversation

2opremio
Copy link
Contributor

Fixes #2392

PR Checklist

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into two PRs
    otherwise).
  • This PR's title starts with name of package that is most changed in the PR, ex.
    services/friendbot, or all or doc if the changes are broad or impact many
    packages.

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or fixes.
  • I've updated any docs (developer docs, .md
    files, etc... affected by this change). Take a look in the docs folder for a given service,
    like this one.

Release planning

  • I've updated the relevant CHANGELOG (here for Horizon) if
    needed with deprecations, added features, breaking changes, and DB schema changes.
  • I've decided if this PR requires a new major/minor version according to
    semver, or if it's mainly a patch change. The PR is targeted at the next
    release branch if it's not a patch change.

What

Validate transaction ID URL parameters as 64 lowercase hexadecimal characters

Why

Injecting 0x00 was causing internal errors. Also, it's more intuitive to receive a bad request error than a not-found error when there is a problem in the format.

Known limitations

Hardcoding the length may not be a good idea if we change the hash format/length.

This requires a minor release, since we change the HTTP error returned for badly-formatted hashes.

@cla-bot cla-bot bot added the cla: yes label Mar 17, 2020
@2opremio 2opremio requested a review from tamirms March 17, 2020 19:32
@2opremio 2opremio force-pushed the 2392-validate-transaction-hash-ids branch from 80e131a to 71d2b47 Compare March 17, 2020 19:34
@2opremio 2opremio changed the title service/horizon: Validate transaction hash IDs as 64 lowecase hex chars services/horizon: Validate transaction hash IDs as 64 lowecase hex chars Mar 17, 2020
Copy link
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

Nice, it'll be way better to return 400 instead of 404 for these. Some requests regarding the tests below.

services/horizon/internal/actions_transaction_test.go Outdated Show resolved Hide resolved
services/horizon/internal/handler.go Outdated Show resolved Hide resolved
@tamirms
Copy link
Contributor

tamirms commented Mar 17, 2020

@2opremio the tx_id parameter is used in the following endpoints:

GET /transactions/{tx_id}
GET /transactions/{tx_id}/effects
GET /transactions/{tx_id}/operations
GET /transactions/{tx_id}/payments

https://github.com/stellar/go/blob/master/services/horizon/internal/web.go#L234-L238

The fix you have implemented only covers the GET /transactions/{tx_id} request.

There are two patterns for implementing HTTP handlers in Horizon. The legacy pattern resembles: https://github.com/stellar/go/blob/master/services/horizon/internal/actions_effects.go
The new pattern, which we eventually want to adopt for all endpoints, is to define all http handlers in the actions package: https://github.com/stellar/go/blob/master/services/horizon/internal/actions

Unfortunately, all the endpoints which have a tx_id parameter are implemented in the legacy style. Eventually, we should refactor those endpoints and move them to the actions package. But this is outside the scope of your PR.

Our strategy for migrating endpoints to the actions package has been to implement functions which parse and validate request parameters in https://github.com/stellar/go/blob/master/services/horizon/internal/actions/helpers.go . Those functions can be used by the legacy handlers which are defined outside the actions package. But, eventually we will move the handlers to the actions package so having the validators live in https://github.com/stellar/go/blob/master/services/horizon/internal/actions/helpers.go makes the migration easier.

My suggestion is to implement a function to extract the transaction hash from a request in helpers.go . Here is a similar function that you can use as a model https://github.com/stellar/go/blob/master/services/horizon/internal/actions/helpers.go#L379-L396

Then you can call the function in each of the handlers for the endpoints I listed above.

@tamirms tamirms requested a review from abuiles March 18, 2020 05:15
@2opremio 2opremio force-pushed the 2392-validate-transaction-hash-ids branch 2 times, most recently from 2c291b5 to 0b29a51 Compare March 18, 2020 13:49
@2opremio 2opremio force-pushed the 2392-validate-transaction-hash-ids branch from 0b29a51 to fc9d999 Compare March 18, 2020 13:54
This is needed since actions try to decode all parameters even if not present
@2opremio
Copy link
Contributor Author

@tamirms Thanks a lot for the comprehensive explanation. I think everything is addressed. PTAL

@tamirms tamirms changed the base branch from master to release-horizon-v1.1.0 March 18, 2020 16:04
@2opremio 2opremio force-pushed the 2392-validate-transaction-hash-ids branch from 7f25614 to cd6eff2 Compare March 18, 2020 19:02
Copy link
Contributor

@tamirms tamirms left a comment

Choose a reason for hiding this comment

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

looks good to me!

@2opremio 2opremio merged commit 2a50565 into release-horizon-v1.1.0 Mar 18, 2020
@2opremio 2opremio deleted the 2392-validate-transaction-hash-ids branch March 18, 2020 19:22
@ire-and-curses ire-and-curses modified the milestone: Horizon 1.1.0 Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Transaction details endpoint should validate hash parameter
5 participants