|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | require 'uri'
|
| 4 | +require 'ipaddr' |
| 5 | +require 'public_suffix' |
4 | 6 |
|
| 7 | +# TODO: ensure rescues |
5 | 8 | class MiniDefender::Rules::Url < MiniDefender::Rule
|
| 9 | + ALLOWED_MODIFIERS = %w[https public not_ip not_private] |
| 10 | + |
| 11 | + def initialize(modifiers = []) |
| 12 | + @modifiers = Array(modifiers).map(&:to_s) |
| 13 | + |
| 14 | + unless @modifiers.empty? |
| 15 | + validate_modifiers! |
| 16 | + end |
| 17 | + |
| 18 | + @validation_error = "URL modifiers list contains only #{ALLOWED_MODIFIERS.join(', ')}." |
| 19 | + end |
| 20 | + |
6 | 21 | def self.signature
|
7 | 22 | 'url'
|
8 | 23 | end
|
9 | 24 |
|
| 25 | + def self.make(modifiers) # no need to raise an error when no modifier is entered; as 'url' rule checks URL structure on its own |
| 26 | + new(modifiers) |
| 27 | + end |
| 28 | + |
10 | 29 | def passes?(attribute, value, validator)
|
11 |
| - value.is_a?(String) && URI.regexp(%w[http https]).match?(value) |
| 30 | + # TODO: warning: URI.regexp is obsolete; use URI::DEFAULT_PARSER.make_regexp instead |
| 31 | + unless value.is_a?(String) && URI::DEFAULT_PARSER.make_regexp(%w[http https]).match?(value) |
| 32 | + return false |
| 33 | + end |
| 34 | + |
| 35 | + begin |
| 36 | + uri = URI.parse(value) |
| 37 | + |
| 38 | + if @modifiers.empty? |
| 39 | + return true |
| 40 | + end |
| 41 | + |
| 42 | + if @modifiers.include?('https') && uri.scheme != 'https' |
| 43 | + @validation_error = 'The URL must use HTTPS.' |
| 44 | + return false |
| 45 | + end |
| 46 | + |
| 47 | + if @modifiers.include?('public') && (!PublicSuffix.valid?(uri.host) || self.class.private_network?(uri.host)) |
| 48 | + @validation_error = 'The URL must use a valid public domain.' |
| 49 | + return false |
| 50 | + end |
| 51 | + |
| 52 | + if @modifiers.include?('not_ip') && ip_address?(uri.host) |
| 53 | + @validation_error = 'IP addresses are not allowed in URLs.' |
| 54 | + return false |
| 55 | + end |
| 56 | + |
| 57 | + if @modifiers.include?('not_private') && self.class.private_network?(uri.host) |
| 58 | + @validation_error = 'Private or reserved resources are not allowed.' |
| 59 | + return false |
| 60 | + end |
| 61 | + |
| 62 | + true |
| 63 | + rescue URI::InvalidURIError |
| 64 | + @validation_error = 'The field must contain a valid URL.' |
| 65 | + false |
| 66 | + rescue PublicSuffix::Error |
| 67 | + false |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def self.private_network?(host) |
| 72 | + unless host |
| 73 | + return false |
| 74 | + end |
| 75 | + |
| 76 | + host = host.downcase |
| 77 | + |
| 78 | + private_patterns.any? { |pattern| pattern.match?(host) } |
12 | 79 | end
|
13 | 80 |
|
14 | 81 | def message(attribute, value, validator)
|
15 |
| - 'The field must contain a valid URL.' |
| 82 | + @validation_error || 'The field must contain a valid URL.' |
| 83 | + end |
| 84 | + |
| 85 | + private |
| 86 | + |
| 87 | + def validate_modifiers! |
| 88 | + invalid_modifiers = @modifiers - ALLOWED_MODIFIERS |
| 89 | + if invalid_modifiers.empty? |
| 90 | + return |
| 91 | + end |
| 92 | + |
| 93 | + raise ArgumentError, "Invalid URL modifiers: #{invalid_modifiers.join(', ')}" |
| 94 | + end |
| 95 | + |
| 96 | + def ip_address?(host) |
| 97 | + unless host |
| 98 | + return false |
| 99 | + end |
| 100 | + |
| 101 | + begin |
| 102 | + IPAddr.new(host) |
| 103 | + true |
| 104 | + rescue IPAddr::InvalidAddressError |
| 105 | + false |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + def self.private_patterns |
| 110 | + @private_patterns ||= begin |
| 111 | + pattern_file = File.expand_path('../data/private_network_patterns.txt', __dir__) |
| 112 | + File.readlines(pattern_file).filter_map do |line| |
| 113 | + line = line.strip |
| 114 | + |
| 115 | + if line.empty? || line.start_with?('#') |
| 116 | + next |
| 117 | + end |
| 118 | + |
| 119 | + # Pattern => regex (once) |
| 120 | + pattern = line |
| 121 | + .gsub('.', '\.') # escape dots |
| 122 | + .gsub('*', '.*') # wildcards => regex |
| 123 | + .gsub('[0-9]+', '\d+') # convert number ranges |
| 124 | + .gsub(/\[(.+?)\]/, '(\1)') # convert chars classes |
| 125 | + |
| 126 | + Regexp.new("^#{pattern}$", Regexp::IGNORECASE) |
| 127 | + end |
| 128 | + end |
16 | 129 | end
|
17 | 130 | end
|
0 commit comments