Path: blob/dev/pkg/input/formats/yaml/multidoc.go
2070 views
package yaml12import (3"io"4"strings"56"github.com/pkg/errors"7"github.com/projectdiscovery/gologger"8"github.com/projectdiscovery/nuclei/v3/pkg/input/formats"9"github.com/projectdiscovery/nuclei/v3/pkg/input/types"10YamlUtil "gopkg.in/yaml.v3"11)1213// YamlMultiDocFormat is a Yaml format parser for nuclei14// input HTTP requests with multiple documents separated by ---15type YamlMultiDocFormat struct {16opts formats.InputFormatOptions17}1819// New creates a new JSON format parser20func New() *YamlMultiDocFormat {21return &YamlMultiDocFormat{}22}2324var _ formats.Format = &YamlMultiDocFormat{}2526// proxifyRequest is a request for proxify27type proxifyRequest struct {28URL string `json:"url"`29Request struct {30Header map[string]string `json:"header"`31Body string `json:"body"`32Raw string `json:"raw"`33} `json:"request"`34}3536// Name returns the name of the format37func (j *YamlMultiDocFormat) Name() string {38return "yaml"39}4041func (j *YamlMultiDocFormat) SetOptions(options formats.InputFormatOptions) {42j.opts = options43}4445// Parse parses the input and calls the provided callback46// function for each RawRequest it discovers.47func (j *YamlMultiDocFormat) Parse(input io.Reader, resultsCb formats.ParseReqRespCallback, filePath string) error {48decoder := YamlUtil.NewDecoder(input)49for {50var request proxifyRequest51err := decoder.Decode(&request)52if err == io.EOF {53break54}55if err != nil {56return errors.Wrap(err, "could not decode json file")57}58if strings.TrimSpace(request.Request.Raw) == "" {59continue60}6162rawRequest, err := types.ParseRawRequestWithURL(request.Request.Raw, request.URL)63if err != nil {64gologger.Warning().Msgf("multidoc-yaml: Could not parse raw request %s: %s\n", request.URL, err)65continue66}67resultsCb(rawRequest)68}69return nil70}717273