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

items from first channel upgrades security audit session #5664

Merged
merged 25 commits into from
Jan 23, 2024

Conversation

crodriguezvega
Copy link
Contributor

@crodriguezvega crodriguezvega commented Jan 18, 2024

Description

I have created this branch where we can tackle the issues from the audit. @DimitrisJim @charleenfei please pick any items from the list and push to this branch, I thought doing it like this would be easier and faster than opening individual issues (and then we only need to backport one PR). I think the issues are small enough that should be easy to review all of them in one PR.

closes: #XXXX

Commit Message / Changelog Entry

type: commit message

see the guidelines for commit messages. (view raw markdown for examples)


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against the correct branch (see CONTRIBUTING.md).
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards and Go style guide.
  • Wrote unit and integration tests.
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/).
  • Added relevant godoc comments.
  • Provide a commit message to be used for the changelog entry in the PR description for review.
  • Re-reviewed Files changed in the Github PR explorer.
  • Review Codecov Report in the comment section below once CI passes.

@crodriguezvega crodriguezvega changed the title items from first security audit session items from first channel upgrades security audit session Jan 18, 2024
@crodriguezvega crodriguezvega marked this pull request as draft January 18, 2024 23:04
@codecov-commenter
Copy link

codecov-commenter commented Jan 18, 2024

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (40564ed) 81.16% compared to head (61bfcd5) 81.17%.
Report is 4 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #5664   +/-   ##
=======================================
  Coverage   81.16%   81.17%           
=======================================
  Files         199      199           
  Lines       15278    15286    +8     
=======================================
+ Hits        12401    12409    +8     
  Misses       2408     2408           
  Partials      469      469           
Files Coverage Δ
modules/core/04-channel/keeper/keeper.go 90.77% <100.00%> (ø)
modules/core/04-channel/keeper/packet.go 99.12% <100.00%> (+<0.01%) ⬆️
modules/core/04-channel/keeper/upgrade.go 92.27% <100.00%> (+0.04%) ⬆️
modules/core/04-channel/types/upgrade.go 100.00% <100.00%> (ø)
modules/core/keeper/msg_server.go 66.81% <90.90%> (-0.19%) ⬇️
modules/core/24-host/packet_keys.go 0.00% <0.00%> (ø)

@DimitrisJim
Copy link
Contributor

sgtm, would prefer PRs since its an easier workflow to not make mistakes in but lets give this a shot. Would be nice to sync up via slack when people plan to push so we don't end up with weird conflicts. also no force pushing!

Move log into the if to only log if channel state actually changes. Could be moved outside for both cases
but might then be logging when channel state doesn't change.
@DimitrisJim DimitrisJim marked this pull request as ready for review January 19, 2024 15:55
@DimitrisJim
Copy link
Contributor

I see we've done all things from the list so opening this up for review/e2e run. Feel free to make draft if I missed something.

Copy link
Contributor Author

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Changes look great to me, pending the decision on the order of calling the callback. Great work @DimitrisJim and @charleenfei completing all the audit issues in less than 24 hours! 🦾

@crodriguezvega crodriguezvega added the priority PRs that need prompt reviews label Jan 21, 2024
@crodriguezvega crodriguezvega added this to the 04-channel upgrades RC milestone Jan 21, 2024
Copy link
Contributor

@chatton chatton left a comment

Choose a reason for hiding this comment

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

Looks great thank you! I left a couple of small suggestions

