Skip to content

Commit 803b27f

Browse files
committed
Merge branch 'fix-base64-check' of https://github.com/phlipper/ruby-saml
2 parents c850f4c + 75a9f70 commit 803b27f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/onelogin/ruby-saml/saml_message.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class SamlMessage
1414
ASSERTION = "urn:oasis:names:tc:SAML:2.0:assertion"
1515
PROTOCOL = "urn:oasis:names:tc:SAML:2.0:protocol"
1616

17+
BASE64_FORMAT_REGEXP = %r{\A(([A-Za-z0-9+/]{4}))*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)\Z}
18+
1719
def self.schema
1820
@schema ||= Mutex.new.synchronize do
1921
Dir.chdir(File.expand_path("../../../schemas", __FILE__)) do
@@ -46,7 +48,7 @@ def validation_error(message)
4648
# is to try and inflate it and fall back to the base64 decoded string if
4749
# the stream contains errors.
4850
def decode_raw_saml(saml)
49-
return saml unless is_base64?(saml)
51+
return saml unless base64_formatted?(saml)
5052

5153
decoded = decode(saml)
5254
begin
@@ -73,11 +75,11 @@ def encode(encoded)
7375
##
7476
# Check if +string+ is base64 encoded
7577
#
76-
# The function is not strict and does allow newline. This is because some SAML implementations
77-
# uses newline in the base64-encoded data, even if they shouldn't have (RFC4648).
78-
def is_base64?(string)
79-
string = string.gsub(/\r\n/, "").gsub(/\n/, "")
80-
string.match(%r{\A(([A-Za-z0-9+/]{4}))*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)\Z})
78+
# The function is not strict and allows newlines. This is because some
79+
# SAML implementations use newlines in the base64-encoded data even if
80+
# they shouldn't (RFC4648).
81+
def base64_formatted?(string)
82+
string.gsub(/[\r\n]|\\r|\\n/, "").match(BASE64_FORMAT_REGEXP)
8183
end
8284

8385
def escape(unescaped)

0 commit comments

Comments
 (0)