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

Add icon validation for file size and duplicates #6076

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions .github/workflows/pr_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

- name: Verify the changed icons
if: steps.changed-icons.outputs.any_changed == 'true'
run: |

Check failure on line 38 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2043:warning:7:21: This loop will only ever run once. Bad quoting or missing glob/expansion? [shellcheck] Raw Output: .github/workflows/pr_checks.yml:38:7: shellcheck reported issue in this script: SC2043:warning:7:21: This loop will only ever run once. Bad quoting or missing glob/expansion? [shellcheck]

Check failure on line 38 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2002:style:9:13: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead [shellcheck] Raw Output: .github/workflows/pr_checks.yml:38:7: shellcheck reported issue in this script: SC2002:style:9:13: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead [shellcheck]

Check failure on line 38 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2076:warning:13:42: Remove quotes from right-hand side of =~ to match as a regex rather than literally [shellcheck] Raw Output: .github/workflows/pr_checks.yml:38:7: shellcheck reported issue in this script: SC2076:warning:13:42: Remove quotes from right-hand side of =~ to match as a regex rather than literally [shellcheck]

Check failure on line 38 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2086:info:21:25: Double quote to prevent globbing and word splitting [shellcheck] Raw Output: .github/workflows/pr_checks.yml:38:7: shellcheck reported issue in this script: SC2086:info:21:25: Double quote to prevent globbing and word splitting [shellcheck]

Check failure on line 38 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2002:style:23:24: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead [shellcheck] Raw Output: .github/workflows/pr_checks.yml:38:7: shellcheck reported issue in this script: SC2002:style:23:24: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead [shellcheck]

Check failure on line 38 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2002:style:24:25: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead [shellcheck] Raw Output: .github/workflows/pr_checks.yml:38:7: shellcheck reported issue in this script: SC2002:style:24:25: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead [shellcheck]

Check failure on line 38 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2002:style:25:23: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead [shellcheck] Raw Output: .github/workflows/pr_checks.yml:38:7: shellcheck reported issue in this script: SC2002:style:25:23: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead [shellcheck]
set -euo pipefail

BAD=0
Expand Down Expand Up @@ -64,6 +64,7 @@
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 @@
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 Expand Up @@ -130,7 +143,7 @@

- name: Verify the changed icons
if: steps.changed-files.outputs.any_changed == 'true'
run: |

Check failure on line 146 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2034:warning:3:1: BAD appears unused. Verify use (or export if used externally) [shellcheck] Raw Output: .github/workflows/pr_checks.yml:146:7: shellcheck reported issue in this script: SC2034:warning:3:1: BAD appears unused. Verify use (or export if used externally) [shellcheck]

Check failure on line 146 in .github/workflows/pr_checks.yml

View workflow job for this annotation

GitHub Actions / actionlint

[actionlint] reported by reviewdog 🐶 shellcheck reported issue in this script: SC2043:warning:5:21: This loop will only ever run once. Bad quoting or missing glob/expansion? [shellcheck] Raw Output: .github/workflows/pr_checks.yml:146:7: shellcheck reported issue in this script: SC2043:warning:5:21: This loop will only ever run once. Bad quoting or missing glob/expansion? [shellcheck]
set -euo pipefail

BAD=0
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)
}
Loading