Skip to content

Fix input sanitation for listchaintxns lncli cmd#9558

Merged
guggero merged 2 commits into
lightningnetwork:masterfrom
kornpow:fix-listchaintxns-lncli-inputs
Mar 24, 2025
Merged

Fix input sanitation for listchaintxns lncli cmd#9558
guggero merged 2 commits into
lightningnetwork:masterfrom
kornpow:fix-listchaintxns-lncli-inputs

Conversation

@kornpow

@kornpow kornpow commented Feb 26, 2025

Copy link
Copy Markdown
Contributor

Change Description

This PR was based originally on the PR #7496 and issue #7316. Upon rebasing and looking at the solutions there, I found the bigger problem with the command was the lack of error messaging when the end_height < start_height rather than the necessity to support a "reverse ordering" feature.

In this PR, I added additional checks to the block height input arguments which prevents providing arguments which get passed down the stack which yields incorrect results.

For example:

end_height < start_height
OR
start_height < 0

This is my first code commit to LND, 2nd including a comment update, so I appreciate the help to getting this PR mergable!

Steps to Test

Try using different options for start_height and end_height, including them, not including them, and having integer values in a way which doesn't make sense, and ensure that the proper errors are shown.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

@coderabbitai

coderabbitai Bot commented Feb 26, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch 2 times, most recently from 4f4641d to fa5d0b4 Compare February 26, 2025 23:06

@guggero guggero left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fix. Makes sense, I think the "will be queried in reverse" was probably from an earlier version of the PR that added the offset.

Comment thread cmd/commands/commands.go Outdated
Comment thread docs/release-notes/release-notes-0.19.0.md Outdated
Comment thread docs/release-notes/release-notes-0.19.0.md Outdated
@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch from fa5d0b4 to 60b4c13 Compare February 27, 2025 16:53
@kornpow

kornpow commented Feb 27, 2025

Copy link
Copy Markdown
Contributor Author

"will be queried in reverse"

This wording has been there for a long time, and its what inspired the original issue/PR, even before the offset PR. But now we fixed the help text, we don't have to implement it.

@kornpow kornpow requested a review from guggero February 27, 2025 18:33
@kornpow

kornpow commented Feb 27, 2025

Copy link
Copy Markdown
Contributor Author

@guggero I would like to potentially add a test for this, but I am not really sure where? Maybe there isnt tests for these kinds of things.

@guggero

guggero commented Feb 28, 2025

Copy link
Copy Markdown
Collaborator

@guggero I would like to potentially add a test for this, but I am not really sure where? Maybe there isnt tests for these kinds of things.

You could extract the argument parsing into a function, then write a unit test for that in cmd/commands/commands_test.go.

The other approach would be an integration test that executes the binary directly. See

