Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all panics on unexpected data #4

Closed
vearutop opened this issue Jun 4, 2021 · 0 comments · Fixed by #5
Closed

Remove all panics on unexpected data #4

vearutop opened this issue Jun 4, 2021 · 0 comments · Fixed by #5

Comments

@vearutop
Copy link
Member

vearutop commented Jun 4, 2021

Having invalid data in decoder input is not something exceptional, very often decoder input is provided by an untrusted external party. Invalid data should be handled with errors, not panics.

Discovered with a fuzz test.

// +build gofuzzbeta

package request_test

import (
	"context"
	"net/http"
	"testing"

	"github.com/swaggest/rest/request"
)

func FuzzParseQuery(f *testing.F) {
	df := request.NewDecoderFactory()

	input := new(struct {
		InQuery map[int]float64 `query:"in_query"`
	})
	dec := df.MakeDecoder(http.MethodGet, input, nil)

	f.Add("/?in_query[1]=1.0&in_query[2]=2.1&in_query[3]=0")
	f.Fuzz(func(t *testing.T, queryStr string) {
		req, err := http.NewRequestWithContext(context.Background(), http.MethodGet,
			queryStr, nil)
		if err != nil {
			t.Fatalf("ParseQuery failed to decode a valid encoded query %s: %v", queryStr, err)
		}

		err = dec.Decode(req, &input, nil)
		if err != nil {
			t.Skip()
		}
	})
}
gotip test --fuzz=FuzzParseQuery
2021/06/04 12:13:18 invalid formatting for key 'in_quer[1]]' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'inWque]y[1r' missing '[' bracket
found a crash, minimizing...
2021/06/04 12:13:18 invalid formatting for key 'inWque]y[1r' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'inWque]y' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'inWque]' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'inWque]' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'nWque]' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'Wque]' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'que]' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'ue]' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key 'e]' missing '[' bracket
2021/06/04 12:13:18 invalid formatting for key ']' missing '[' bracket
fuzzing, elapsed: 1.0s, execs: 13 (12/sec), workers: 8, interesting: 2
--- FAIL: FuzzParseQuery (1.05s)
        panic: invalid formatting for key ']' missing '[' bracket
        goroutine 59 [running]:
        runtime/debug.Stack()
                /Users/viacheslavpoturaev/sdk/gotip/src/runtime/debug/stack.go:24 +0x90
        testing.tRunner.func1.2({0x14d85c0, 0xc000154d40})
                /Users/viacheslavpoturaev/sdk/gotip/src/testing/testing.go:1271 +0x267
        testing.tRunner.func1()
                /Users/viacheslavpoturaev/sdk/gotip/src/testing/testing.go:1278 +0x218
        panic({0x14d85c0, 0xc000154d40})
                /Users/viacheslavpoturaev/sdk/gotip/src/runtime/panic.go:1038 +0x215
        log.Panicf({0x1573104, 0x1}, {0xc0000b6eb8, 0x0, 0x0})
                /Users/viacheslavpoturaev/sdk/gotip/src/log/log.go:361 +0x74
        github.com/swaggest/form/v5.(*decoder).parseMapData(0xc0000a46c0)
                /Users/viacheslavpoturaev/go/pkg/mod/github.com/swaggest/form/v5@v5.0.0/decoder.go:80 +0x4bd
        github.com/swaggest/form/v5.(*decoder).setFieldByType(0xc0000a46c0, {0x14cf860, 0xc0000a2098, 0x1943108}, 0x0, {0xc0000a0740, 0x0, 0x40}, 0x0)
                /Users/viacheslavpoturaev/go/pkg/mod/github.com/swaggest/form/v5@v5.0.0/decoder.go:705 +0x3615
        github.com/swaggest/form/v5.(*Decoder).Decode(0xc0000ecf00, {0x14c7e60, 0xc0000a2098}, 0xc000162c30, {0x0, 0x0, 0x1551a00})
                /Users/viacheslavpoturaev/go/pkg/mod/github.com/swaggest/form/v5@v5.0.0/form_decoder.go:167 +0x628
        github.com/swaggest/rest/request.makeDecoder.func1(0x0, {0x14c7e60, 0xc0000a2098}, {0x0, 0x0})
                /Users/viacheslavpoturaev/dev/rest/request/decoder.go:40 +0x109
        github.com/swaggest/rest/request.(*decoder).Decode(0xc0001629f0, 0xc0000a6008, {0x14c7e60, 0xc0000a2098}, {0x0, 0x0})
                /Users/viacheslavpoturaev/dev/rest/request/decoder.go:55 +0xfe
        github.com/swaggest/rest/request_test.FuzzParseQuery.func1(0xc000283040, {0xc00015e7c6, 0x2})
                /Users/viacheslavpoturaev/dev/rest/request/decoder_fuzz_test.go:28 +0x184
        reflect.Value.call({0x14dc880, 0xc0000deb60, 0x13}, {0x155d746, 0x4}, {0xc000162bd0, 0x2, 0x2})
                /Users/viacheslavpoturaev/sdk/gotip/src/reflect/value.go:543 +0xf0d
        reflect.Value.Call({0x14dc880, 0xc0000deb60, 0xc00015e7c8}, {0xc000162bd0, 0x2, 0x2})
                /Users/viacheslavpoturaev/sdk/gotip/src/reflect/value.go:339 +0x13e
        testing.(*F).Fuzz.func1.1(0x0)
                /Users/viacheslavpoturaev/sdk/gotip/src/testing/fuzz.go:347 +0x193
        testing.tRunner(0xc000283040, 0xc000122930)
                /Users/viacheslavpoturaev/sdk/gotip/src/testing/testing.go:1325 +0x102
        created by testing.(*F).Fuzz.func1
                /Users/viacheslavpoturaev/sdk/gotip/src/testing/fuzz.go:341 +0x545
        
        --- FAIL: FuzzParseQuery (0.00s)
    
    Crash written to testdata/corpus/FuzzParseQuery/d54b2cdb8e76ab6be16c0a5d935ea8ff855df4018291b84bf05f54759ac0b96c
    To re-run:
    go test github.com/swaggest/rest/request -run=FuzzParseQuery/d54b2cdb8e76ab6be16c0a5d935ea8ff855df4018291b84bf05f54759ac0b96c
FAIL
exit status 1
FAIL    github.com/swaggest/rest/request        1.274s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant