github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/http/signature.go (about)

     1  package http
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  
     7  	"github.com/alecthomas/jsonschema"
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/signer"
    11  )
    12  
    13  // SignatureType is the type of signature
    14  type SignatureType int
    15  
    16  // Supported values for the SignatureType
    17  const (
    18  	AWSSignature SignatureType = iota + 1
    19  	signatureLimit
    20  )
    21  
    22  // signatureTypeMappings is a table for conversion of signature type from string.
    23  var signatureTypeMappings = map[SignatureType]string{
    24  	AWSSignature: "AWS",
    25  }
    26  
    27  func GetSupportedSignaturesTypes() []SignatureType {
    28  	var result []SignatureType
    29  	for index := SignatureType(1); index < signatureLimit; index++ {
    30  		result = append(result, index)
    31  	}
    32  	return result
    33  }
    34  
    35  func toSignatureType(valueToMap string) (SignatureType, error) {
    36  	normalizedValue := normalizeValue(valueToMap)
    37  	for key, currentValue := range signatureTypeMappings {
    38  		if normalizedValue == currentValue {
    39  			return key, nil
    40  		}
    41  	}
    42  	return -1, errors.New("invalid signature type: " + valueToMap)
    43  }
    44  
    45  func (t SignatureType) String() string {
    46  	return signatureTypeMappings[t]
    47  }
    48  
    49  // SignatureTypeHolder is used to hold internal type of the signature
    50  type SignatureTypeHolder struct {
    51  	Value SignatureType
    52  }
    53  
    54  func (holder SignatureTypeHolder) JSONSchemaType() *jsonschema.Type {
    55  	gotType := &jsonschema.Type{
    56  		Type:        "string",
    57  		Title:       "type of the signature",
    58  		Description: "Type of the signature",
    59  	}
    60  	for _, types := range GetSupportedSignaturesTypes() {
    61  		gotType.Enum = append(gotType.Enum, types.String())
    62  	}
    63  	return gotType
    64  }
    65  
    66  func (holder *SignatureTypeHolder) UnmarshalYAML(unmarshal func(interface{}) error) error {
    67  	var marshalledTypes string
    68  	if err := unmarshal(&marshalledTypes); err != nil {
    69  		return err
    70  	}
    71  
    72  	computedType, err := toSignatureType(marshalledTypes)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	holder.Value = computedType
    78  	return nil
    79  }
    80  
    81  func (holder *SignatureTypeHolder) UnmarshalJSON(data []byte) error {
    82  	s := strings.Trim(string(data), `"`)
    83  	if s == "" {
    84  		return nil
    85  	}
    86  	computedType, err := toSignatureType(s)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	holder.Value = computedType
    92  	return nil
    93  }
    94  
    95  func (holder SignatureTypeHolder) MarshalJSON() ([]byte, error) {
    96  	return json.Marshal(holder.Value.String())
    97  }
    98  
    99  func (holder SignatureTypeHolder) MarshalYAML() (interface{}, error) {
   100  	return holder.Value.String(), nil
   101  }
   102  
   103  var ErrNoIgnoreList = errors.New("unknown signature types")
   104  
   105  // GetVariablesNamesSkipList depending on the signature type
   106  func GetVariablesNamesSkipList(signature SignatureType) map[string]interface{} {
   107  	switch signature {
   108  	case AWSSignature:
   109  		return signer.AwsSkipList
   110  	default:
   111  		return nil
   112  	}
   113  }
   114  
   115  // GetDefaultSignerVars returns the default signer variables
   116  func GetDefaultSignerVars(signatureType SignatureType) map[string]interface{} {
   117  	if signatureType == AWSSignature {
   118  		return signer.AwsDefaultVars
   119  	}
   120  	return map[string]interface{}{}
   121  }