modules/core/04-channel/keeper/upgrade.go Outdated Show resolved Hide resolved
modules/core/04-channel/keeper/upgrade.go Outdated Show resolved Hide resolved
modules/core/04-channel/keeper/upgrade_test.go Outdated Show resolved Hide resolved
modules/core/04-channel/keeper/upgrade_test.go Outdated Show resolved Hide resolved
modules/core/04-channel/keeper/upgrade_test.go Outdated Show resolved Hide resolved
modules/core/04-channel/types/upgrade_test.go Outdated Show resolved Hide resolved
modules/core/04-channel/types/upgrade_test.go Show resolved Hide resolved
Comment on lines +125 to +134
for {
_, ok := err.(*UpgradeError)
if ok {
return true
}

if err = errors.Unwrap(err); err == nil {
return false
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

the presence of this function, means we can actually update the Unwrap fn of the UpgradeError type, I believe it can just be

// Unwrap returns the base error that caused the upgrade to fail.
func (u *UpgradeError) Unwrap() error {
	return errors.Unwrap(u.err)
}

And the corresponding test can be updated to

// TestUpgradeErrorUnwrap tests that the underlying error is not modified when Unwrap is called.
func (suite *TypesTestSuite) TestUpgradeErrorUnwrap() {
	baseUnderlyingError := errorsmod.Wrap(types.ErrInvalidChannel, "base error")
	wrappedErr := errorsmod.Wrap(baseUnderlyingError, "wrapped error")
	upgradeError := types.NewUpgradeError(1, wrappedErr)

	originalUpgradeError := upgradeError.Error()
	unWrapped := errors.Unwrap(upgradeError)

	postUnwrapUpgradeError := upgradeError.Error()

	suite.Require().Equal(baseUnderlyingError, unWrapped, "unwrapped error was not equal to base underlying error")
	suite.Require().Equal(originalUpgradeError, postUnwrapUpgradeError, "original error was modified when unwrapped")
}

I think I like this better since it becomes more of a direct passthrough.

Copy link
Contributor

Choose a reason for hiding this comment

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

this would return nil on unwrapping a simple wrapped err no? UpgradeError { err: types.ErrInvalidChannel } would call errors.Unwrap(types.ErrInvalidChannel) which returns nil yea?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, nil is returned if err is just a single unwrapped error in that case

@DimitrisJim DimitrisJim self-assigned this Jan 22, 2024
@DimitrisJim
Copy link
Contributor

I can take care of addressing review(s)

DimitrisJim and others added 5 commits January 22, 2024 16:35
Co-authored-by: Cian Hatton <cian@interchain.io>
- Prefer using SetChannelState over look-up channel, modify, set.
- Add deeply test case with error wrapped multiple times.
channel.State = types.FLUSHCOMPLETE
k.SetChannel(ctx, portID, channelID, channel)
k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", channel.State)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I guess the reason we had to the logger at the bottom is that startFlushing might move the channel state from OPEN to FLUSHING, but, the previous channel state also could have been FLUSHING. A bit of a rock and a hard place

modules/core/04-channel/keeper/upgrade.go Outdated Show resolved Hide resolved
modules/core/04-channel/keeper/upgrade.go Outdated Show resolved Hide resolved
modules/core/04-channel/keeper/upgrade.go Outdated Show resolved Hide resolved
Copy link
Contributor

@colin-axner colin-axner left a comment

Choose a reason for hiding this comment

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

Thanks everyone pushing commits! Big thanks to @DimitrisJim for following up on review comments!

Copy link
Member

@damiannolan damiannolan left a comment

Choose a reason for hiding this comment

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

Thank you all! 🚀

Comment on lines +125 to +134
for {
_, ok := err.(*UpgradeError)
if ok {
return true
}

if err = errors.Unwrap(err); err == nil {
return false
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, nil is returned if err is just a single unwrapped error in that case

Copy link
Member

@AdityaSripal AdityaSripal left a comment

Choose a reason for hiding this comment

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

Nice work!

@DimitrisJim DimitrisJim enabled auto-merge (squash) January 23, 2024 10:46
@DimitrisJim DimitrisJim merged commit 41501a2 into main Jan 23, 2024
63 of 64 checks passed
@DimitrisJim DimitrisJim deleted the channel-upgrades/security-audit-day1 branch January 23, 2024 10:49
mergify bot pushed a commit that referenced this pull request Jan 23, 2024
* some items from the security audit

* update tests for cancelling when channel is in FLUSHCOMPLETE

* remove duplicate test

* nit: Move wrapping of returned error to direct return statement.

Do not create a new errChanUpgradeFailed for logging/returning, it obfuscates the logic and does not
yield much benefit.

* nit: consistency in err return cbs.OnChanUpgradeTry

Wrap the error before returning as is done in ChannelUpgradeInit.

* add comment

* nit: Call application callbacks before performing any state change operations.

* chore: Add documentation indicating to app devs that callbacks are invoked _before_ core state is written.

* upgrade error

* nit: Add note on handling wrapped error in IsUpgradeError docu string, add fmt.Errorf wrap test case.

* Update modules/core/04-channel/types/upgrade_test.go

* chore: Use consistent logging in writeUpgradeAck + writeUpgradeConfirm

Move log into the if to only log if channel state actually changes. Could be moved outside for both cases
but might then be logging when channel state doesn't change.

* Apply suggestions from code review

Co-authored-by: Cian Hatton <cian@interchain.io>

* nit: review comments from Cian.

- Prefer using SetChannelState over look-up channel, modify, set.
- Add deeply test case with error wrapped multiple times.

* nit: Address Colin's comment

* nit: just linter thangs.

* Apply suggestions from code review

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* nit: Fix linter flatten if else.

---------

Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
Co-authored-by: Charly <charly@interchain.berlin>
Co-authored-by: Charly <charly@interchain.io>
Co-authored-by: Cian Hatton <cian@interchain.io>
Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
(cherry picked from commit 41501a2)

# Conflicts:
#	docs/docs/01-ibc/06-channel-upgrades.md
damiannolan pushed a commit that referenced this pull request Jan 23, 2024
) (#5688)

* items from first channel upgrades security audit session (#5664)

* some items from the security audit

* update tests for cancelling when channel is in FLUSHCOMPLETE

* remove duplicate test

* nit: Move wrapping of returned error to direct return statement.

Do not create a new errChanUpgradeFailed for logging/returning, it obfuscates the logic and does not
yield much benefit.

* nit: consistency in err return cbs.OnChanUpgradeTry

Wrap the error before returning as is done in ChannelUpgradeInit.

* add comment

* nit: Call application callbacks before performing any state change operations.

* chore: Add documentation indicating to app devs that callbacks are invoked _before_ core state is written.

* upgrade error

* nit: Add note on handling wrapped error in IsUpgradeError docu string, add fmt.Errorf wrap test case.

* Update modules/core/04-channel/types/upgrade_test.go

* chore: Use consistent logging in writeUpgradeAck + writeUpgradeConfirm

Move log into the if to only log if channel state actually changes. Could be moved outside for both cases
but might then be logging when channel state doesn't change.

* Apply suggestions from code review

Co-authored-by: Cian Hatton <cian@interchain.io>

* nit: review comments from Cian.

- Prefer using SetChannelState over look-up channel, modify, set.
- Add deeply test case with error wrapped multiple times.

* nit: Address Colin's comment

* nit: just linter thangs.

* Apply suggestions from code review

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* nit: Fix linter flatten if else.

---------

Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
Co-authored-by: Charly <charly@interchain.berlin>
Co-authored-by: Charly <charly@interchain.io>
Co-authored-by: Cian Hatton <cian@interchain.io>
Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
(cherry picked from commit 41501a2)

# Conflicts:
#	docs/docs/01-ibc/06-channel-upgrades.md

* nit: rm docs.

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority PRs that need prompt reviews
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

8 participants