From 067948a158bc31f0863724ae2bfb83120792cac5 Mon Sep 17 00:00:00 2001 From: AdamKorcz Date: Fri, 29 Jul 2022 15:04:23 +0100 Subject: [PATCH] Add fuzzer Signed-off-by: AdamKorcz --- .clusterfuzzlite/Dockerfile | 4 ++++ .clusterfuzzlite/build.sh | 6 ++++++ .clusterfuzzlite/project.yaml | 1 + .github/workflows/cflite.yml | 29 ++++++++++++++++++++++++++ unmarshal_fuzz.go | 39 +++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 .clusterfuzzlite/Dockerfile create mode 100755 .clusterfuzzlite/build.sh create mode 100644 .clusterfuzzlite/project.yaml create mode 100644 .github/workflows/cflite.yml create mode 100644 unmarshal_fuzz.go diff --git a/.clusterfuzzlite/Dockerfile b/.clusterfuzzlite/Dockerfile new file mode 100644 index 000000000..24c75a550 --- /dev/null +++ b/.clusterfuzzlite/Dockerfile @@ -0,0 +1,4 @@ +FROM gcr.io/oss-fuzz-base/base-builder-go +COPY . $SRC/ +WORKDIR colly +COPY .clusterfuzzlite/build.sh $SRC/ diff --git a/.clusterfuzzlite/build.sh b/.clusterfuzzlite/build.sh new file mode 100755 index 000000000..f1c823da2 --- /dev/null +++ b/.clusterfuzzlite/build.sh @@ -0,0 +1,6 @@ +#!/bin/bash -eu + +go get github.com/AdamKorcz/go-118-fuzz-build/utils +go get github.com/AdamKorcz/go-fuzz-headers +compile_native_go_fuzzer github.com/gocolly/colly/v2 FuzzHtmlElementUnmarshal FuzzHtmlElementUnmarshal + diff --git a/.clusterfuzzlite/project.yaml b/.clusterfuzzlite/project.yaml new file mode 100644 index 000000000..4f2ee4d97 --- /dev/null +++ b/.clusterfuzzlite/project.yaml @@ -0,0 +1 @@ +language: go diff --git a/.github/workflows/cflite.yml b/.github/workflows/cflite.yml new file mode 100644 index 000000000..080859c15 --- /dev/null +++ b/.github/workflows/cflite.yml @@ -0,0 +1,29 @@ +name: ClusterFuzzLite PR fuzzing +on: + workflow_dispatch: + pull_request: + paths: + - '**' +permissions: read-all +jobs: + PR: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + sanitizer: [address] + steps: + - name: Build Fuzzers (${{ matrix.sanitizer }}) + id: build + uses: google/clusterfuzzlite/actions/build_fuzzers@v1 + with: + sanitizer: ${{ matrix.sanitizer }} + language: go + - name: Run Fuzzers (${{ matrix.sanitizer }}) + id: run + uses: google/clusterfuzzlite/actions/run_fuzzers@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + fuzz-seconds: 400 + mode: 'code-change' + sanitizer: ${{ matrix.sanitizer }} diff --git a/unmarshal_fuzz.go b/unmarshal_fuzz.go new file mode 100644 index 000000000..5ab74157f --- /dev/null +++ b/unmarshal_fuzz.go @@ -0,0 +1,39 @@ +package colly + +import ( + "bytes" + "testing" + + fuzz "github.com/AdamKorcz/go-fuzz-headers" + "github.com/PuerkitoBio/goquery" +) + +type info struct { + Text string `selector:"span"` +} + +type object struct { + Info []*info `selector:"li.info"` +} + +func FuzzHtmlElementUnmarshal(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + gfh := fuzz.NewConsumer(data) + e := &HTMLElement{} + err := gfh.GenerateStruct(e) + if err != nil { + return + } + d2, err := gfh.GetBytes() + if err != nil { + return + } + doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(d2)) + if err != nil { + return + } + e.DOM = doc.First() + o := object{} + e.Unmarshal(&o) + }) +}