From ffb3fef7dba2f7ca2d927bc8432731962d2ee4f9 Mon Sep 17 00:00:00 2001 From: Daniel M Brasil Date: Tue, 9 May 2023 09:45:47 -0300 Subject: [PATCH] Fix uncaught `ActiveRecord::StatementInvalid` in Mastodon::IpBlocksCLI (#24861) --- lib/mastodon/ip_blocks_cli.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/mastodon/ip_blocks_cli.rb b/lib/mastodon/ip_blocks_cli.rb index 3b99595857..8a8685fe5a 100644 --- a/lib/mastodon/ip_blocks_cli.rb +++ b/lib/mastodon/ip_blocks_cli.rb @@ -36,6 +36,12 @@ module Mastodon failed = 0 addresses.each do |address| + unless valid_ip_address?(address) + say("#{address} is invalid", :red) + failed += 1 + next + end + ip_block = IpBlock.find_by(ip: address) if ip_block.present? && !options[:force] @@ -79,6 +85,12 @@ module Mastodon skipped = 0 addresses.each do |address| + unless valid_ip_address?(address) + say("#{address} is invalid", :yellow) + skipped += 1 + next + end + ip_blocks = if options[:force] IpBlock.where('ip >>= ?', address) else @@ -126,5 +138,12 @@ module Mastodon :red end end + + def valid_ip_address?(ip_address) + IPAddr.new(ip_address) + true + rescue IPAddr::InvalidAddressError + false + end end end