From 99139b00858fdb00cd2e607b60af0253bb160a4d Mon Sep 17 00:00:00 2001 From: Madhur Shrimal Date: Tue, 19 Dec 2023 10:20:23 -0800 Subject: [PATCH] add actual image and check mimetype --- types/operator_metadata.go | 19 +++++++++++++++++++ types/operator_metadata_test.go | 31 +++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/types/operator_metadata.go b/types/operator_metadata.go index 4f9b6b3f..3d5b87a0 100644 --- a/types/operator_metadata.go +++ b/types/operator_metadata.go @@ -3,12 +3,18 @@ package types import ( "errors" "fmt" + "io" + "net/http" "net/url" "path/filepath" "regexp" "strings" ) +const ( + PngMimeType = "image/png" +) + // OperatorMetadata is the metadata operator uploads while registering // itself to eigenlayer type OperatorMetadata struct { @@ -119,6 +125,19 @@ func isImageURL(urlString string) error { // Check if the extension is in the list of image extensions for _, imgExt := range imageExtensions { if strings.EqualFold(extension, imgExt) { + imageResponse, err := http.Get(urlString) + if err != nil { + return err + } + imageBytes, err := io.ReadAll(imageResponse.Body) + if err != nil { + return err + } + + contentType := http.DetectContentType(imageBytes) + if contentType != PngMimeType { + return errors.New("invalid image format. only png is supported") + } return nil } } diff --git a/types/operator_metadata_test.go b/types/operator_metadata_test.go index cfde97de..085997eb 100644 --- a/types/operator_metadata_test.go +++ b/types/operator_metadata_test.go @@ -13,7 +13,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "test", Description: "test", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "https://twitter.com/test", Website: "https://test.com", }, @@ -24,7 +24,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "", Description: "test", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "https://twitter.com/test", Website: "https://test.com", }, @@ -35,7 +35,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "test", Description: "", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "https://twitter.com/test", Website: "https://test.com", }, @@ -52,12 +52,23 @@ func TestOperatorMetadata(t *testing.T) { }, wantErr: true, }, + { + name: "Invalid metadata - invalid mime type with correct extension", + metadata: OperatorMetadata{ + Name: "test", + Description: "My operator", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/cat.png", + Twitter: "https://twitter.com/test", + Website: "https://test.com", + }, + wantErr: true, + }, { name: "Invalid metadata - description > 500 characters", metadata: OperatorMetadata{ Name: "test", Description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "https://twitter.com/test", Website: "https://test.com", }, @@ -101,7 +112,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "test", Description: "test", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "https://twitter.com/test", Website: "https", }, @@ -112,7 +123,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "test", Description: "test", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "https://twitter.com/test", Website: "https:/test.com", }, @@ -123,7 +134,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "test", Description: "test", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "https://twitter.com/test", Website: "ps://test.com", }, @@ -134,7 +145,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "test", Description: "test", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "http", Website: "https://test.com", }, @@ -145,7 +156,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "test", Description: "test", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "ht://twitter.com/test", Website: "https:/test.com", }, @@ -156,7 +167,7 @@ func TestOperatorMetadata(t *testing.T) { metadata: OperatorMetadata{ Name: "test", Description: "test", - Logo: "https://test.com/test.png", + Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png", Twitter: "https://twitt", Website: "ps://test.com", },