Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/input/formats/formats.go
2851 views
1
package formats
2
3
import (
4
"errors"
5
"io"
6
"os"
7
"strings"
8
9
"github.com/projectdiscovery/nuclei/v3/pkg/input/types"
10
"github.com/projectdiscovery/retryablehttp-go"
11
fileutil "github.com/projectdiscovery/utils/file"
12
"gopkg.in/yaml.v3"
13
)
14
15
// ParseReqRespCallback is a callback function for discovered raw requests
16
type ParseReqRespCallback func(rr *types.RequestResponse) bool
17
18
// InputFormatOptions contains options for the input
19
// this can be variables that can be passed or
20
// overrides or some other options
21
type InputFormatOptions struct {
22
// Variables is list of variables that can be used
23
// while generating requests in given format
24
Variables map[string]interface{}
25
// SkipFormatValidation is used to skip format validation
26
// while debugging or testing if format is invalid then
27
// requests are skipped instead of creating invalid requests
28
SkipFormatValidation bool
29
// RequiredOnly only uses required fields when generating requests
30
// instead of all fields
31
RequiredOnly bool
32
// VarsTextTemplating uses Variables and inject it into the input
33
// this is used for text templating of variables based on carvel ytt
34
// Only available for Yaml formats
35
VarsTextTemplating bool
36
// VarsFilePaths is the path to the file containing variables
37
VarsFilePaths []string
38
}
39
40
// Format is an interface implemented by all input formats
41
type Format interface {
42
// Name returns the name of the format
43
Name() string
44
// Parse parses the input and calls the provided callback
45
// function for each RawRequest it discovers.
46
Parse(input io.Reader, resultsCb ParseReqRespCallback, filePath string) error
47
// SetOptions sets the options for the input format
48
SetOptions(options InputFormatOptions)
49
}
50
51
// SpecDownloader is an interface for downloading API specifications from URLs
52
type SpecDownloader interface {
53
// Download downloads the spec from the given URL and saves it to tmpDir
54
// Returns the path to the downloaded file
55
// httpClient is a retryablehttp.Client instance (can be nil for fallback)
56
Download(url, tmpDir string, httpClient *retryablehttp.Client) (string, error)
57
// SupportedExtensions returns the list of supported file extensions
58
SupportedExtensions() []string
59
}
60
61
var (
62
DefaultVarDumpFileName = "required_openapi_params.yaml"
63
ErrNoVarsDumpFile = errors.New("no required params file found")
64
)
65
66
// == OpenAPIParamsCfgFile ==
67
// this file is meant to be used in CLI mode
68
// to be more interactive and user-friendly when
69
// running nuclei with openapi format
70
71
// OpenAPIParamsCfgFile is the structure of the required vars dump file
72
type OpenAPIParamsCfgFile struct {
73
Var []string `yaml:"var"`
74
OptionalVars []string `yaml:"-"` // this will be written to the file as comments
75
}
76
77
// ReadOpenAPIVarDumpFile reads the required vars dump file
78
func ReadOpenAPIVarDumpFile() (*OpenAPIParamsCfgFile, error) {
79
var vars OpenAPIParamsCfgFile
80
if !fileutil.FileExists(DefaultVarDumpFileName) {
81
return nil, ErrNoVarsDumpFile
82
}
83
bin, err := os.ReadFile(DefaultVarDumpFileName)
84
if err != nil {
85
return nil, err
86
}
87
err = yaml.Unmarshal(bin, &vars)
88
if err != nil {
89
return nil, err
90
}
91
filtered := []string{}
92
for _, v := range vars.Var {
93
v = strings.TrimSpace(v)
94
if !strings.HasSuffix(v, "=") {
95
filtered = append(filtered, v)
96
}
97
}
98
vars.Var = filtered
99
return &vars, nil
100
}
101
102
// WriteOpenAPIVarDumpFile writes the required vars dump file
103
func WriteOpenAPIVarDumpFile(vars *OpenAPIParamsCfgFile) error {
104
f, err := os.OpenFile(DefaultVarDumpFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
105
if err != nil {
106
return err
107
}
108
defer func() {
109
_ = f.Close()
110
}()
111
bin, err := yaml.Marshal(vars)
112
if err != nil {
113
return err
114
}
115
_, _ = f.Write(bin)
116
if len(vars.OptionalVars) > 0 {
117
_, _ = f.WriteString("\n # Optional parameters\n")
118
for _, v := range vars.OptionalVars {
119
_, _ = f.WriteString(" # - " + v + "=\n")
120
}
121
}
122
return f.Sync()
123
}
124
125