Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/cmd/integration-test/websocket.go
2070 views
1
package main
2
3
import (
4
"net"
5
"strings"
6
7
"github.com/gobwas/ws/wsutil"
8
9
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
10
)
11
12
var websocketTestCases = []TestCaseInfo{
13
{Path: "protocols/websocket/basic.yaml", TestCase: &websocketBasic{}},
14
{Path: "protocols/websocket/cswsh.yaml", TestCase: &websocketCswsh{}},
15
{Path: "protocols/websocket/no-cswsh.yaml", TestCase: &websocketNoCswsh{}},
16
{Path: "protocols/websocket/path.yaml", TestCase: &websocketWithPath{}},
17
}
18
19
type websocketBasic struct{}
20
21
// Execute executes a test case and returns an error if occurred
22
func (h *websocketBasic) Execute(filePath string) error {
23
connHandler := func(conn net.Conn) {
24
for {
25
msg, op, _ := wsutil.ReadClientData(conn)
26
if string(msg) != "hello" {
27
return
28
}
29
_ = wsutil.WriteServerMessage(conn, op, []byte("world"))
30
}
31
}
32
originValidate := func(origin string) bool {
33
return true
34
}
35
ts := testutils.NewWebsocketServer("", connHandler, originValidate)
36
defer ts.Close()
37
38
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)
39
if err != nil {
40
return err
41
}
42
43
return expectResultsCount(results, 1)
44
}
45
46
type websocketCswsh struct{}
47
48
// Execute executes a test case and returns an error if occurred
49
func (h *websocketCswsh) Execute(filePath string) error {
50
connHandler := func(conn net.Conn) {
51
52
}
53
originValidate := func(origin string) bool {
54
return true
55
}
56
ts := testutils.NewWebsocketServer("", connHandler, originValidate)
57
defer ts.Close()
58
59
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)
60
if err != nil {
61
return err
62
}
63
64
return expectResultsCount(results, 1)
65
}
66
67
type websocketNoCswsh struct{}
68
69
// Execute executes a test case and returns an error if occurred
70
func (h *websocketNoCswsh) Execute(filePath string) error {
71
connHandler := func(conn net.Conn) {
72
73
}
74
originValidate := func(origin string) bool {
75
return origin == "https://google.com"
76
}
77
ts := testutils.NewWebsocketServer("", connHandler, originValidate)
78
defer ts.Close()
79
80
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)
81
if err != nil {
82
return err
83
}
84
85
return expectResultsCount(results, 0)
86
}
87
88
type websocketWithPath struct{}
89
90
// Execute executes a test case and returns an error if occurred
91
func (h *websocketWithPath) Execute(filePath string) error {
92
connHandler := func(conn net.Conn) {
93
94
}
95
originValidate := func(origin string) bool {
96
return origin == "https://google.com"
97
}
98
ts := testutils.NewWebsocketServer("/test", connHandler, originValidate)
99
defer ts.Close()
100
101
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)
102
if err != nil {
103
return err
104
}
105
106
return expectResultsCount(results, 0)
107
}
108
109