2021-07-12 17:20:01 +03:00
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2021-08-17 13:46:53 +03:00
|
|
|
"encoding/json"
|
2021-08-03 14:51:34 +03:00
|
|
|
"fmt"
|
2021-08-17 13:46:53 +03:00
|
|
|
"gopkg.in/yaml.v2"
|
2021-07-19 21:04:08 +03:00
|
|
|
"strings"
|
|
|
|
|
|
2021-07-16 17:28:13 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/internal/severity"
|
2021-08-03 15:39:15 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
|
2021-07-12 17:20:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Info struct {
|
2021-08-17 13:46:53 +03:00
|
|
|
Name string `json:"name" yaml:"name"`
|
|
|
|
|
Authors StringSlice `json:"authors" yaml:"authors"`
|
|
|
|
|
Tags StringSlice `json:"tags" yaml:"tags"`
|
|
|
|
|
Description string `json:"description" yaml:"description"`
|
|
|
|
|
Reference StringSlice `json:"reference" yaml:"reference"`
|
|
|
|
|
SeverityHolder severity.SeverityHolder `json:"severity" yaml:"severity"`
|
2021-07-12 17:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 14:51:34 +03:00
|
|
|
// StringSlice represents a single (in-lined) or multiple string value(s).
|
|
|
|
|
// The unmarshaller does not automatically convert in-lined strings to []string, hence the interface{} type is required.
|
2021-07-12 17:20:01 +03:00
|
|
|
type StringSlice struct {
|
|
|
|
|
Value interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (stringSlice *StringSlice) IsEmpty() bool {
|
2021-08-03 14:51:34 +03:00
|
|
|
return len(stringSlice.ToSlice()) == 0
|
2021-07-12 17:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (stringSlice StringSlice) ToSlice() []string {
|
|
|
|
|
switch value := stringSlice.Value.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
return []string{value}
|
|
|
|
|
case []string:
|
|
|
|
|
return value
|
2021-07-15 13:41:41 +03:00
|
|
|
case nil:
|
|
|
|
|
return []string{}
|
2021-08-03 14:51:34 +03:00
|
|
|
default:
|
|
|
|
|
panic(fmt.Sprintf("Unexpected StringSlice type: '%T'", value))
|
2021-07-12 17:20:01 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (stringSlice *StringSlice) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2021-07-15 13:41:41 +03:00
|
|
|
marshalledSlice, err := marshalStringToSlice(unmarshal)
|
2021-07-12 17:20:01 +03:00
|
|
|
if err != nil {
|
2021-07-15 13:41:41 +03:00
|
|
|
return err
|
2021-07-12 17:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
2021-07-15 13:41:41 +03:00
|
|
|
result := make([]string, len(marshalledSlice))
|
2021-08-03 15:39:15 +03:00
|
|
|
//nolint:gosimple,nolintlint //cannot be replaced with result = append(result, slices...) because the values are being normalized
|
2021-07-15 13:41:41 +03:00
|
|
|
for _, value := range marshalledSlice {
|
2021-08-03 14:51:34 +03:00
|
|
|
result = append(result, strings.ToLower(strings.TrimSpace(value))) // TODO do we need to introduce RawStringSlice and/or NormalizedStringSlices?
|
2021-07-12 17:20:01 +03:00
|
|
|
}
|
|
|
|
|
stringSlice.Value = result
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 13:41:41 +03:00
|
|
|
func marshalStringToSlice(unmarshal func(interface{}) error) ([]string, error) {
|
|
|
|
|
var marshalledValueAsString string
|
|
|
|
|
var marshalledValuesAsSlice []string
|
|
|
|
|
|
|
|
|
|
sliceMarshalError := unmarshal(&marshalledValuesAsSlice)
|
|
|
|
|
if sliceMarshalError != nil {
|
|
|
|
|
stringMarshalError := unmarshal(&marshalledValueAsString)
|
|
|
|
|
if stringMarshalError != nil {
|
|
|
|
|
return nil, stringMarshalError
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result []string
|
|
|
|
|
if len(marshalledValuesAsSlice) > 0 {
|
|
|
|
|
result = marshalledValuesAsSlice
|
2021-08-03 14:51:34 +03:00
|
|
|
} else if utils.IsNotBlank(marshalledValueAsString) {
|
2021-07-15 13:41:41 +03:00
|
|
|
result = strings.Split(marshalledValueAsString, ",")
|
|
|
|
|
} else {
|
|
|
|
|
result = []string{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 17:20:01 +03:00
|
|
|
func (stringSlice StringSlice) MarshalYAML() (interface{}, error) {
|
2021-08-17 13:46:53 +03:00
|
|
|
return yaml.Marshal(stringSlice.Value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (stringSlice StringSlice) MarshalJSON() ([]byte, error) {
|
|
|
|
|
return json.Marshal(stringSlice.Value)
|
2021-07-12 17:20:01 +03:00
|
|
|
}
|