From d0f77ef7fa5f7fb6f0446a067201c409696987c9 Mon Sep 17 00:00:00 2001 From: duhow Date: Sat, 28 Oct 2023 19:15:27 +0200 Subject: [PATCH] Add test to find invalid IP address --- pre-commit | 30 ++++++++++++++++++++++++++++-- source | 1 - 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pre-commit b/pre-commit index e0f5cda..48e6d8b 100755 --- a/pre-commit +++ b/pre-commit @@ -5,9 +5,9 @@ _fail(){ echo "::error::$@"; FAIL=1; } _fail_exit(){ _fail "$@"; exit 1; } -test_list_has_unique_ip_addrs(){ - FILE="source" +FILE="source" +test_0_list_has_unique_ip_addrs(){ REPEATED=`grep -ve '^#' -ve '^$' $FILE | sort | uniq -d` if [ -n "$REPEATED" ]; then echo "${REPEATED}" @@ -18,6 +18,32 @@ test_list_has_unique_ip_addrs(){ # Filter: # grep -vFf duplicated_list.txt new_list.txt | awk '{print $1}' > final_list.txt +test_1_ip_is_valid(){ + # extract only up to first space, exclude comments (hopefully) + IP_LIST=`grep -ve '^#' -ve '^$' $FILE | awk '{print $1}'` + while IFS= read -r ip; do + # count how many dots are in string + dots="${ip//[^.]/}" + if [ "${#dots}" -ne 3 ]; then + _fail "Invalid IP address: $ip" + continue + fi + + IFS='.' read -r -a blocks <<< "$ip" + for block in "${blocks[@]}"; do + if ! [[ $block =~ ^[0-9]+(\/[0-9]+)?$ ]]; then + _fail "Invalid IP address: $ip" + continue 2 # Skip to the next IP address + fi + + if ((block < 0 || block > 255)); then + _fail "Invalid IP address: $ip" + continue 2 # Skip to the next IP address + fi + done + done <<< $IP_LIST +} + # --- FAIL=0 diff --git a/source b/source index 921aef4..e781ee9 100644 --- a/source +++ b/source @@ -2765,7 +2765,6 @@ 68.132.154.22 79.52.3.40 95.95.177.143 -4.52.168 104.224.54.179 111.242.34.86 113.124.84.254