func testDebuglevelShow(ht *lntest.HarnessTest) {

@lightninglabs-deploy

Copy link
Copy Markdown
Collaborator

@guggero: review reminder

@guggero guggero removed their request for review March 7, 2025 13:39
@guggero

guggero commented Mar 12, 2025

Copy link
Copy Markdown
Collaborator

!lightninglabs-deploy mute

@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch 3 times, most recently from 42436ba to 3bf1cf6 Compare March 12, 2025 18:16
@kornpow

kornpow commented Mar 12, 2025

Copy link
Copy Markdown
Contributor Author

I have added a test with a decent number of test cases.

I also modified the listchaintxns command to formalize that the default case value for end_height=-1. This part confused me a bit when reading through the code, since things "seem to work" when end_height is left unset, which used to give it a default value of 0. However, I think setting it to a default is better.

Finally, as much as I would like to remove the cli context from the test itself, since it makes the test code a decent bit more verbose, I think it is necessary because of testing the behavior like in the paragraph above, where there are certain values unset in the CLI. Also I think it is good because it could provide an example for others how to test the CLI.

Please let me know if you would like me to improve the test structure. I potentially could make the inputs inside a loop and reusing variables, but there is funkiness there with redefining values in flag which I'd have to tackle.

@kornpow

kornpow commented Mar 12, 2025

Copy link
Copy Markdown
Contributor Author

Been running my new test like this:
env GOEXPERIMENT=loopvar go test -tags="dev autopilotrpc chainrpc invoicesrpc neutrinorpc peersrpc routerrpc signrpc verrpc walletrpc watchtowerrpc wtclientrpc nolog" github.com/lightningnetwork/lnd/cmd/commands -v -run "TestInputListChainTxns"

@MPins MPins left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good job for your first PR 👍

I ran the test and it’s working — maybe just a few small adjustments are needed.

Comment thread cmd/commands/commands_test.go Outdated
Comment thread cmd/commands/commands_test.go Outdated
@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch 3 times, most recently from 303b741 to 8690d93 Compare March 14, 2025 21:10
@kornpow

kornpow commented Mar 14, 2025

Copy link
Copy Markdown
Contributor Author

I also made a commit with some changes to the help text of the command. It feels like too much of the Flag Usage is finding its way into the Description. Its also really redundant trying to get across the end_height=-1 concept. Let me know if that is out of scope.

@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch 2 times, most recently from 4efbbe2 to 1ea7067 Compare March 14, 2025 22:36
@kornpow kornpow requested a review from MPins March 17, 2025 18:37

@MPins MPins left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great progress! 👍

I’d suggest squashing the commits into two: one for the doc release and one for the other files — what do you think?

https://github.com/lightningnetwork/lnd/blob/master/docs/code_contribution_guidelines.md#ideal-git-commit-structure

Comment thread cmd/commands/commands_test.go
@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch 2 times, most recently from 387cc42 to 1cd4d7d Compare March 18, 2025 00:00
@kornpow

kornpow commented Mar 18, 2025

Copy link
Copy Markdown
Contributor Author

Great progress! 👍

I’d suggest squashing the commits into two: one for the doc release and one for the other files — what do you think?

https://github.com/lightningnetwork/lnd/blob/master/docs/code_contribution_guidelines.md#ideal-git-commit-structure

Sounds good. Squashed to 2 commits. Getting better at managing the commit history, but still learning where to split the commits. Getting closer I hope!

@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch from 1cd4d7d to 3988024 Compare March 18, 2025 00:26

@MPins MPins left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some final comments and LGTM! 👍

It would be a good idea to have some description on commit message. Something like:

commands: add a parsing block height function
Added parseBlockHeightInputs function to parse start_height and end_height
from the CLI, ensuring start_height is non-negative and not greater than
end_height (unless it's -1), returning both values and any validation error.

Comment thread cmd/commands/commands_test.go Outdated
@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch from 3988024 to 0354a72 Compare March 18, 2025 17:25
@kornpow

kornpow commented Mar 18, 2025

Copy link
Copy Markdown
Contributor Author

rebased to fix a merge conflict and to improve commit message.

@MPins

MPins commented Mar 20, 2025

Copy link
Copy Markdown
Contributor

rebased to fix a merge conflict and to improve commit message.

You should leave just one of the following lines in the commit message:

cmd: Modified listchaintxns command

commands: add a parsing block height function

Maybe the first is better 😉

@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch from 0354a72 to 23f6368 Compare March 23, 2025 18:08
@kornpow

kornpow commented Mar 23, 2025

Copy link
Copy Markdown
Contributor Author

rebased to fix a merge conflict and to improve commit message.

You should leave just one of the following lines in the commit message:

cmd: Modified listchaintxns command

commands: add a parsing block height function

Maybe the first is better 😉

Rebased again onto master, and modified the commit message... Hopefully acceptable now.

Its been hard to get all of the integration tests to always pass consistently? I don't think it is related to my changes though? Please let me know.

@kornpow kornpow requested a review from guggero March 23, 2025 18:11

@guggero guggero left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the updates. Getting closer.

Comment thread cmd/commands/commands.go Outdated
Comment thread cmd/commands/commands.go
Comment thread cmd/commands/commands.go Outdated
Comment thread cmd/commands/commands.go Outdated
Comment thread cmd/commands/commands_test.go Outdated
Comment thread cmd/commands/commands_test.go Outdated
@kornpow

kornpow commented Mar 24, 2025

Copy link
Copy Markdown
Contributor Author

image
Picture of the new help text, to get a feel for how it looks in-situ

kornpow added 2 commits March 24, 2025 10:53
add a parsing block height function and error when block heights would produce invalid results
@kornpow kornpow force-pushed the fix-listchaintxns-lncli-inputs branch from 23f6368 to 2d82956 Compare March 24, 2025 16:53
@guggero guggero self-requested a review March 24, 2025 17:47
@guggero guggero merged commit f7b3177 into lightningnetwork:master Mar 24, 2025
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.

4 participants