Skip to content

Commit

Permalink
chore: variable names and types
Browse files Browse the repository at this point in the history
  • Loading branch information
agparadiso committed Jan 13, 2025
1 parent 2b894f7 commit 82d5beb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
40 changes: 20 additions & 20 deletions pkg/workflows/wasm/host/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,29 @@ func (r *store) delete(id string) {
}

var (
defaultTickInterval = 100 * time.Millisecond
defaultTimeout = 10 * time.Second
defaultMinMemoryMBs = 128
DefaultInitialFuel = uint64(100_000_000)
defaultMaxFetchRequests = 5
defaultMaxBinarySize = 10 * 1024 * 1024 // 10 MB
defaultTickInterval = 100 * time.Millisecond
defaultTimeout = 10 * time.Second
defaultMinMemoryMBs = 128
DefaultInitialFuel = uint64(100_000_000)
defaultMaxFetchRequests = 5
defaultMaxCompressedBinarySize = 10 * 1024 * 1024 // 10 MB
)

type DeterminismConfig struct {
// Seed is the seed used to generate cryptographically insecure random numbers in the module.
Seed int64
}
type ModuleConfig struct {
TickInterval time.Duration
Timeout *time.Duration
MaxMemoryMBs int64
MinMemoryMBs int64
InitialFuel uint64
Logger logger.Logger
IsUncompressed bool
Fetch func(ctx context.Context, req *wasmpb.FetchRequest) (*wasmpb.FetchResponse, error)
MaxFetchRequests int
MaxBinarySize int64
TickInterval time.Duration
Timeout *time.Duration
MaxMemoryMBs int64
MinMemoryMBs int64
InitialFuel uint64
Logger logger.Logger
IsUncompressed bool
Fetch func(ctx context.Context, req *wasmpb.FetchRequest) (*wasmpb.FetchResponse, error)
MaxFetchRequests int
MaxCompressedBinarySize uint64

// Labeler is used to emit messages from the module.
Labeler custmsg.MessageEmitter
Expand Down Expand Up @@ -168,8 +168,8 @@ func NewModule(modCfg *ModuleConfig, binary []byte, opts ...func(*ModuleConfig))
modCfg.MinMemoryMBs = int64(defaultMinMemoryMBs)
}

if modCfg.MaxBinarySize == 0 {
modCfg.MaxBinarySize = int64(defaultMaxBinarySize)
if modCfg.MaxCompressedBinarySize == 0 {
modCfg.MaxCompressedBinarySize = uint64(defaultMaxCompressedBinarySize)
}

// Take the max of the min and the configured max memory mbs.
Expand All @@ -191,8 +191,8 @@ func NewModule(modCfg *ModuleConfig, binary []byte, opts ...func(*ModuleConfig))
if !modCfg.IsUncompressed {
// validate the binary size before decompressing
// this is to prevent decompression bombs
if int64(len(binary)) > modCfg.MaxBinarySize {
return nil, fmt.Errorf("binary size exceeds the maximum allowed size of %d bytes", modCfg.MaxBinarySize)
if uint64(len(binary)) > modCfg.MaxCompressedBinarySize {
return nil, fmt.Errorf("binary size exceeds the maximum allowed size of %d bytes", modCfg.MaxCompressedBinarySize)
}

rdr := brotli.NewReader(bytes.NewBuffer(binary))
Expand Down
14 changes: 6 additions & 8 deletions pkg/workflows/wasm/host/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,7 @@ func TestModule_CompressedBinarySize(t *testing.T) {
})

t.Run("compressed binary size is bigger than the default 10mb limit", func(t *testing.T) {
// 11mb binary
binary := make([]byte, 11*1024*1024)
binary := make([]byte, defaultMaxCompressedBinarySize+1)

var b bytes.Buffer
bwr := brotli.NewWriter(&b)
Expand All @@ -924,23 +923,22 @@ func TestModule_CompressedBinarySize(t *testing.T) {
require.NoError(t, bwr.Close())

_, err = NewModule(&ModuleConfig{IsUncompressed: false, Logger: logger.Test(t)}, binary)
default10mbLimit := fmt.Sprintf("binary size exceeds the maximum allowed size of %d bytes", 10*1024*1024)
default10mbLimit := fmt.Sprintf("binary size exceeds the maximum allowed size of %d bytes", defaultMaxCompressedBinarySize)
require.ErrorContains(t, err, default10mbLimit)
})

t.Run("compressed binary size is bigger than the custom limit", func(t *testing.T) {
// 2mb binary
binary := make([]byte, 2*1024*1024)
customMaxCompressedBinarySize := uint64(1 * 1024 * 1024)
binary := make([]byte, customMaxCompressedBinarySize+1)

var b bytes.Buffer
bwr := brotli.NewWriter(&b)
_, err := bwr.Write(binary)
require.NoError(t, err)
require.NoError(t, bwr.Close())

customMaxBinarySize := int64(1 * 1024 * 1024)
_, err = NewModule(&ModuleConfig{IsUncompressed: false, MaxBinarySize: customMaxBinarySize, Logger: logger.Test(t)}, binary)
default10mbLimit := fmt.Sprintf("binary size exceeds the maximum allowed size of %d bytes", customMaxBinarySize)
_, err = NewModule(&ModuleConfig{IsUncompressed: false, MaxCompressedBinarySize: customMaxCompressedBinarySize, Logger: logger.Test(t)}, binary)
default10mbLimit := fmt.Sprintf("binary size exceeds the maximum allowed size of %d bytes", customMaxCompressedBinarySize)
require.ErrorContains(t, err, default10mbLimit)
})
}
Expand Down

0 comments on commit 82d5beb

Please sign in to comment.