diff --git a/CLAUDE.md b/CLAUDE.md index 0fc72be..ed9462e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -48,7 +48,7 @@ Settings loaded from `onelogin.saml.properties` on classpath or programmatically ### Security Defaults -SHA-256 digest, RSA-SHA256 signatures, 120s clock drift tolerance. `strict` mode must be TRUE in production. By default, deprecated algorithms are NOT rejected (`rejectDeprecatedAlg` defaults to `false`) and the IdP cert fingerprint algorithm defaults to `sha1`. For production, explicitly set `rejectDeprecatedAlg=true`, `idpCertFingerprintAlgorithm=sha256`, and `wantAssertionsSigned`/`wantMessagesSigned=true`. +SHA-256 digest, RSA-SHA256 signatures, 120s clock drift tolerance. `strict` mode must be TRUE in production. By default, deprecated algorithms are NOT rejected (`rejectDeprecatedAlg` defaults to `false`) and the IdP cert fingerprint algorithm defaults to `sha1`. For production, explicitly set `rejectDeprecatedAlg=true`, `idpCertFingerprintAlgorithm=sha256`, and `wantAssertionsSigned`/`wantMessagesSigned=true`. An Assertion with no Audience is also accepted by default; set `rejectMissingAudience=true` (`onelogin.saml2.security.reject_missing_audience`) to reject it. ## Test Resources diff --git a/README.md b/README.md index 9b2dd04..7f4d980 100644 --- a/README.md +++ b/README.md @@ -413,6 +413,11 @@ onelogin.saml2.security.digest_algorithm = http://www.w3.org/2001/04/xmlenc#sha2 # Reject Signatures with deprecated algorithms (sha1) onelogin.saml2.security.reject_deprecated_alg = true +# Reject an Assertion that carries no Audience (AudienceRestriction). +# By default an Assertion with no Audience is accepted; an Assertion that lists +# Audiences without the SP entity ID is always rejected. +onelogin.saml2.security.reject_missing_audience = false + # Enable trimming of parsed Name IDs and attribute values # SAML specification states that no trimming for string elements should be performed, so no trimming will be # performed by default on extracted Name IDs and attribute values. However, some SAML implementations may add diff --git a/core/src/main/java/org/codelibs/saml2/core/authn/SamlResponse.java b/core/src/main/java/org/codelibs/saml2/core/authn/SamlResponse.java index 36623b6..fa0eafe 100644 --- a/core/src/main/java/org/codelibs/saml2/core/authn/SamlResponse.java +++ b/core/src/main/java/org/codelibs/saml2/core/authn/SamlResponse.java @@ -1275,12 +1275,13 @@ protected Document getSAMLResponseDocument() { /** * Validates the audiences. * - * - * + * An Assertion with no Audience is accepted unless {@link Saml2Settings#isRejectMissingAudience()} is set. */ protected void validateAudiences() { final List validAudiences = getAudiences(); - if (!validAudiences.isEmpty() && !validAudiences.contains(settings.getSpEntityId())) { + final boolean wrongAudience = validAudiences.isEmpty() ? settings.isRejectMissingAudience() + : !validAudiences.contains(settings.getSpEntityId()); + if (wrongAudience) { throw new ValidationException(settings.getSpEntityId() + " is not a valid audience for this Response", ValidationException.WRONG_AUDIENCE); } diff --git a/core/src/main/java/org/codelibs/saml2/core/settings/Saml2Settings.java b/core/src/main/java/org/codelibs/saml2/core/settings/Saml2Settings.java index 3895ae1..aa7274f 100644 --- a/core/src/main/java/org/codelibs/saml2/core/settings/Saml2Settings.java +++ b/core/src/main/java/org/codelibs/saml2/core/settings/Saml2Settings.java @@ -86,6 +86,7 @@ public Saml2Settings() { private String nameIdEncryptionAlgorithm = Constants.RSA_1_5; private Set allowedKeyTransportAlgorithms; private boolean rejectUnsolicitedResponsesWithInResponseTo; + private boolean rejectMissingAudience; private boolean allowRepeatAttributeName; private boolean rejectDeprecatedAlg; private String uniqueIdPrefix; @@ -1014,6 +1015,32 @@ public boolean isRejectUnsolicitedResponsesWithInResponseTo() { return rejectUnsolicitedResponsesWithInResponseTo; } + /** + * Controls if a Response whose Assertion carries no Audience is rejected. + * + * If false (the default) an Assertion with no AudienceRestriction/Audience is accepted, and only an Assertion that + * lists Audiences, none of which is the SP entity ID, is rejected. + * + * If true an Assertion with no Audience is rejected as well, with the same error as an Audience that does not + * match the SP entity ID ({@link org.codelibs.saml2.core.exception.ValidationException#WRONG_AUDIENCE}). The Web + * Browser SSO profile requires a bearer Assertion to carry an AudienceRestriction naming the SP, and an Assertion + * without one can be replayed at any other SP that trusts the same IdP. + * + * @param rejectMissingAudience whether to require the Assertion to carry an Audience + */ + public void setRejectMissingAudience(final boolean rejectMissingAudience) { + this.rejectMissingAudience = rejectMissingAudience; + } + + /** + * Returns whether a Response whose Assertion carries no Audience is rejected. + * + * @return the rejectMissingAudience setting value + */ + public boolean isRejectMissingAudience() { + return rejectMissingAudience; + } + /** * Set the compressRequest setting value * diff --git a/core/src/main/java/org/codelibs/saml2/core/settings/SettingsBuilder.java b/core/src/main/java/org/codelibs/saml2/core/settings/SettingsBuilder.java index d2f387f..439420c 100644 --- a/core/src/main/java/org/codelibs/saml2/core/settings/SettingsBuilder.java +++ b/core/src/main/java/org/codelibs/saml2/core/settings/SettingsBuilder.java @@ -178,6 +178,8 @@ public SettingsBuilder() { /** Property key controlling whether unsolicited responses with an InResponseTo attribute are rejected ({@code onelogin.saml2.security.reject_unsolicited_responses_with_inresponseto}). */ public static final String SECURITY_REJECT_UNSOLICITED_RESPONSES_WITH_INRESPONSETO = "onelogin.saml2.security.reject_unsolicited_responses_with_inresponseto"; + /** Property key controlling whether a Response whose Assertion has no Audience is rejected ({@code onelogin.saml2.security.reject_missing_audience}). */ + public static final String SECURITY_REJECT_MISSING_AUDIENCE = "onelogin.saml2.security.reject_missing_audience"; /** Property key controlling whether duplicated attribute names are allowed ({@code onelogin.saml2.security.allow_duplicated_attribute_name}). */ public static final String SECURITY_ALLOW_REPEAT_ATTRIBUTE_NAME_PROPERTY_KEY = "onelogin.saml2.security.allow_duplicated_attribute_name"; @@ -550,6 +552,11 @@ private void loadSecuritySetting() { saml2Settings.setRejectUnsolicitedResponsesWithInResponseTo(rejectUnsolicitedResponsesWithInResponseTo); } + final Boolean rejectMissingAudience = loadBooleanProperty(SECURITY_REJECT_MISSING_AUDIENCE); + if (rejectMissingAudience != null) { + saml2Settings.setRejectMissingAudience(rejectMissingAudience); + } + final Boolean allowRepeatAttributeName = loadBooleanProperty(SECURITY_ALLOW_REPEAT_ATTRIBUTE_NAME_PROPERTY_KEY); if (allowRepeatAttributeName != null) { saml2Settings.setAllowRepeatAttributeName(allowRepeatAttributeName); diff --git a/core/src/test/java/org/codelibs/saml2/core/test/authn/AuthnResponseTest.java b/core/src/test/java/org/codelibs/saml2/core/test/authn/AuthnResponseTest.java index 76e2d18..55bf080 100644 --- a/core/src/test/java/org/codelibs/saml2/core/test/authn/AuthnResponseTest.java +++ b/core/src/test/java/org/codelibs/saml2/core/test/authn/AuthnResponseTest.java @@ -2610,6 +2610,60 @@ public void testUnexpectedRequestId() throws Exception { "The Response has an InResponseTo attribute: ONELOGIN_5fe9d6e499b2f0913206aab3f7191729049bb807 while no InResponseTo was expected"); } + @Test + public void testRejectMissingAudienceIsOffByDefault() throws Exception { + final Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build(); + settings.setWantAssertionsSigned(false); + settings.setWantMessagesSigned(true); + assertFalse(settings.isRejectMissingAudience()); + + final String samlResponseEncoded = loadSignMessageAndEncode("data/responses/invalids/invalid_no_audience_restriction.xml"); + + assertResponseValid(settings, samlResponseEncoded, true, true, null); + } + + @Test + public void testRejectMissingAudience() throws Exception { + final Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build(); + settings.setWantAssertionsSigned(false); + settings.setWantMessagesSigned(true); + settings.setRejectMissingAudience(true); + + final String samlResponseEncoded = loadSignMessageAndEncode("data/responses/invalids/invalid_no_audience_restriction.xml"); + + assertResponseValid(settings, samlResponseEncoded, true, false, + "http://localhost:8080/java-saml-jspsample/metadata.jsp is not a valid audience for this Response"); + final SamlResponse samlResponse = new SamlResponse(settings, newHttpRequest(samlResponseEncoded)); + assertFalse(samlResponse.isValid()); + assertEquals(ValidationException.WRONG_AUDIENCE, ((ValidationException) samlResponse.getValidationException()).getErrorCode()); + } + + @Test + public void testRejectMissingAudienceAcceptsMatchingAudience() throws Exception { + final Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build(); + settings.setWantAssertionsSigned(false); + settings.setWantMessagesSigned(true); + settings.setRejectMissingAudience(true); + + final String samlResponseEncoded = loadSignMessageAndEncode("data/responses/valid_idp_initiated_response.xml"); + + assertResponseValid(settings, samlResponseEncoded, true, true, null); + } + + @Test + public void testMismatchedAudienceIsRejectedWhetherOrNotMissingAudienceIsRejected() throws Exception { + final String samlResponseEncoded = loadSignMessageAndEncode("data/responses/invalids/invalid_audience_idp_initiated.xml"); + for (final boolean rejectMissingAudience : new boolean[] { false, true }) { + final Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build(); + settings.setWantAssertionsSigned(false); + settings.setWantMessagesSigned(true); + settings.setRejectMissingAudience(rejectMissingAudience); + + assertResponseValid(settings, samlResponseEncoded, true, false, + "http://localhost:8080/java-saml-jspsample/metadata.jsp is not a valid audience for this Response"); + } + } + @Test public void testMissingExpectedRequestId() throws Exception { Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build(); diff --git a/core/src/test/java/org/codelibs/saml2/core/test/settings/SettingBuilderTest.java b/core/src/test/java/org/codelibs/saml2/core/test/settings/SettingBuilderTest.java index fa7d1be..56eda5e 100644 --- a/core/src/test/java/org/codelibs/saml2/core/test/settings/SettingBuilderTest.java +++ b/core/src/test/java/org/codelibs/saml2/core/test/settings/SettingBuilderTest.java @@ -713,6 +713,22 @@ public void testLoadFromFileMinUniqueIDProp() throws Exception { assertEquals("_", setting.getUniqueIDPrefix()); } + /** + * Tests that the reject-missing-audience security option is off by default and can be enabled by property. + */ + @Test + public void testRejectMissingAudienceProperty() throws Exception { + assertEquals("onelogin.saml2.security.reject_missing_audience", SettingsBuilder.SECURITY_REJECT_MISSING_AUDIENCE); + assertFalse(new SettingsBuilder().fromFile("config/config.min.properties").build().isRejectMissingAudience()); + + final Properties prop = new Properties(); + prop.setProperty(SettingsBuilder.SECURITY_REJECT_MISSING_AUDIENCE, "true"); + assertTrue(new SettingsBuilder().fromProperties(prop).build().isRejectMissingAudience()); + + prop.setProperty(SettingsBuilder.SECURITY_REJECT_MISSING_AUDIENCE, "false"); + assertFalse(new SettingsBuilder().fromProperties(prop).build().isRejectMissingAudience()); + } + /** * Tests SettingsBuilder fromProperties method * diff --git a/core/src/test/resources/data/responses/invalids/invalid_audience_idp_initiated.xml b/core/src/test/resources/data/responses/invalids/invalid_audience_idp_initiated.xml new file mode 100644 index 0000000..794339e --- /dev/null +++ b/core/src/test/resources/data/responses/invalids/invalid_audience_idp_initiated.xml @@ -0,0 +1,31 @@ + + + https://pitbulk.no-ip.org/simplesaml/saml2/idp/metadata.php + + + + + https://pitbulk.no-ip.org/simplesaml/saml2/idp/metadata.php + + someone@example.com + + + + + + + http://other-sp.example.com/metadata + + + + + urn:oasis:names:tc:SAML:2.0:ac:classes:Password + + + + + someone@example.com + + + + \ No newline at end of file diff --git a/core/src/test/resources/data/responses/invalids/invalid_no_audience_restriction.xml b/core/src/test/resources/data/responses/invalids/invalid_no_audience_restriction.xml new file mode 100644 index 0000000..a035c4c --- /dev/null +++ b/core/src/test/resources/data/responses/invalids/invalid_no_audience_restriction.xml @@ -0,0 +1,28 @@ + + + https://pitbulk.no-ip.org/simplesaml/saml2/idp/metadata.php + + + + + https://pitbulk.no-ip.org/simplesaml/saml2/idp/metadata.php + + someone@example.com + + + + + + + + + urn:oasis:names:tc:SAML:2.0:ac:classes:Password + + + + + someone@example.com + + + + \ No newline at end of file diff --git a/samples/java-saml-tookit-jspsample/src/main/resources/onelogin.saml.properties b/samples/java-saml-tookit-jspsample/src/main/resources/onelogin.saml.properties index e724970..abca2d2 100644 --- a/samples/java-saml-tookit-jspsample/src/main/resources/onelogin.saml.properties +++ b/samples/java-saml-tookit-jspsample/src/main/resources/onelogin.saml.properties @@ -158,6 +158,9 @@ onelogin.saml2.security.digest_algorithm = http://www.w3.org/2001/04/xmlenc#sha2 # Reject Signatures with deprecated algorithms (sha1) onelogin.saml2.security.reject_deprecated_alg = true +# Reject an Assertion that carries no Audience (AudienceRestriction) +onelogin.saml2.security.reject_missing_audience = false + # Organization onelogin.saml2.organization.name = SP Java onelogin.saml2.organization.displayname = SP Java Example