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

types: refactor multiple fuzzers #1258

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions pkg/fuzz/fuzz_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fuzz

import (
"net/url"
"os"
"path/filepath"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"

fuzz "github.com/AdaLogics/go-fuzz-headers"

"github.com/sigstore/rekor/pkg/log"
"github.com/sigstore/rekor/pkg/types"
)

func CreateProps(ff *fuzz.ConsumeFuzzer) (types.ArtifactProperties, func(), error) {
props := types.ArtifactProperties{}
ff.GenerateStruct(&props) //nolint:all

if props.ArtifactBytes == nil {
artifactBytes, err := ff.GetBytes()
if err != nil {
return props, nil, err
}
artifactFile, err := os.Create("ArtifactFile")
if err != nil {
return props, nil, err
}
defer artifactFile.Close()

artifactPath, err := filepath.Abs("ArtifactFile")
if err != nil {
return props, nil, err
}
artifactURL, err := url.Parse(artifactPath)
if err != nil {
return props, nil, err
}
props.ArtifactPath = artifactURL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases, if ArtifactBytes are written, then we won't even look at the ArtifactPath (

if artifactBytes == nil {
var artifactReader io.ReadCloser
if props.ArtifactPath == nil {
) -- maybe instead you can take some input from ff to either use ArtifactBytes directly or use it from ArtifactPath and clear ArtifactBytes?

The same goes for SignatureBytes and SignaturePath/PublicKeyBytes and PublicKeyPaths

Copy link
Contributor

@asraa asraa Jan 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misread this! If the ArtifactBytes are nil -- lgtm! (Just see about the Signature/PublicKey if there are any issues)


_, err = artifactFile.Write(artifactBytes)
return props, func() {
os.Remove("ArtifactFile")
}, err

}
return props, func() {}, nil
}

func SetFuzzLogger() {
config := zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(zapcore.FatalLevel)
logger, err := config.Build()
if err != nil {
panic(err)
}
log.Logger = logger.Named("rekor-fuzz-logger").Sugar()
}
5 changes: 5 additions & 0 deletions pkg/types/cose/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ package cose

import (
"context"
"sync"
"testing"

fuzzUtils "github.com/sigstore/rekor/pkg/fuzz"
"github.com/sigstore/rekor/pkg/types"
)

var initter sync.Once

func FuzzCreateProposedEntry(f *testing.F) {
f.Fuzz(func(t *testing.T, version string) {
initter.Do(fuzzUtils.SetFuzzLogger)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be moved to cose v0.0.1 and populate the props with CreateProps like other types?

ctx := context.Background()
brt := New()
props := types.ArtifactProperties{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,32 @@ package helm
import (
"bytes"
"context"
"sync"
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"

"github.com/sigstore/rekor/pkg/types"
fuzzUtils "github.com/sigstore/rekor/pkg/fuzz"
"github.com/sigstore/rekor/pkg/types/helm"
)

var initter sync.Once

func FuzzHelmCreateProposedEntry(f *testing.F) {
f.Fuzz(func(t *testing.T, version string, propsData []byte) {
f.Fuzz(func(t *testing.T, propsData []byte) {
initter.Do(fuzzUtils.SetFuzzLogger)

version := "0.0.1"

ff := fuzz.NewConsumer(propsData)
props := types.ArtifactProperties{}
ff.GenerateStruct(&props)
it := New()

props, cleanup, err := fuzzUtils.CreateProps(ff)
if err != nil {
t.Skip()
}
defer cleanup()

it := helm.New()
entry, err := it.CreateProposedEntry(context.Background(), version, props)
if err != nil {
t.Skip()
Expand All @@ -44,7 +57,7 @@ func FuzzHelmCreateProposedEntry(f *testing.F) {

func FuzzHelmProvenanceUnmarshal(f *testing.F) {
f.Fuzz(func(t *testing.T, provenanceData []byte) {
p := &Provenance{}
p := &helm.Provenance{}
r := bytes.NewReader(provenanceData)
p.Unmarshal(r)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ package intoto

import (
"context"
"sync"
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"

"github.com/sigstore/rekor/pkg/types"
fuzzUtils "github.com/sigstore/rekor/pkg/fuzz"
"github.com/sigstore/rekor/pkg/types/intoto"
)

var initter sync.Once

func FuzzIntotoCreateProposedEntry(f *testing.F) {
f.Fuzz(func(t *testing.T, version string, propsData []byte) {
f.Fuzz(func(t *testing.T, propsData []byte) {
initter.Do(fuzzUtils.SetFuzzLogger)

version := "0.0.1"

ff := fuzz.NewConsumer(propsData)
props := types.ArtifactProperties{}
ff.GenerateStruct(&props)
it := New()

props, cleanup, err := fuzzUtils.CreateProps(ff)
if err != nil {
t.Skip()
}
defer cleanup()

it := intoto.New()
entry, err := it.CreateProposedEntry(context.Background(), version, props)
if err != nil {
t.Skip()
Expand Down
55 changes: 55 additions & 0 deletions pkg/types/intoto/v0.0.2/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package intoto

import (
"context"
"sync"
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"

fuzzUtils "github.com/sigstore/rekor/pkg/fuzz"
"github.com/sigstore/rekor/pkg/types/intoto"
)

var initter sync.Once

func FuzzIntotoCreateProposedEntry(f *testing.F) {
f.Fuzz(func(t *testing.T, propsData []byte) {
initter.Do(fuzzUtils.SetFuzzLogger)

version := "0.0.2"

ff := fuzz.NewConsumer(propsData)

props, cleanup, err := fuzzUtils.CreateProps(ff)
if err != nil {
t.Skip()
}
defer cleanup()

it := intoto.New()
entry, err := it.CreateProposedEntry(context.Background(), version, props)
if err != nil {
t.Skip()
}
_, err = it.UnmarshalEntry(entry)
if err != nil {
t.Skip()
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ package rekord

import (
"context"
"sync"
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"

"github.com/sigstore/rekor/pkg/types"
fuzzUtils "github.com/sigstore/rekor/pkg/fuzz"
"github.com/sigstore/rekor/pkg/types/rekord"
)

var initter sync.Once

func FuzzRekordCreateProposedEntry(f *testing.F) {
f.Fuzz(func(t *testing.T, version string, propsData []byte) {
f.Fuzz(func(t *testing.T, propsData []byte) {
initter.Do(fuzzUtils.SetFuzzLogger)

version := "0.0.1"

ff := fuzz.NewConsumer(propsData)
props := types.ArtifactProperties{}
ff.GenerateStruct(&props)
it := New()

props, cleanup, err := fuzzUtils.CreateProps(ff)
if err != nil {
t.Skip()
}
defer cleanup()

it := rekord.New()
entry, err := it.CreateProposedEntry(context.Background(), version, props)
if err != nil {
t.Skip()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ package rfc3161

import (
"context"
"sync"
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"

"github.com/sigstore/rekor/pkg/types"
fuzzUtils "github.com/sigstore/rekor/pkg/fuzz"
"github.com/sigstore/rekor/pkg/types/rfc3161"
)

var initter sync.Once

func FuzzRfc3161CreateProposedEntry(f *testing.F) {
f.Fuzz(func(t *testing.T, version string, propsData []byte) {
f.Fuzz(func(t *testing.T, propsData []byte) {
initter.Do(fuzzUtils.SetFuzzLogger)

version := "0.0.1"

ff := fuzz.NewConsumer(propsData)
props := types.ArtifactProperties{}
ff.GenerateStruct(&props)
it := New()

props, cleanup, err := fuzzUtils.CreateProps(ff)
if err != nil {
t.Skip()
}
defer cleanup()

it := rfc3161.New()
entry, err := it.CreateProposedEntry(context.Background(), version, props)
if err != nil {
t.Skip()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ package rpm

import (
"context"
"sync"
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"

"github.com/sigstore/rekor/pkg/types"
fuzzUtils "github.com/sigstore/rekor/pkg/fuzz"
"github.com/sigstore/rekor/pkg/types/rpm"
)

var initter sync.Once

func FuzzRpmCreateProposedEntry(f *testing.F) {
f.Fuzz(func(t *testing.T, version string, propsData []byte) {
f.Fuzz(func(t *testing.T, propsData []byte) {
initter.Do(fuzzUtils.SetFuzzLogger)

version := "0.0.1"

ff := fuzz.NewConsumer(propsData)
props := types.ArtifactProperties{}
ff.GenerateStruct(&props)
it := New()

props, cleanup, err := fuzzUtils.CreateProps(ff)
if err != nil {
t.Skip()
}
defer cleanup()

it := rpm.New()
entry, err := it.CreateProposedEntry(context.Background(), version, props)
if err != nil {
t.Skip()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ package tuf

import (
"context"
"sync"
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"

"github.com/sigstore/rekor/pkg/types"
fuzzUtils "github.com/sigstore/rekor/pkg/fuzz"
"github.com/sigstore/rekor/pkg/types/tuf"
)

var initter sync.Once

func FuzzTufCreateProposedEntry(f *testing.F) {
f.Fuzz(func(t *testing.T, version string, propsData []byte) {
f.Fuzz(func(t *testing.T, propsData []byte) {
initter.Do(fuzzUtils.SetFuzzLogger)

version := "0.0.1"

ff := fuzz.NewConsumer(propsData)
props := types.ArtifactProperties{}
ff.GenerateStruct(&props)
it := New()

props, cleanup, err := fuzzUtils.CreateProps(ff)
if err != nil {
t.Skip()
}
defer cleanup()

it := tuf.New()
entry, err := it.CreateProposedEntry(context.Background(), version, props)
if err != nil {
t.Skip()
Expand Down
Loading