Skip to content

Commit

Permalink
chore(*): use sonic wrapper instead
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <[email protected]>
  • Loading branch information
dwisiswant0 committed Feb 7, 2025
1 parent 885fe5b commit ca5b6fa
Show file tree
Hide file tree
Showing 43 changed files with 121 additions and 126 deletions.
6 changes: 3 additions & 3 deletions cmd/integration-test/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"net/http/httptest"
"net/url"

"github.com/bytedance/sonic"
"github.com/julienschmidt/httprouter"
"github.com/projectdiscovery/nuclei/v3/pkg/output"
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
)

const (
Expand Down Expand Up @@ -87,7 +87,7 @@ func (h *fuzzModeOverride) Execute(filePath string) error {
return err
}
var event output.ResultEvent
err = sonic.Unmarshal([]byte(results[0]), &event)
err = json.Unmarshal([]byte(results[0]), &event)
if err != nil {
return fmt.Errorf("could not unmarshal event: %s", err)
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func (h *fuzzTypeOverride) Execute(filePath string) error {
return err
}
var event output.ResultEvent
err = sonic.Unmarshal([]byte(results[0]), &event)
err = json.Unmarshal([]byte(results[0]), &event)
if err != nil {
return fmt.Errorf("could not unmarshal event: %s", err)
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/integration-test/matcher-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"strings"

"github.com/bytedance/sonic"
"github.com/projectdiscovery/nuclei/v3/pkg/output"
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
)

var matcherStatusTestcases = []TestCaseInfo{
Expand All @@ -26,7 +26,7 @@ func (h *httpNoAccess) Execute(filePath string) error {
return err
}
event := &output.ResultEvent{}
_ = sonic.Unmarshal([]byte(results[0]), event)
_ = json.Unmarshal([]byte(results[0]), event)
expectedError := "no address found for host"
if !strings.Contains(event.Error, expectedError) {
return fmt.Errorf("unexpected result: expecting \"%s\" error but got \"%s\"", expectedError, event.Error)
Expand All @@ -43,7 +43,7 @@ func (h *networkNoAccess) Execute(filePath string) error {
return err
}
event := &output.ResultEvent{}
_ = sonic.Unmarshal([]byte(results[0]), event)
_ = json.Unmarshal([]byte(results[0]), event)

if event.Error != "no address found for host" {
return fmt.Errorf("unexpected result: expecting \"no address found for host\" error but got \"%s\"", event.Error)
Expand All @@ -60,7 +60,7 @@ func (h *headlessNoAccess) Execute(filePath string) error {
return err
}
event := &output.ResultEvent{}
_ = sonic.Unmarshal([]byte(results[0]), event)
_ = json.Unmarshal([]byte(results[0]), event)

if event.Error == "" {
return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error)
Expand All @@ -77,7 +77,7 @@ func (h *javascriptNoAccess) Execute(filePath string) error {
return err
}
event := &output.ResultEvent{}
_ = sonic.Unmarshal([]byte(results[0]), event)
_ = json.Unmarshal([]byte(results[0]), event)

if event.Error == "" {
return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error)
Expand All @@ -94,7 +94,7 @@ func (h *websocketNoAccess) Execute(filePath string) error {
return err
}
event := &output.ResultEvent{}
_ = sonic.Unmarshal([]byte(results[0]), event)
_ = json.Unmarshal([]byte(results[0]), event)

if event.Error == "" {
return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error)
Expand All @@ -111,7 +111,7 @@ func (h *dnsNoAccess) Execute(filePath string) error {
return err
}
event := &output.ResultEvent{}
_ = sonic.Unmarshal([]byte(results[0]), event)
_ = json.Unmarshal([]byte(results[0]), event)

if event.Error == "" {
return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error)
Expand Down
4 changes: 2 additions & 2 deletions internal/pdcp/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"sync/atomic"
"time"

"github.com/bytedance/sonic"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v3/pkg/output"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
"github.com/projectdiscovery/retryablehttp-go"
pdcpauth "github.com/projectdiscovery/utils/auth/pdcp"
"github.com/projectdiscovery/utils/env"
Expand Down Expand Up @@ -222,7 +222,7 @@ func (u *UploadWriter) upload(data []byte) error {
return fmt.Errorf("could not upload results got status code %v on %v", resp.StatusCode, resp.Request.URL.String())
}
var uploadResp uploadResponse
if err := sonic.Unmarshal(bin, &uploadResp); err != nil {
if err := json.Unmarshal(bin, &uploadResp); err != nil {
return errorutil.NewWithErr(err).Msgf("could not unmarshal response got %v", string(bin))
}
if uploadResp.ID != "" && u.scanID == "" {
Expand Down
8 changes: 4 additions & 4 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
"sync/atomic"
"time"

"github.com/bytedance/sonic"
"github.com/projectdiscovery/nuclei/v3/internal/pdcp"
"github.com/projectdiscovery/nuclei/v3/pkg/authprovider"
"github.com/projectdiscovery/nuclei/v3/pkg/fuzz/frequency"
"github.com/projectdiscovery/nuclei/v3/pkg/input/provider"
"github.com/projectdiscovery/nuclei/v3/pkg/installer"
"github.com/projectdiscovery/nuclei/v3/pkg/loader/parser"
"github.com/projectdiscovery/nuclei/v3/pkg/scan/events"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
uncoverlib "github.com/projectdiscovery/uncover"
pdcpauth "github.com/projectdiscovery/utils/auth/pdcp"
"github.com/projectdiscovery/utils/env"
Expand Down Expand Up @@ -288,7 +288,7 @@ func New(options *types.Options) (*Runner, error) {
if err != nil {
return nil, err
}
err = sonic.Unmarshal(file, &resumeCfg)
err = json.Unmarshal(file, &resumeCfg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -795,7 +795,7 @@ func (r *Runner) SaveResumeConfig(path string) error {
}
resumeCfgClone := r.resumeCfg.Clone()
resumeCfgClone.ResumeFrom = resumeCfgClone.Current
data, _ := sonic.MarshalIndent(resumeCfgClone, "", "\t")
data, _ := json.MarshalIndent(resumeCfgClone, "", "\t")

return os.WriteFile(path, data, permissionutil.ConfigFilePermission)
}
Expand Down Expand Up @@ -830,7 +830,7 @@ func UploadResultsToCloud(options *types.Options) error {
defer file.Close()

gologger.Info().Msgf("Uploading scan results to cloud dashboard from %s", options.ScanUploadFile)
dec := sonic.ConfigStd.NewDecoder(file)
dec := json.NewDecoder(file)
for dec.More() {
var r output.ResultEvent
err := dec.Decode(&r)
Expand Down
7 changes: 3 additions & 4 deletions pkg/authprovider/authx/dynamic.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package authx

import (
"encoding/json"
"fmt"
"strings"
"sync"

"github.com/bytedance/sonic"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/replacer"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
errorutil "github.com/projectdiscovery/utils/errors"
)

Expand All @@ -35,11 +34,11 @@ type Dynamic struct {
}

func (d *Dynamic) UnmarshalJSON(data []byte) error {
if err := sonic.Unmarshal(data, &d); err != nil {
if err := json.Unmarshal(data, &d); err != nil {
return err
}
var s Secret
if err := sonic.Unmarshal(data, &s); err != nil {
if err := json.Unmarshal(data, &s); err != nil {
return err
}
d.Secret = s
Expand Down
4 changes: 2 additions & 2 deletions pkg/authprovider/authx/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"regexp"
"strings"

"github.com/bytedance/sonic"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
errorutil "github.com/projectdiscovery/utils/errors"
"github.com/projectdiscovery/utils/generic"
stringsutil "github.com/projectdiscovery/utils/strings"
Expand Down Expand Up @@ -245,7 +245,7 @@ func GetAuthDataFromYAML(data []byte) (*Authx, error) {
// GetAuthDataFromJSON reads the auth data from json
func GetAuthDataFromJSON(data []byte) (*Authx, error) {
var auth Authx
err := sonic.Unmarshal(data, &auth)
err := json.Unmarshal(data, &auth)
if err != nil {
return nil, errorutil.NewWithErr(err).Msgf("could not unmarshal json")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/catalog/config/nucleiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"path/filepath"
"strings"

"github.com/bytedance/sonic"
"github.com/projectdiscovery/goflags"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
"github.com/projectdiscovery/utils/env"
errorutil "github.com/projectdiscovery/utils/errors"
fileutil "github.com/projectdiscovery/utils/file"
Expand Down Expand Up @@ -261,7 +261,7 @@ func (c *Config) ReadTemplatesConfig() error {
if err != nil {
return errorutil.NewWithErr(err).Msgf("could not read nuclei config file at %s", c.getTemplatesConfigFilePath())
}
if err := sonic.Unmarshal(bin, &cfg); err != nil {
if err := json.Unmarshal(bin, &cfg); err != nil {
return errorutil.NewWithErr(err).Msgf("could not unmarshal nuclei config file at %s", c.getTemplatesConfigFilePath())
}
// apply config
Expand All @@ -279,7 +279,7 @@ func (c *Config) WriteTemplatesConfig() error {
if err := c.createConfigDirIfNotExists(); err != nil {
return err
}
bin, err := sonic.Marshal(c)
bin, err := json.Marshal(c)
if err != nil {
return errorutil.NewWithErr(err).Msgf("failed to marshal nuclei config")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fuzz/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"regexp"
"strings"

"github.com/bytedance/sonic"
"github.com/pkg/errors"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/fuzz/component"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/expressions"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/generators"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
"github.com/projectdiscovery/retryablehttp-go"
errorutil "github.com/projectdiscovery/utils/errors"
mapsutil "github.com/projectdiscovery/utils/maps"
Expand Down Expand Up @@ -125,7 +125,7 @@ func (rule *Rule) Execute(input *ExecuteRuleInput) (err error) {
finalComponentList = append(finalComponentList, component)
}
if len(displayDebugFuzzPoints) > 0 {
marshalled, _ := sonic.MarshalIndent(displayDebugFuzzPoints, "", " ")
marshalled, _ := json.MarshalIndent(displayDebugFuzzPoints, "", " ")
gologger.Info().Msgf("[%s] Fuzz points for %s [%s]\n%s\n", rule.options.TemplateID, input.Input.MetaInput.Input, input.BaseRequest.Method, string(marshalled))
}

Expand Down
18 changes: 8 additions & 10 deletions pkg/fuzz/type.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package fuzz

import (
"encoding/json"
"fmt"

"github.com/bytedance/sonic"
"github.com/invopop/jsonschema"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
mapsutil "github.com/projectdiscovery/utils/maps"
"gopkg.in/yaml.v2"
)

var (
_ json.Marshaler = &SliceOrMapSlice{}
_ json.Unmarshaler = &SliceOrMapSlice{}
_ json.JSONCodec = &SliceOrMapSlice{}
_ yaml.Marshaler = &SliceOrMapSlice{}
_ yaml.Unmarshaler = &SliceOrMapSlice{}
)
Expand Down Expand Up @@ -71,25 +69,25 @@ func (v SliceOrMapSlice) JSONSchema() *jsonschema.Schema {
return gotType
}

// UnmarshalJSON implements sonic.Unmarshaler interface.
// UnmarshalJSON implements json.Unmarshaler interface.
func (v *SliceOrMapSlice) UnmarshalJSON(data []byte) error {
// try to unmashal as a string and fallback to map
if err := sonic.Unmarshal(data, &v.Value); err == nil {
if err := json.Unmarshal(data, &v.Value); err == nil {
return nil
}
err := sonic.Unmarshal(data, &v.KV)
err := json.Unmarshal(data, &v.KV)
if err != nil {
return fmt.Errorf("object can be a key:value or a string")
}
return nil
}

// MarshalJSON implements sonic.Marshaler interface.
// MarshalJSON implements json.Marshaler interface.
func (v SliceOrMapSlice) MarshalJSON() ([]byte, error) {
if v.KV != nil {
return sonic.Marshal(v.KV)
return json.Marshal(v.KV)
}
return sonic.Marshal(v.Value)
return json.Marshal(v.Value)
}

// UnmarshalYAML implements yaml.Unmarshaler interface.
Expand Down
4 changes: 2 additions & 2 deletions pkg/input/formats/openapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"os"
"strings"

"github.com/bytedance/sonic"
"github.com/clbanning/mxj/v2"
"github.com/getkin/kin-openapi/openapi3"
"github.com/pkg/errors"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/projectdiscovery/nuclei/v3/pkg/input/formats"
httpTypes "github.com/projectdiscovery/nuclei/v3/pkg/input/types"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
errorutil "github.com/projectdiscovery/utils/errors"
"github.com/projectdiscovery/utils/generic"
mapsutil "github.com/projectdiscovery/utils/maps"
Expand Down Expand Up @@ -276,7 +276,7 @@ func generateRequestsFromOp(opts *generateReqOptions) error {
// var body string
switch content {
case "application/json":
if marshalled, err := sonic.Marshal(example); err == nil {
if marshalled, err := json.Marshal(example); err == nil {
// body = string(marshalled)
cloned.Body = io.NopCloser(bytes.NewReader(marshalled))
cloned.ContentLength = int64(len(marshalled))
Expand Down
Loading

0 comments on commit ca5b6fa

Please sign in to comment.