You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
and from a trusted source. Ruby SAML does not perform any validation that the URL
47
55
you entered is correct and/or safe.
48
56
-**False-Positive Security Warnings:** Some tools may incorrectly report Ruby SAML as a
49
-
potential security vulnerability, due to it's dependency on Nokogiri. Such warnings can
57
+
potential security vulnerability, due to its dependency on Nokogiri. Such warnings can
50
58
be ignored; Ruby SAML uses Nokogiri in a safe way, by always disabling its DTDLOAD option
51
59
and enabling its NONET option.
60
+
-**Prevent Replay attacks:** A replay attack is when an attacker intercepts a valid SAML assertion and "replays" it at a later time to gain unauthorized access. The `ruby-saml` library provides the tools to prevent this, but **you, the developer, must implement the core logic**, see an specific section later in the README.
52
61
53
62
### Supported Ruby Versions
54
63
@@ -179,7 +188,7 @@ def saml_settings
179
188
end
180
189
```
181
190
182
-
The use of settings.issuer is deprecated in favour of settings.sp_entity_id since version 1.11.0
191
+
The use of settings.issuer is deprecated in favor of settings.sp_entity_id since version 1.11.0
183
192
184
193
Some assertion validations can be skipped by passing parameters to `RubySaml::Response.new()`.
185
194
For example, you can skip the `AuthnStatement`, `Conditions`, `Recipient`, or the `SubjectConfirmation`
@@ -255,13 +264,13 @@ Ruby SAML allows different ways to validate the signature of the SAML Response:
255
264
`idp_cert_fingerprint` and `idp_cert_fingerprint_algorithm` parameters.
256
265
257
266
In addition, you may pass the option `:relax_signature_validation` to `SloLogoutrequest` and
258
-
`Logoutresponse` if want to skip signature validation on logout.
267
+
`Logoutresponse` if you want to skip signature validation on logout.
259
268
260
269
The `idp_cert_fingerprint` option is deprecated for the following reasons. It will be
261
270
removed in Ruby SAML version 2.1.0.
262
271
1. It only works with HTTP-POST binding, not HTTP-Redirect, since the full certificate
263
272
is not sent in the Redirect URL parameters.
264
-
2. It is theoretically be susceptible to collision attacks, by which a malicious
273
+
2. It is theoretically susceptible to collision attacks, by which a malicious
265
274
actor could impersonate the IdP. (However, as of January 2025, such attacks have not
266
275
been publicly demonstrated for SHA-256.)
267
276
3. It has been removed already from several other SAML libraries in other languages.
@@ -365,8 +374,7 @@ Those return an Hash instead of a `Settings` object, which may be useful for con
365
374
366
375
### Validating Signature of Metadata and retrieve settings
367
376
368
-
Right now there is no method at ruby_saml to validate the signature of the metadata that gonna be parsed,
369
-
but it can be done as follows:
377
+
Right now there is no method at ruby_saml to validate the signature of the metadata that is going to be parsed, but it can be done as follows:
370
378
* Download the XML.
371
379
* Validate the Signature, providing the cert.
372
380
* Provide the XML to the parse method if the signature was validated
You may require the IdP to sign its SAML Assertions using the following setting.
658
-
With will add `<md:SPSSODescriptor WantAssertionsSigned="true">` to your SP Metadata XML.
666
+
This will add `<md:SPSSODescriptor WantAssertionsSigned="true">` to your SP Metadata XML.
659
667
The signature will be checked against the `<md:KeyDescriptor use="signing">` element
660
668
present in the IdP's metadata.
661
669
@@ -687,7 +695,7 @@ advanced usage scenarios:
687
695
- Specifying separate SP certificates for signing and encryption.
688
696
689
697
The `sp_cert_multi` parameter replaces `certificate` and `private_key`
690
-
(you may not specify both pparameters at the same time.) `sp_cert_multi` has the following shape:
698
+
(you may not specify both parameters at the same time.) `sp_cert_multi` has the following shape:
691
699
692
700
```ruby
693
701
settings.sp_cert_multi = {
@@ -729,7 +737,7 @@ JRuby cannot support ECDSA due to a [known issue](https://github.com/jruby/jruby
729
737
### Audience Validation
730
738
731
739
A service provider should only consider a SAML response valid if the IdP includes an <AudienceRestriction>
732
-
element containting an <Audience> element that uniquely identifies the service provider. Unless you specify
740
+
element containing an <Audience> element that uniquely identifies the service provider. Unless you specify
733
741
the `skip_audience` option, Ruby SAML will validate that each SAML response includes an <Audience> element
734
742
whose contents matches `settings.sp_entity_id`.
735
743
@@ -762,7 +770,7 @@ def sp_logout_request
762
770
settings = saml_settings
763
771
764
772
if settings.idp_slo_service_url.nil?
765
-
logger.info "SLO IdP Endpoint not found in settings, executing then a normal logout'"
773
+
logger.info "SLO IdP Endpoint not found in settings, then executing a normal logout'"
766
774
delete_session
767
775
else
768
776
@@ -910,7 +918,7 @@ end
910
918
911
919
### Attribute Service
912
920
913
-
To request attributes from the IdP the SP needs to provide an attribute service within it's metadata and reference the index in the assertion.
921
+
To request attributes from the IdP the SP needs to provide an attribute service within its metadata and reference the index in the assertion.
914
922
915
923
```ruby
916
924
settings =RubySaml::Settings.new
@@ -936,7 +944,7 @@ or underscore, and can only contain letters, digits, underscores, hyphens, and p
936
944
937
945
### Custom Metadata Fields
938
946
939
-
Some IdPs may require to add SPs to add additional fields (Organization, ContactPerson, etc.)
947
+
Some IdPs may require SPs to add additional fields (Organization, ContactPerson, etc.)
940
948
into the SP metadata. This can be done by extending the `RubySaml::Metadata` class and
941
949
overriding the `#add_extras` method where the first arg is a
942
950
[Nokogiri::XML::Builder](https://nokogiri.org/rdoc/Nokogiri/XML/Builder.html) object as per
@@ -964,6 +972,111 @@ end
964
972
MyMetadata.new.generate(settings)
965
973
```
966
974
975
+
### Preventing Replay Attacks
976
+
977
+
A replay attack is when an attacker intercepts a valid SAML assertion and "replays" it at a later time to gain unauthorized access.
978
+
979
+
The library only checks the assertion's validity window (`NotBefore` and `NotOnOrAfter` conditions). An attacker can replay a valid assertion as many times as they want within this window.
980
+
981
+
A robust defense requires tracking of assertion IDs to ensure any given assertion is only accepted once.
982
+
983
+
#### 1. Extract the Assertion ID after Validation
984
+
985
+
After a response has been successfully validated, get the assertion ID. The library makes this available via `response.assertion_id`.
986
+
987
+
988
+
#### 2. Store the ID with an Expiry
989
+
990
+
You must store this ID in a persistent cache (like Redis or Memcached) that is shared across your servers. Do not store it in the user's session, as that is not a secure cache.
991
+
992
+
The ID should be stored until the assertion's validity window has passed. You will need to check how long the trusted IdPs consider the assertion valid and then add the allowed_clock_drift.
993
+
994
+
You can define a global value, or set this value dinamically based on the `not_on_or_after` value of the re + `allowed_clock_drift`.
995
+
996
+
```ruby
997
+
# In your `consume` action, after a successful validation:
998
+
if response.is_valid?
999
+
# Prevent replay of this specific assertion
1000
+
assertion_id = response.assertion_id
1001
+
authorize_failure("Assertion ID is mandatory") if assertion_id.nil?
### Enforce SP-Initiated Flow with `InResponseTo` validation
1039
+
1040
+
This is the best way to prevent IdP-initiated logins and ensure that you only accept assertions that you recently requested.
1041
+
1042
+
#### 1. Store the `AuthnRequest` ID
1043
+
1044
+
When you create an `AuthnRequest`, the library assigns it a unique ID. You must store this ID, for example in the user's session *before* redirecting them to the IdP.
1045
+
1046
+
```ruby
1047
+
definit
1048
+
request =OneLogin::RubySaml::Authrequest.new
1049
+
# The unique ID of the request is in request.uuid
1050
+
session[:saml_request_id] = request.uuid
1051
+
redirect_to(request.create(saml_settings))
1052
+
end
1053
+
```
1054
+
1055
+
#### 2. Validate the `InResponseTo` value of the `Response` with the Stored ID
1056
+
1057
+
When you process the `SAMLResponse`, retrieve the ID from the session and pass it to the `Response` constructor. Use `session.delete` to ensure the ID can only be used once.
1058
+
1059
+
```ruby
1060
+
defconsume
1061
+
request_id = session.delete(:saml_request_id) # Use delete to prevent re-use
1062
+
1063
+
# You can reject the response if no previous saml_request_id was stored
1064
+
raise"IdP-initiaited detected"if request_id.nil?
1065
+
1066
+
response =OneLogin::RubySaml::Response.new(
1067
+
params[:SAMLResponse],
1068
+
settings: saml_settings,
1069
+
matches_request_id: request_id
1070
+
)
1071
+
1072
+
if response.is_valid?
1073
+
# ... authorize user
1074
+
else
1075
+
# Response is invalid, errors in response.errors
1076
+
end
1077
+
end
1078
+
```
1079
+
967
1080
## Contributing
968
1081
969
1082
### Pay it Forward: Support RubySAML and Strengthen Open-Source Security
@@ -998,9 +1111,17 @@ behind paywalls.
998
1111
999
1112
### Sponsors
1000
1113
1001
-
Thanks to the following sponsors for securing the open source ecosystem.
1114
+
Thanks to the following sponsors for securing the open source ecosystem:
RubySaml `1.x` used a combination of REXML and Nokogiri for XML parsing and generation.
77
85
In `2.0.0`, REXML has been replaced with Nokogiri. As a result, there are minor differences
78
-
in how XML is generated, ncluding SAML requests and SP Metadata:
86
+
in how XML is generated, including SAML requests and SP Metadata:
79
87
80
88
1. All XML namespace declarations will be on the root node of the XML. Previously,
81
89
some declarations such as `xmlns:ds` were done on child nodes.
@@ -121,7 +129,7 @@ The reasons for this change are:
121
129
### Removal of embed_sign setting
122
130
123
131
The deprecated `settings.security[:embed_sign]` parameter has been removed. If you were using it, please instead switch
124
-
to using both the `settings.idp_sso_service_binding` and `settings.idp_slo_service_binding` parameters as show below.
132
+
to using both the `settings.idp_sso_service_binding` and `settings.idp_slo_service_binding` parameters as shown below.
125
133
(This new syntax is supported on version 1.13.0 and later.)
126
134
127
135
```ruby
@@ -230,8 +238,8 @@ how validation happens in the toolkit and also the toolkit by default will check
230
238
when parsing a SAML Message (`settings.check_malformed_doc`).
231
239
232
240
The SignedDocument class defined at xml_security.rb experienced several changes.
233
-
We don't expect compatibilty issues if you use the main methods offered by ruby-saml, but if
234
-
you use a fork or customized usage, is possible that you need to adapt your code.
241
+
We don't expect compatibility issues if you use the main methods offered by ruby-saml, but if
242
+
you use a fork or customized usage, it is possible that you will need to adapt your code.
235
243
236
244
## Upgrading from 1.12.x to 1.13.0
237
245
@@ -257,7 +265,7 @@ in favor of `idp_sso_service_url` and `idp_slo_service_url`. The `IdpMetadataPar
257
265
258
266
## Upgrading from 1.10.x to 1.11.0
259
267
260
-
Version `1.11.0` deprecates the use of `settings.issuer` in favour of `settings.sp_entity_id`.
268
+
Version `1.11.0` deprecates the use of `settings.issuer` in favor of `settings.sp_entity_id`.
261
269
There are two new security settings: `settings.security[:check_idp_cert_expiration]` and
262
270
`settings.security[:check_sp_cert_expiration]` (both false by default) that check if the
263
271
IdP or SP X.509 certificate has expired, respectively.
@@ -268,7 +276,7 @@ Version `1.10.1` improves Ruby 1.8.7 support.
268
276
269
277
## Upgrading from 1.9.0 to 1.10.0
270
278
271
-
Version `1.10.0` improves IdpMetadataParser to allow parse multiple IDPSSODescriptor,
279
+
Version `1.10.0` improves IdpMetadataParser to allow parsing multiple IDPSSODescriptor,
272
280
Add Subject support on AuthNRequest to allow SPs provide info to the IdP about the user
273
281
to be authenticated and updates the format_cert method to accept certs with /\x0d/
274
282
@@ -352,7 +360,7 @@ It adds security improvements in order to prevent Signature wrapping attacks.
352
360
353
361
## Upgrading from 1.1.x to 1.2.x
354
362
355
-
Version `1.2` adds IDP metadata parsing improvements, uuid deprecation in favour of SecureRandom,
363
+
Version `1.2` adds IDP metadata parsing improvements, uuid deprecation in favor of SecureRandom,
356
364
refactor error handling and some minor improvements.
357
365
358
366
There is no compatibility issue detected.
@@ -367,7 +375,7 @@ Version `1.1` adds some improvements on signature validation and solves some nam
367
375
368
376
Version `1.0` is a recommended update for all Ruby SAML users as it includes security fixes.
369
377
370
-
Version `1.0` adds security improvements like entity expansion limitation, more SAML message validations, and other important improvements like decrypt support.
378
+
Version `1.0` adds security improvements like entity expansion limitation, more SAML message validations, and other important improvements like decryption support.
0 commit comments