Skip to content

Remove all panics on unexpected data #4

Closed
@vearutop

Description

@vearutop

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/[email protected]/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/[email protected]/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/[email protected]/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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions