Path: blob/dev/pkg/input/formats/yaml/multidoc.go
2852 views
package yaml12import (3"bytes"4"io"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 {48finalInput := input4950// Apply text templating if enabled51if j.opts.VarsTextTemplating {52data, err := io.ReadAll(input)53if err != nil {54return errors.Wrap(err, "could not read input")55}56tpl := []string{string(data)}57dvs := mapToKeyValueSlice(j.opts.Variables)58finalData, err := ytt(tpl, dvs, j.opts.VarsFilePaths)59if err != nil {60return errors.Wrap(err, "could not apply ytt templating")61}62finalInput = bytes.NewReader(finalData)63}6465decoder := YamlUtil.NewDecoder(finalInput)66for {67var request proxifyRequest68if err := decoder.Decode(&request); err != nil {69if err == io.EOF {70break71}72return errors.Wrap(err, "could not decode yaml file")73}7475raw := request.Request.Raw76if raw == "" {77continue78}7980rawRequest, err := types.ParseRawRequestWithURL(raw, request.URL)81if err != nil {82gologger.Warning().Msgf("multidoc-yaml: Could not parse raw request %s: %s", request.URL, err)83continue84}85resultsCb(rawRequest)86}87return nil88}899091