Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public Saml2Settings() {
private String nameIdEncryptionAlgorithm = Constants.RSA_1_5;
private Set<String> allowedKeyTransportAlgorithms;
private boolean rejectUnsolicitedResponsesWithInResponseTo;
private boolean rejectMissingAudience;
private boolean allowRepeatAttributeName;
private boolean rejectDeprecatedAlg;
private String uniqueIdPrefix;
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="pfxc32aed67-820f-4296-0c20-205a10dd5787" Version="2.0" IssueInstant="2011-06-17T14:54:14Z" Destination="http://localhost:8080/java-saml-jspsample/acs.jsp">
<saml:Issuer>https://pitbulk.no-ip.org/simplesaml/saml2/idp/metadata.php</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" ID="pfx7841991c-c73f-4035-e2ee-c170c0e1d3e4" Version="2.0" IssueInstant="2011-06-17T14:54:14Z">
<saml:Issuer>https://pitbulk.no-ip.org/simplesaml/saml2/idp/metadata.php</saml:Issuer>
<saml:Subject>
<saml:NameID SPNameQualifier="hello.com" Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">someone@example.com</saml:NameID>
<saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
<saml:SubjectConfirmationData NotOnOrAfter="2020-06-17T14:59:14Z" Recipient="http://localhost:8080/java-saml-jspsample/acs.jsp"/>
</saml:SubjectConfirmation>
</saml:Subject>
<saml:Conditions NotBefore="2010-06-17T14:53:44Z" NotOnOrAfter="2021-06-17T14:59:14Z">
<saml:AudienceRestriction>
<saml:Audience>http://other-sp.example.com/metadata</saml:Audience>
</saml:AudienceRestriction>
</saml:Conditions>
<saml:AuthnStatement AuthnInstant="2011-06-17T14:54:07Z" SessionNotOnOrAfter="2021-06-17T22:54:14Z" SessionIndex="_51be37965feb5579d803141076936dc2e9d1d98ebf">
<saml:AuthnContext>
<saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef>
</saml:AuthnContext>
</saml:AuthnStatement>
<saml:AttributeStatement>
<saml:Attribute Name="mail" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml:AttributeValue xsi:type="xs:string">someone@example.com</saml:AttributeValue>
</saml:Attribute>
</saml:AttributeStatement>
</saml:Assertion>
</samlp:Response>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="pfxc32aed67-820f-4296-0c20-205a10dd5787" Version="2.0" IssueInstant="2011-06-17T14:54:14Z" Destination="http://localhost:8080/java-saml-jspsample/acs.jsp">
<saml:Issuer>https://pitbulk.no-ip.org/simplesaml/saml2/idp/metadata.php</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" ID="pfx7841991c-c73f-4035-e2ee-c170c0e1d3e4" Version="2.0" IssueInstant="2011-06-17T14:54:14Z">
<saml:Issuer>https://pitbulk.no-ip.org/simplesaml/saml2/idp/metadata.php</saml:Issuer>
<saml:Subject>
<saml:NameID SPNameQualifier="hello.com" Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">someone@example.com</saml:NameID>
<saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
<saml:SubjectConfirmationData NotOnOrAfter="2020-06-17T14:59:14Z" Recipient="http://localhost:8080/java-saml-jspsample/acs.jsp"/>
</saml:SubjectConfirmation>
</saml:Subject>
<saml:Conditions NotBefore="2010-06-17T14:53:44Z" NotOnOrAfter="2021-06-17T14:59:14Z">
</saml:Conditions>
<saml:AuthnStatement AuthnInstant="2011-06-17T14:54:07Z" SessionNotOnOrAfter="2021-06-17T22:54:14Z" SessionIndex="_51be37965feb5579d803141076936dc2e9d1d98ebf">
<saml:AuthnContext>
<saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef>
</saml:AuthnContext>
</saml:AuthnStatement>
<saml:AttributeStatement>
<saml:Attribute Name="mail" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml:AttributeValue xsi:type="xs:string">someone@example.com</saml:AttributeValue>
</saml:Attribute>
</saml:AttributeStatement>
</saml:Assertion>
</samlp:Response>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading