Skip to content

Commit

Permalink
fix urlencode validor format
Browse files Browse the repository at this point in the history
- Encode URL with Addressable::URI.encode before parsing
- check value before encoding
  • Loading branch information
kvkq committed Jan 14, 2025
1 parent dd2ef79 commit 83f514c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions app/validators/url_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,24 @@ def validate_url(record, attribute, value, message, schemes)
end

# If not an email, validate as URL
uri = Addressable::URI.parse(value)
host = uri && uri.host
scheme = uri && uri.scheme
begin
uri = if value.present?
encoded_value = Addressable::URI.encode(value.to_s)
Addressable::URI.parse(encoded_value)
end

host = uri && uri.host
scheme = uri && uri.scheme

valid_scheme = host && scheme && schemes.include?(scheme)
valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, default_rule: nil))
valid_scheme = host && scheme && schemes.include?(scheme)
valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, default_rule: nil))

unless valid_scheme && valid_no_local && valid_suffix
unless valid_scheme && valid_no_local && valid_suffix
record.errors.add(attribute, message, **filtered_options(value))
end
rescue Addressable::URI::InvalidURIError
record.errors.add(attribute, message, **filtered_options(value))

Check warning on line 81 in app/validators/url_validator.rb

View check run for this annotation

Codecov / codecov/patch

app/validators/url_validator.rb#L81

Added line #L81 was not covered by tests
end
rescue Addressable::URI::InvalidURIError
record.errors.add(attribute, message, **filtered_options(value))
end
end

0 comments on commit 83f514c

Please sign in to comment.