|
| 1 | +/* |
| 2 | + * The contents of this file are subject to the terms of the Common Development and |
| 3 | + * Distribution License (the License). You may not use this file except in compliance with the |
| 4 | + * License. |
| 5 | + * |
| 6 | + * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the |
| 7 | + * specific language governing permission and limitations under the License. |
| 8 | + * |
| 9 | + * When distributing Covered Software, include this CDDL Header Notice in each file and include |
| 10 | + * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL |
| 11 | + * Header, with the fields enclosed by brackets [] replaced by your own identifying |
| 12 | + * information: "Portions Copyright [year] [name of copyright owner]". |
| 13 | + * |
| 14 | + * Portions Copyright 2026 3A Systems, LLC |
| 15 | + */ |
| 16 | +package org.opends.server.authorization.dseecompat; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import org.opends.server.DirectoryServerTestCase; |
| 21 | +import org.opends.server.TestCaseUtils; |
| 22 | +import org.opends.server.types.DirectoryException; |
| 23 | +import org.testng.annotations.AfterClass; |
| 24 | +import org.testng.annotations.BeforeClass; |
| 25 | +import org.testng.annotations.DataProvider; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +/** |
| 29 | + * Verifies that {@link BindRule#decode(String)} treats parentheses embedded in a |
| 30 | + * quoted bind rule expression (e.g. a DN value containing '(' or ')') as literal |
| 31 | + * data instead of grouping parentheses. Before the quote-aware scan, a grouped |
| 32 | + * bind rule whose value contained a parenthesis was wrongly rejected because the |
| 33 | + * embedded parenthesis was counted when locating the matching close parenthesis. |
| 34 | + * Also verifies the scan stays fail-closed: genuinely unbalanced grouping |
| 35 | + * parentheses (outside quotes) are still rejected with an {@link AciException}. |
| 36 | + */ |
| 37 | +@SuppressWarnings("javadoc") |
| 38 | +public class BindRuleParenTest extends DirectoryServerTestCase |
| 39 | +{ |
| 40 | + @BeforeClass |
| 41 | + public void setUp() throws Exception |
| 42 | + { |
| 43 | + TestCaseUtils.startFakeServer(); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterClass |
| 47 | + public void tearDown() throws DirectoryException |
| 48 | + { |
| 49 | + TestCaseUtils.shutdownFakeServer(); |
| 50 | + } |
| 51 | + |
| 52 | + @DataProvider(name = "acisWithParenInValue") |
| 53 | + public Object[][] acisWithParenInValue() |
| 54 | + { |
| 55 | + return new Object[][] { |
| 56 | + // simple (ungrouped) bind rule with a ')' / '(' in the DN value |
| 57 | + { "(version 3.0; acl \"t\"; allow (search) userdn=\"ldap:///cn=a)b,dc=x\";)" }, |
| 58 | + { "(version 3.0; acl \"t\"; allow (search) userdn=\"ldap:///cn=a(b,dc=x\";)" }, |
| 59 | + // grouped bind rule with a ')' embedded in a value |
| 60 | + { "(version 3.0; acl \"t\"; allow (search) " |
| 61 | + + "(userdn=\"ldap:///cn=a)b,dc=x\" or userdn=\"ldap:///self\");)" }, |
| 62 | + // grouped bind rule with a '(' embedded in a value |
| 63 | + { "(version 3.0; acl \"t\"; allow (search) " |
| 64 | + + "(userdn=\"ldap:///cn=a(b,dc=x\" or userdn=\"ldap:///self\");)" }, |
| 65 | + // grouped bind rule with matched parentheses embedded in a value |
| 66 | + { "(version 3.0; acl \"t\"; allow (search) " |
| 67 | + + "(userdn=\"ldap:///cn=a(b),dc=x\" or userdn=\"ldap:///self\");)" }, |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + @Test(dataProvider = "acisWithParenInValue") |
| 72 | + public void decodesParenInsideQuotedValue(String aci) throws Exception |
| 73 | + { |
| 74 | + AciBody body = AciBody.decode(aci); |
| 75 | + assertThat(body.getPermBindRulePairs()).hasSize(1); |
| 76 | + assertThat(body.getPermBindRulePairs().get(0).getBindRule()).isNotNull(); |
| 77 | + } |
| 78 | + |
| 79 | + @DataProvider(name = "malformedBindRules") |
| 80 | + public Object[][] malformedBindRules() |
| 81 | + { |
| 82 | + return new Object[][] { |
| 83 | + // grouped bind rule with no close parenthesis at all |
| 84 | + { "(userdn=\"ldap:///self\"" }, |
| 85 | + // grouped bind rule with one extra (unmatched) open parenthesis |
| 86 | + { "((userdn=\"ldap:///self\")" }, |
| 87 | + // grouped bind rule whose only ')' is embedded in a quoted value, so the |
| 88 | + // group is genuinely unterminated: the quote-aware scan must not treat the |
| 89 | + // embedded ')' as the closing parenthesis and silently accept the input. |
| 90 | + { "(userdn=\"ldap:///cn=a)b,dc=x\" or userdn=\"ldap:///self\"" }, |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + @Test(dataProvider = "malformedBindRules") |
| 95 | + public void rejectsUnbalancedParensOutsideQuotes(String bindRule) |
| 96 | + { |
| 97 | + assertThatThrownBy(() -> BindRule.decode(bindRule)).isInstanceOf(AciException.class); |
| 98 | + } |
| 99 | +} |
0 commit comments