Skip to content

Commit 7cf0d87

Browse files
committed
- Extract RubySaml::XML::Decoder
- Use Base64.strict_encode64 everywhere
1 parent 0f026a7 commit 7cf0d87

21 files changed

+244
-218
lines changed

lib/ruby_saml/authrequest.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def create_params(settings, params={})
5151

5252
Logging.debug "Created AuthnRequest: #{request}"
5353

54-
request = deflate(request) if binding_redirect
55-
base64_request = encode(request)
54+
base64_request = RubySaml::XML::Decoder.encode_message(request, compress: binding_redirect)
5655
request_params = {"SAMLRequest" => base64_request}
5756
sp_signing_key = settings.get_sp_signing_key
5857

@@ -66,7 +65,7 @@ def create_params(settings, params={})
6665
)
6766
sign_algorithm = RubySaml::XML.hash_algorithm(settings.get_sp_signature_method)
6867
signature = sp_signing_key.sign(sign_algorithm.new, url_string)
69-
params['Signature'] = encode(signature)
68+
params['Signature'] = Base64.strict_encode64(signature)
7069
end
7170

7271
params.each_pair do |key, value|

lib/ruby_saml/logoutrequest.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def create_params(settings, params={})
5151

5252
Logging.debug "Created SLO Logout Request: #{request}"
5353

54-
request = deflate(request) if binding_redirect
55-
base64_request = encode(request)
54+
base64_request = RubySaml::XML::Decoder.encode_message(request, compress: binding_redirect)
5655
request_params = {"SAMLRequest" => base64_request}
5756
sp_signing_key = settings.get_sp_signing_key
5857

@@ -66,7 +65,7 @@ def create_params(settings, params={})
6665
)
6766
sign_algorithm = RubySaml::XML.hash_algorithm(settings.get_sp_signature_method)
6867
signature = settings.get_sp_signing_key.sign(sign_algorithm.new, url_string)
69-
params['Signature'] = encode(signature)
68+
params['Signature'] = Base64.strict_encode64(signature)
7069
end
7170

7271
params.each_pair do |key, value|

lib/ruby_saml/logoutresponse.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def initialize(response, settings = nil, options = {})
4040
end
4141

4242
@options = options
43-
@response = decode_raw_saml(response, settings)
43+
@response = RubySaml::XML::Decoder.decode_message(response, @settings&.message_max_bytesize)
4444
@document = RubySaml::XML::SignedDocument.new(@response)
4545
super()
4646
end

lib/ruby_saml/metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def output_xml(meta_doc, pretty_print)
149149
private
150150

151151
def add_certificate_element(xml, cert, use)
152-
cert_text = Base64.encode64(cert.to_der).delete("\n")
152+
cert_text = Base64.strict_encode64(cert.to_der)
153153
xml['md'].KeyDescriptor('use' => use.to_s) do
154154
xml['ds'].KeyInfo('xmlns:ds' => 'http://www.w3.org/2000/09/xmldsig#') do
155155
xml['ds'].X509Data do

lib/ruby_saml/response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def initialize(response, options = {})
6161
end
6262
end
6363

64-
@response = decode_raw_saml(response, settings)
64+
@response = RubySaml::XML::Decoder.decode_message(response, @settings&.message_max_bytesize)
6565
@document = RubySaml::XML::SignedDocument.new(@response, @errors)
6666

6767
if assertion_encrypted?

