Skip to content

Commit

Permalink
Add icon validation for file size and duplicates
Browse files Browse the repository at this point in the history
Add validation for icon file size and duplicate icons.

* **README.md**
  - Add information about icon file size validation.
  - Add information about duplicate icon validation.

* **.github/workflows/pr_checks.yml**
  - Add validation for icon file size in the `validate_icons` job.
  - Add validation for duplicate icons in the `validate_icons` job.

* **maintainer_checklist.md**
  - Add a step to check for duplicate icons.
  - Add a step to check for icon file size.

* **processor/src/main/kotlin/org/ethereum/lists/chains/Main.kt**
  - Add logic to check for duplicate icons by calculating a hash (e.g., SHA-256) of each icon file.
  - Add logic to validate icon file size.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ethereum-lists/chains?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
Ramyromel committed Oct 26, 2024
1 parent 6272e7b commit b9caa52
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .github/workflows/pr_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
META_WIDTH=$(echo "$METADATA" | jq '.[0].ImageWidth' -r)
META_HEIGHT=$(echo "$METADATA" | jq '.[0].ImageHeight' -r)
META_TYPE=$(echo "$METADATA" | jq '.[0].FileTypeExtension' -r)
FILE_SIZE=$(stat -c%s "$TARGET_FILE")
if [ "$SCHEMA_WIDTH" != "$META_WIDTH" ]; then
echo "Expected width $SCHEMA_WIDTH, got $META_WIDTH"
Expand Down Expand Up @@ -99,6 +100,18 @@ jobs:
BAD=1
;;
esac
if [ "$FILE_SIZE" -gt 102400 ]; then
echo "File size exceeds 100 KB"
BAD=1
fi
for CHANGED_ICON_BLOB in "${CHANGED_ICON_BLOBS[@]}"; do
if [ "$CHANGED_ICON_BLOB" != "$TARGET_FILE" ] && cmp -s "$CHANGED_ICON_BLOB" "$TARGET_FILE"; then
echo "Duplicate icon found: $CHANGED_ICON_BLOB"
BAD=1
fi
done
else
echo "Expected an IPFS URL, got $URL"
BAD=1
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ where:
* the URL must be an IPFS url that is publicly resolvable
* width and height are positive integers
* format is either "png", "jpg" or "svg"
* file size must not exceed 100 KB
* icons must not be duplicates

If the chain is an L2 or a shard of another chain you can link it to the parent chain like this:

Expand Down
2 changes: 2 additions & 0 deletions maintainer_checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ A checklist for things to check before merging a chain PR.
* If the PR contains icons:
* `ipfs get` all icon CIDs
* check if the size of the icons you got match the size given in the PR
* check for duplicate icons
* check if the icon file size does not exceed 100 KB
* Check if a PR does not remove a chain - chains cannot be re-moved - only deprecated (to protect from replay attacks)
* Check if a PR does not assign a chainID to a newer chain (something like https://github.com/ethereum-lists/chains/pull/1750)

Expand Down
26 changes: 25 additions & 1 deletion processor/src/main/kotlin/org/ethereum/lists/chains/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.kethereum.rpc.HttpEthereumRPC
import java.math.BigInteger
import javax.imageio.ImageIO
import kotlin.io.OnErrorAction.*
import java.security.MessageDigest

val parsedShortNames = mutableSetOf<String>()
val parsedNames = mutableSetOf<String>()
Expand Down Expand Up @@ -233,6 +234,16 @@ fun checkIcon(icon: File, withIconDownload: Boolean, allIconCIDs: MutableSet<Str
if (image.raster.height != height) {
error("height in json ($icon) is $height but actually is in imageDownload ${image.height}")
}

val fileSize = iconDownloadFile.length()
if (fileSize > 100 * 1024) {
error("Icon file size exceeds 100 KB")
}

val iconHash = calculateHash(iconDownloadFile)
if (isDuplicateIcon(iconHash)) {
error("Duplicate icon found: $icon")
}
} catch (e: Exception) {
e.printStackTrace()
error("problem with image $iconDownloadFile")
Expand Down Expand Up @@ -540,4 +551,17 @@ private fun getNumber(jsonObject: JsonObject, field: String): Long {
is Long -> chainId
else -> throw (Exception("not a number at $field"))
}
}
}

private fun calculateHash(file: File): String {
val digest = MessageDigest.getInstance("SHA-256")
val bytes = file.readBytes()
val hashBytes = digest.digest(bytes)
return hashBytes.joinToString("") { "%02x".format(it) }
}

private val iconHashes = mutableSetOf<String>()

private fun isDuplicateIcon(hash: String): Boolean {
return !iconHashes.add(hash)
}

0 comments on commit b9caa52

Please sign in to comment.