-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: allow overriding default exclude-ports in CLI #7324
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
Open
chovanecadam
wants to merge
1
commit into
projectdiscovery:dev
Choose a base branch
from
chovanecadam:cli-reserved-ports
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package contextargs | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestUseNetworkPort(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| target string | ||
| templatePort string | ||
| templateExcl string | ||
| cliReserved []string | ||
| expectedHost string | ||
| }{ | ||
| { | ||
| name: "default-reserved-ports-work", | ||
| target: "example.com:80", | ||
| templatePort: "1234", | ||
| templateExcl: "", | ||
| cliReserved: nil, | ||
| expectedHost: "example.com:1234", | ||
| }, | ||
| { | ||
| name: "default-target-works", | ||
| target: "example.com:22", | ||
| templatePort: "80", | ||
| templateExcl: "", | ||
| cliReserved: nil, | ||
| expectedHost: "example.com:22", | ||
| }, | ||
| { | ||
| name: "template-overwrites-defaults-no-exclusions", | ||
| target: "example.com:80", | ||
| templatePort: "1234", | ||
| templateExcl: "0", | ||
| cliReserved: nil, | ||
| expectedHost: "example.com:80", | ||
| }, | ||
| { | ||
| name: "template-overwrites-defaults-some-exclusions", | ||
| target: "example.com:5353", | ||
| templatePort: "1234", | ||
| templateExcl: "5353", | ||
| cliReserved: nil, | ||
| expectedHost: "example.com:1234", | ||
| }, | ||
| { | ||
| name: "cli-overwrites-template", | ||
| target: "example.com:80", | ||
| templatePort: "1234", | ||
| templateExcl: "0", | ||
| cliReserved: []string{"80"}, | ||
| expectedHost: "example.com:1234", | ||
| }, | ||
| { | ||
| name: "cli-overwrites-default-no-exclusions", | ||
| target: "example.com:80", | ||
| templatePort: "1234", | ||
| templateExcl: "", | ||
| cliReserved: []string{"0"}, | ||
| expectedHost: "example.com:80", | ||
| }, | ||
| { | ||
| name: "cli-overwrites-default-some-exclusions", | ||
| target: "example.com:5353", | ||
| templatePort: "1234", | ||
| templateExcl: "", | ||
| cliReserved: []string{"5353"}, | ||
| expectedHost: "example.com:1234", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx := NewWithInput(context.Background(), tt.target) | ||
| err := ctx.UseNetworkPort(tt.templatePort, tt.templateExcl, tt.cliReserved) | ||
|
|
||
| require.NoError(t, err, "unexpected error") | ||
| require.Equal(t, tt.expectedHost, ctx.MetaInput.Input) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 1835
🏁 Script executed:
rg -n "normalizePorts|TrimSpace" pkg/protocols/common/contextargs/ --type goRepository: projectdiscovery/nuclei
Length of output: 49
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 174
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 152
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 520
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 636
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 49
🏁 Script executed:
# Find sliceutil package/file fd -t f -i sliceutilRepository: projectdiscovery/nuclei
Length of output: 49
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 908
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 49
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 556
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 2619
🏁 Script executed:
# Check all imports in the contextargs file to understand what stringsutil is used head -20 pkg/protocols/common/contextargs/contextargs.goRepository: projectdiscovery/nuclei
Length of output: 598
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 49
🏁 Script executed:
Repository: projectdiscovery/nuclei
Length of output: 119
🏁 Script executed:
# Examine the full test file to see all test cases cat pkg/protocols/common/contextargs/contextargs_test.goRepository: projectdiscovery/nuclei
Length of output: 2137
Normalize excluded port tokens before matching.
templateExcludePortsand CLI-providedcliExcludePortsare not trimmed. Inputs like"80, 443"will create tokens with leading/trailing whitespace that fail to match during port comparison, causing incorrect port-rewrite behavior.This differs from the port parsing in
pkg/protocols/javascript/js.go, which correctly appliesstrings.TrimSpace()to each token. Test coverage also lacks whitespace-padded port cases.💡 Proposed fix
func (ctx *Context) UseNetworkPort(port string, templateExcludePorts string, cliExcludePorts []string) error { ignorePorts := defaultReservedPorts if len(cliExcludePorts) > 0 { - ignorePorts = cliExcludePorts + ignorePorts = normalizePorts(cliExcludePorts) } else if templateExcludePorts != "" { // TODO: add support for service names like http,https,ssh etc once https://github.com/projectdiscovery/netdb is ready - ignorePorts = sliceutil.Dedupe(strings.Split(templateExcludePorts, ",")) + ignorePorts = normalizePorts(strings.Split(templateExcludePorts, ",")) } @@ return nil } + +func normalizePorts(ports []string) []string { + normalized := make([]string, 0, len(ports)) + for _, p := range ports { + p = strings.TrimSpace(p) + if p != "" { + normalized = append(normalized, p) + } + } + return sliceutil.Dedupe(normalized) +}📝 Committable suggestion
🤖 Prompt for AI Agents