lib/ruby_saml/saml_message.rb

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
module RubySaml
1111
# SAML2 Message
1212
class SamlMessage
13-
BASE64_FORMAT = %r{\A([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\Z}
1413

1514
# @return [Nokogiri::XML::Schema] Gets the schema object of the SAML 2.0 Protocol schema
1615
#
@@ -75,91 +74,6 @@ def valid_saml?(document, soft = true, check_malformed_doc: true)
7574

7675
private
7776

78-
# Base64 decode and try also to inflate a SAML Message
79-
# @param saml [String] The deflated and encoded SAML Message
80-
# @param settings [RubySaml::Settings|nil] Toolkit settings
81-
# @return [String] The plain SAML Message
82-
#
83-
def decode_raw_saml(saml, settings = nil)
84-
return saml unless base64_encoded?(saml)
85-
86-
settings ||= RubySaml::Settings.new
87-
if saml.bytesize > settings.message_max_bytesize
88-
raise ValidationError.new("Encoded SAML Message exceeds #{settings.message_max_bytesize} bytes, so was rejected")
89-
end
90-
91-
decoded = decode(saml)
92-
message = begin
93-
inflate(decoded)
94-
rescue StandardError
95-
decoded
96-
end
97-
98-
if message.bytesize > settings.message_max_bytesize
99-
raise ValidationError.new("SAML Message exceeds #{settings.message_max_bytesize} bytes, so was rejected")
100-
end
101-
102-
message
103-
end
104-
105-
# Deflate, base64 encode and url-encode a SAML Message (To be used in the HTTP-redirect binding)
106-
# @param saml [String] The plain SAML Message
107-
# @param settings_or_compress [true|false|RubySaml::Settings|nil] Whether or not the SAML should be deflated.
108-
# The usage of RubySaml::Settings here is deprecated.
109-
# @return [String] The deflated and encoded SAML Message (encoded if the compression is requested)
110-
def encode_raw_saml(saml, settings_or_compress = false)
111-
if settings_or_compress.is_a?(TrueClass)
112-
saml = deflate(saml)
113-
elsif settings_or_compress.respond_to?(:compress_request)
114-
Logging.deprecate('Please change the second argument of `encode_raw_saml_message` to a boolean ' \
115-
'indicating whether or not to use compression. Using a boolean will be required ' \
116-
'in RubySaml 2.1.0.')
117-
saml = deflate(saml) if settings_or_compress.compress_request
118-
end
119-
120-
CGI.escape(encode(saml))
121-
end
122-
123-
# Base 64 decode method
124-
# @param string [String] The string message
125-
# @return [String] The decoded string
126-
#
127-
def decode(string)
128-
Base64.decode64(string)
129-
end
130-
131-
# Base 64 encode method
132-
# @param string [String] The string
133-
# @return [String] The encoded string
134-
#
135-
def encode(string)
136-
Base64.strict_encode64(string)
137-
end
138-
139-
# Check if a string is base64 encoded
140-
# @param string [String] string to check the encoding of
141-
# @return [true, false] whether or not the string is base64 encoded
142-
#
143-
def base64_encoded?(string)
144-
!!string.gsub(/[\r\n]|\\r|\\n|\s/, "").match(BASE64_FORMAT)
145-
end
146-
147-
# Inflate method
148-
# @param deflated [String] The string
149-
# @return [String] The inflated string
150-
#
151-
def inflate(deflated)
152-
Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(deflated)
153-
end
154-
155-
# Deflate method
156-
# @param inflated [String] The string
157-
# @return [String] The deflated string
158-
#
159-
def deflate(inflated)
160-
Zlib::Deflate.deflate(inflated, Zlib::BEST_COMPRESSION)[2..-5]
161-
end
162-
16377
def check_malformed_doc?(settings)
16478
default_value = RubySaml::Settings::DEFAULTS[:check_malformed_doc]
16579

lib/ruby_saml/settings.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def get_binding(value)
225225
assertion_consumer_service_binding: Utils::BINDINGS[:post],
226226
single_logout_service_binding: Utils::BINDINGS[:redirect],
227227
idp_cert_fingerprint_algorithm: RubySaml::XML::SHA256,
228-
message_max_bytesize: 250_000,
228+
message_max_bytesize: RubySaml::XML::Decoder::DEFAULT_MAX_BYTESIZE,
229229
soft: true,
230230
check_malformed_doc: true,
231231
security: {

lib/ruby_saml/slo_logoutrequest.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(request, options = {})
3939
@soft = @settings.soft unless @settings.soft.nil?
4040
end
4141

42-
@request = decode_raw_saml(request, settings)
42+
@request = RubySaml::XML::Decoder.decode_message(request, @settings&.message_max_bytesize)
4343
@document = REXML::Document.new(@request)
4444
super()
4545
end

lib/ruby_saml/slo_logoutresponse.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ def create_params(settings, request_id = nil, logout_message = nil, params = {},
5959

6060
Logging.debug "Created SLO Logout Response: #{response}"
6161

62-
response = deflate(response) if binding_redirect
63-
base64_response = encode(response)
62+
base64_response = RubySaml::XML::Decoder.encode_message(response, compress: binding_redirect)
6463
response_params = { 'SAMLResponse' => base64_response }
6564
sp_signing_key = settings.get_sp_signing_key
6665

@@ -74,7 +73,7 @@ def create_params(settings, request_id = nil, logout_message = nil, params = {},
7473
)
7574
sign_algorithm = RubySaml::XML.hash_algorithm(settings.get_sp_signature_method)
7675
signature = sp_signing_key.sign(sign_algorithm.new, url_string)
77-
params['Signature'] = encode(signature)
76+
params['Signature'] = Base64.strict_encode64(signature)
7877
end
7978

8079
params.each_pair do |key, value|

lib/ruby_saml/xml.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ def get_algorithm_attr(element)
141141
end
142142
end
143143

144-
require 'ruby_saml/xml/document_signer'
144+
require 'ruby_saml/xml/decoder'
145145
require 'ruby_saml/xml/decryptor'
146+
require 'ruby_saml/xml/document_signer'
146147
require 'ruby_saml/xml/signed_document'
147148
require 'ruby_saml/xml/deprecated'

0 commit comments

Comments
 (0)