Skip to content

Commit

Permalink
runtime/evm: prevent saving invaild NFT metadata JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Nov 7, 2023
1 parent 182d8f1 commit e45de7d
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions analyzer/runtime/evm/erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"math/big"
"strings"
"time"

ethCommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -108,20 +107,46 @@ func evmDownloadNFTERC721Metadata(ctx context.Context, logger *log.Logger, sourc
)
return nil
}
// 1. Read metadata into interface{} to allow any syntactically correct
// JSON value.
var metadataAny interface{}
limitedReader := io.LimitReader(rc, MaxMetadataBytes)
var metadataBuilder strings.Builder
teeReader := io.TeeReader(limitedReader, &metadataBuilder)
var metadata ERC721AssetMetadata
if err = json.NewDecoder(teeReader).Decode(&metadata); err != nil {
logger.Info("error decoding token metadata",
if err = json.NewDecoder(limitedReader).Decode(&metadataAny); err != nil {
logger.Info("error decoding token metadata as any",
"uri", nftData.MetadataURI,
"err", err,
)
if err = rc.Close(); err != nil {
return fmt.Errorf("closing metadata reader: %w", err)
}
return nil
}
if err = rc.Close(); err != nil {
return fmt.Errorf("closing metadata reader: %w", err)
}
nftData.Metadata = common.Ptr(metadataBuilder.String())
// 2. Re-serialize into JSON for database. This is to normalize the
// syntax, in case Go and our database have slightly different opinions
// on what's valid JSON.
metadataNormalJSONBuf, err := json.Marshal(metadataAny)
if err != nil {
logger.Info("error re-encoding token metadata",
"uri", nftData.MetadataURI,
"err", err,
)
return nil
}
nftData.Metadata = common.Ptr(string(metadataNormalJSONBuf))
// 3. Parse it into the ERC-721 asset metadata schema. There's no function
// to convert these map[string]interface{} things into structs, so
// we'll just parse it again.
var metadata ERC721AssetMetadata
if err = json.Unmarshal(metadataNormalJSONBuf, &metadata); err != nil {
logger.Info("error decoding token metadata as asset metadata",
"uri", nftData.MetadataURI,
"err", err,
)
return nil
}
nftData.Name = metadata.Name
nftData.Description = metadata.Description
nftData.Image = metadata.Image
Expand Down

0 comments on commit e45de7d

Please sign in to comment.