-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(input): add per-target JSONL filters #7597
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
3nesdeniz
wants to merge
7
commits into
projectdiscovery:dev
Choose a base branch
from
3nesdeniz:agent/jsonl-target-filters
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e2cee24
feat(input): add per-target JSONL filters
3nesdeniz aaa930a
Merge remote-tracking branch 'upstream/dev' into agent/jsonl-target-f…
3nesdeniz 70d9355
Merge remote-tracking branch 'upstream/dev' into agent/jsonl-target-f…
3nesdeniz 356c56a
Merge remote-tracking branch 'upstream/dev' into agent/jsonl-target-f…
3nesdeniz 084d83b
fix(input): normalize per-target template paths and document fixed ex…
3nesdeniz eb1b14c
test(input): assert normalized filter collections and forced-include …
3nesdeniz 4023a4d
fix(input): contain per-target JSONL template selectors to templates dir
3nesdeniz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/projectdiscovery/nuclei/v3/pkg/input/provider" | ||
| inputtypes "github.com/projectdiscovery/nuclei/v3/pkg/input/types" | ||
| "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs" | ||
| mapsutil "github.com/projectdiscovery/utils/maps" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type preflightInputProviderStub struct { | ||
| inputs []*contextargs.MetaInput | ||
| } | ||
|
|
||
| func (p *preflightInputProviderStub) Count() int64 { | ||
| return int64(len(p.inputs)) | ||
| } | ||
|
|
||
| func (p *preflightInputProviderStub) Iterate(callback func(*contextargs.MetaInput) bool) { | ||
| for _, input := range p.inputs { | ||
| if !callback(input) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (*preflightInputProviderStub) Set(string, string) {} | ||
| func (*preflightInputProviderStub) SetWithProbe(string, string, inputtypes.InputLivenessProbe) error { | ||
| return nil | ||
| } | ||
| func (*preflightInputProviderStub) SetWithExclusions(string, string) error { return nil } | ||
| func (*preflightInputProviderStub) InputType() string { return provider.TargetInputProvider } | ||
| func (*preflightInputProviderStub) Close() {} | ||
|
|
||
| func TestPreflightInputKeyPreservesLegacyMarshalString(t *testing.T) { | ||
| input := contextargs.NewMetaInput() | ||
| input.Input = "https://example.com" | ||
| input.CustomIP = "192.0.2.1" | ||
|
|
||
| marshaled, err := input.MarshalString() | ||
| require.NoError(t, err) | ||
|
|
||
| key, err := preflightInputKey(input) | ||
| require.NoError(t, err) | ||
| require.Equal(t, marshaled, key) | ||
| } | ||
|
|
||
| func TestPreflightInputKeySeparatesPresenceAwareFilters(t *testing.T) { | ||
| omitted := newPreflightFilteredInput(&contextargs.TargetFilter{}) | ||
| explicitEmpty := newPreflightFilteredInput(&contextargs.TargetFilter{ | ||
| HasTags: true, | ||
| Tags: []string{}, | ||
| }) | ||
|
|
||
| // TargetFilter's Has* fields are intentionally not serialized, so the old | ||
| // MarshalString key could not distinguish these two JSONL records. | ||
| omittedMarshaled, err := omitted.MarshalString() | ||
| require.NoError(t, err) | ||
| explicitEmptyMarshaled, err := explicitEmpty.MarshalString() | ||
| require.NoError(t, err) | ||
| require.Equal(t, omittedMarshaled, explicitEmptyMarshaled) | ||
|
|
||
| omittedKey, err := preflightInputKey(omitted) | ||
| require.NoError(t, err) | ||
| explicitEmptyKey, err := preflightInputKey(explicitEmpty) | ||
| require.NoError(t, err) | ||
| require.NotEqual(t, omittedKey, explicitEmptyKey) | ||
|
|
||
| // Semantically equivalent filter lists produce one stable canonical key, | ||
| // independent of input ordering. | ||
| firstOrdering := newPreflightFilteredInput(&contextargs.TargetFilter{ | ||
| HasTags: true, | ||
| Tags: []string{"beta", "alpha"}, | ||
| }) | ||
| secondOrdering := newPreflightFilteredInput(&contextargs.TargetFilter{ | ||
| HasTags: true, | ||
| Tags: []string{"alpha", "beta"}, | ||
| }) | ||
| firstKey, err := preflightInputKey(firstOrdering) | ||
| require.NoError(t, err) | ||
| secondKey, err := preflightInputKey(secondOrdering) | ||
| require.NoError(t, err) | ||
| require.Equal(t, firstKey, secondKey) | ||
|
|
||
| allowed := mapsutil.NewSyncLockMap[string, struct{}]() | ||
| require.NoError(t, allowed.Set(omittedKey, struct{}{})) | ||
| require.NoError(t, allowed.Set(explicitEmptyKey, struct{}{})) | ||
|
|
||
| filtered := &filteringInputProvider{ | ||
| base: &preflightInputProviderStub{inputs: []*contextargs.MetaInput{omitted, explicitEmpty}}, | ||
| allowed: allowed, | ||
| allowCnt: 2, | ||
| execID: "test", | ||
| } | ||
| var iterated []*contextargs.MetaInput | ||
| filtered.Iterate(func(input *contextargs.MetaInput) bool { | ||
| iterated = append(iterated, input) | ||
| return true | ||
| }) | ||
| require.EqualValues(t, 2, filtered.Count()) | ||
| require.Len(t, iterated, 2) | ||
| } | ||
|
|
||
| func TestFilteringInputProviderCountsKeptInputsNotAllowedKeys(t *testing.T) { | ||
| first := contextargs.NewMetaInput() | ||
| first.Input = "https://duplicate.example" | ||
| second := contextargs.NewMetaInput() | ||
| second.Input = first.Input | ||
|
|
||
| key, err := preflightInputKey(first) | ||
| require.NoError(t, err) | ||
| allowed := mapsutil.NewSyncLockMap[string, struct{}]() | ||
| require.NoError(t, allowed.Set(key, struct{}{})) | ||
| require.Len(t, allowed.GetAll(), 1) | ||
| require.EqualValues(t, 2, countAllowedPreflightInputs([]preflightTarget{ | ||
| {key: key}, | ||
| {key: key}, | ||
| }, allowed)) | ||
|
|
||
| filtered := &filteringInputProvider{ | ||
| base: &preflightInputProviderStub{inputs: []*contextargs.MetaInput{first, second}}, | ||
| allowed: allowed, | ||
| allowCnt: 2, | ||
| execID: "test", | ||
| } | ||
| var iterated int | ||
| filtered.Iterate(func(*contextargs.MetaInput) bool { | ||
| iterated++ | ||
| return true | ||
| }) | ||
|
|
||
| require.EqualValues(t, 2, filtered.Count()) | ||
| require.Equal(t, 2, iterated) | ||
| } | ||
|
|
||
| func newPreflightFilteredInput(filter *contextargs.TargetFilter) *contextargs.MetaInput { | ||
| input := contextargs.NewMetaInput() | ||
| input.Input = "https://duplicate.example" | ||
| input.TargetFilter = filter | ||
| return 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
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.
Uh oh!
There was an error while loading. Please reload this page.