Giter Site home page Giter Site logo

form's People

Contributors

alxfv avatar deankarn avatar exitialis avatar ngdinhtoan avatar vearutop avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

fourcels

form's Issues

Remove all panics on unexpected data

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.