Improve SVG regexp validation to use \A and \z (#19953)

This commit is contained in:
Helio Cola 2023-08-24 09:29:28 -04:00 committed by GitHub
parent 992ced1908
commit de1e79e722
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -1,5 +1,5 @@
class NavigationLink < ApplicationRecord
SVG_REGEXP = /<svg .*>/im
SVG_REGEXP = /\A<svg .*>[\s]*\z/im
before_validation :allow_relative_url, if: :url?
before_save :strip_local_hostname, if: :url?

View file

@ -51,6 +51,18 @@ RSpec.describe NavigationLink do
navigation_link.icon = "<svg foo='bar'\nbaz='lol'\n\n more stuff...\n\n. >"
expect(navigation_link).to be_valid
navigation_link.icon = "something...something\n<svg foo='bar'\nbaz='lol'\n\n more stuff...\n\n. >"
expect(navigation_link).not_to be_valid
navigation_link.icon = "<svg foo='bar'\nbaz='lol'\n\n more stuff...\n\n. >\n\n\n\n"
expect(navigation_link).to be_valid
navigation_link.icon = "<svg foo='bar'\nbaz='lol'\n\n more stuff...\n\n. >\n\n\t \n\n"
expect(navigation_link).to be_valid
navigation_link.icon = "<svg foo='bar'\nbaz='lol'\n\n more stuff...\n\n. >\n\nsomething\n\n"
expect(navigation_link).not_to be_valid
end
context "when validating the URL" do