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

fix: data race at protocolstate, contextargs & some outdated test cases #5820

Merged
merged 10 commits into from
Nov 19, 2024
37 changes: 18 additions & 19 deletions pkg/tmplexec/flow/flow_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestFlowWithConditionPositive(t *testing.T) {
err = Template.Executer.Compile()
require.Nil(t, err, "could not compile template")

input := contextargs.NewWithInput(context.Background(), "blog.projectdiscovery.io")
input := contextargs.NewWithInput(context.Background(), "https://cloud.projectdiscovery.io")
ctx := scan.NewScanContext(context.Background(), input)
// positive match . expect results also verify that both dns() and http() were executed
gotresults, err := Template.Executer.Execute(ctx)
Expand All @@ -150,36 +150,35 @@ func TestFlowWithNoMatchers(t *testing.T) {
// when using conditional flow with no matchers at all
// we implicitly assume that request was successful and internally changed the result to true (for scope of condition only)

// testcase-1 : no matchers but contains extractor
Template, err := templates.Parse("testcases/condition-flow-extractors.yaml", nil, executerOpts)
Template, err := templates.Parse("testcases/condition-flow-no-operators.yaml", nil, executerOpts)
require.Nil(t, err, "could not parse template")

require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not

err = Template.Executer.Compile()
require.Nil(t, err, "could not compile template")

input := contextargs.NewWithInput(context.Background(), "blog.projectdiscovery.io")
ctx := scan.NewScanContext(context.Background(), input)
anotherInput := contextargs.NewWithInput(context.Background(), "http://scanme.sh")
anotherCtx := scan.NewScanContext(context.Background(), anotherInput)
// positive match . expect results also verify that both dns() and http() were executed
gotresults, err := Template.Executer.Execute(ctx)
gotresults, err := Template.Executer.Execute(anotherCtx)
require.Nil(t, err, "could not execute template")
require.True(t, gotresults)

// testcase-2 : no matchers and no extractors
Template, err = templates.Parse("testcases/condition-flow-no-operators.yaml", nil, executerOpts)
require.Nil(t, err, "could not parse template")

require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not
t.Run("Contains Extractor", func(t *testing.T) {
Template, err := templates.Parse("testcases/condition-flow-extractors.yaml", nil, executerOpts)
require.Nil(t, err, "could not parse template")

err = Template.Executer.Compile()
require.Nil(t, err, "could not compile template")
require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not

anotherInput := contextargs.NewWithInput(context.Background(), "blog.projectdiscovery.io")
anotherCtx := scan.NewScanContext(context.Background(), anotherInput)
// positive match . expect results also verify that both dns() and http() were executed
gotresults, err = Template.Executer.Execute(anotherCtx)
require.Nil(t, err, "could not execute template")
require.True(t, gotresults)
err = Template.Executer.Compile()
require.Nil(t, err, "could not compile template")

input := contextargs.NewWithInput(context.Background(), "http://scanme.sh")
ctx := scan.NewScanContext(context.Background(), input)
// positive match . expect results also verify that both dns() and http() were executed
gotresults, err := Template.Executer.Execute(ctx)
require.Nil(t, err, "could not execute template")
require.True(t, gotresults)
})
}
17 changes: 8 additions & 9 deletions pkg/tmplexec/flow/testcases/condition-flow-extractors.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
id: ghost-blog-detection
id: condition-flow-extractors
info:
name: Ghost blog detection
name: Condition Flow Extractors
author: pdteam
severity: info


flow: dns() && http()

dns:
- name: "{{FQDN}}"
type: CNAME
type: A

extractors:
- type: dsl
name: cname
name: a
internal: true
dsl:
- cname
- a

http:
- method: GET
path:
- "{{BaseURL}}?ref={{cname}}"
- "{{BaseURL}}/?ref={{a}}"

matchers:
- type: word
words:
- "ghost.io"
- "ok"
12 changes: 5 additions & 7 deletions pkg/tmplexec/flow/testcases/condition-flow-no-operators.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
id: ghost-blog-detection
id: condition-flow-no-operators
info:
name: Ghost blog detection
name: Condition Flow No Operators
author: pdteam
severity: info


flow: dns() && http()


dns:
- name: "{{FQDN}}"
type: CNAME

http:
- method: GET
path:
- "{{BaseURL}}?ref={{dns_cname}}"
- "{{BaseURL}}/?ref={{dns_cname}}"

matchers:
- type: word
words:
- "ghost.io"
- "ok"
10 changes: 5 additions & 5 deletions pkg/tmplexec/flow/testcases/condition-flow.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id: ghost-blog-detection
id: vercel-hosted-detection
info:
name: Ghost blog detection
name: Vercel-hosted detection
author: pdteam
severity: info

Expand All @@ -14,14 +14,14 @@ dns:
matchers:
- type: word
words:
- "ghost.io"
- "vercel-dns"

http:
- method: GET
path:
- "{{BaseURL}}"
- "{{dns_cname}}"

matchers:
- type: word
words:
- "ghost.io"
- "DEPLOYMENT_NOT_FOUND"
4 changes: 2 additions & 2 deletions pkg/tmplexec/multiproto/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestMultiProtoWithDynamicExtractor(t *testing.T) {
err = Template.Executer.Compile()
require.Nil(t, err, "could not compile template")

input := contextargs.NewWithInput(context.Background(), "blog.projectdiscovery.io")
input := contextargs.NewWithInput(context.Background(), "http://scanme.sh")
ctx := scan.NewScanContext(context.Background(), input)
gotresults, err := Template.Executer.Execute(ctx)
require.Nil(t, err, "could not execute template")
Expand All @@ -72,7 +72,7 @@ func TestMultiProtoWithProtoPrefix(t *testing.T) {
err = Template.Executer.Compile()
require.Nil(t, err, "could not compile template")

input := contextargs.NewWithInput(context.Background(), "blog.projectdiscovery.io")
input := contextargs.NewWithInput(context.Background(), "https://cloud.projectdiscovery.io/sign-in")
ctx := scan.NewScanContext(context.Background(), input)
gotresults, err := Template.Executer.Execute(ctx)
require.Nil(t, err, "could not execute template")
Expand Down
14 changes: 3 additions & 11 deletions pkg/tmplexec/multiproto/testcases/multiprotodynamic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ info:

dns:
- name: "{{FQDN}}" # DNS Request
type: cname

extractors:
- type: dsl
name: blogid
dsl:
- trim_suffix(cname,'.ghost.io')
internal: true

type: a

http:
- method: GET # http request
Expand All @@ -25,6 +17,6 @@ http:
matchers:
- type: dsl
dsl:
- contains(body,'ProjectDiscovery.io') # check for http string
- blogid == 'projectdiscovery' # check for cname (extracted information from dns response)
- body == "ok"
- dns_a == '128.199.158.128' # check for A record (extracted information from dns response)
condition: and
6 changes: 3 additions & 3 deletions pkg/tmplexec/multiproto/testcases/multiprotowithprefix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ http:
matchers:
- type: dsl
dsl:
- contains(http_body,'ProjectDiscovery.io') # check for http string
- trim_suffix(dns_cname,'.ghost.io') == 'projectdiscovery' # check for cname (extracted information from dns response)
- ssl_subject_cn == 'blog.projectdiscovery.io'
- contains(http_body, 'ProjectDiscovery Cloud Platform') # check for http string
- dns_cname == 'cname.vercel-dns.com' # check for cname (extracted information from dns response)
- ssl_subject_cn == 'cloud.projectdiscovery.io'
condition: and
Loading