Skip to content

Commit b50b829

Browse files
committed
[SHIRO-530] INI parser does not properly handled backslashes at end of values
- Do not skip escape characters for the value (new behaviour demanded by SHIRO-530). - rearrange and comment tests to reflect old and new, desired behaviour. Signed-off-by: Benjamin Marwell <[email protected]>
1 parent 3708d79 commit b50b829

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

config/core/src/main/java/org/apache/shiro/config/Ini.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ protected static String[] splitKeyValue(String keyValueLine) {
587587
} else {
588588
if (valueBuffer.length() == 0 && isKeyValueSeparatorChar(c) && !isCharEscaped(line, i)) {
589589
//swallow the separator chars before we start building the value
590-
} else if (!isCharEscaped(line, i)){
590+
} else {
591591
valueBuffer.append(c);
592592
}
593593
}

config/core/src/test/groovy/org/apache/shiro/config/IniTest.groovy

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ public class IniTest {
7272
assertTrue(Ini.Section.isContinued(line));
7373
}
7474

75+
@Test
76+
public void testBackslash() {
77+
String test = "Truth=Beauty\\\\";
78+
Ini ini = new Ini();
79+
ini.load(test);
80+
81+
assertNotNull(ini.getSections());
82+
assertEquals(1, ini.getSections().size());
83+
84+
Ini.Section section = ini.getSections().iterator().next();
85+
assertEquals(Ini.DEFAULT_SECTION_NAME, section.getName());
86+
assertFalse(section.isEmpty());
87+
assertEquals(1, section.size());
88+
assertEquals("Beauty\\\\", section.get("Truth"));
89+
}
90+
7591
@Test
7692
public void testSplitKeyValue() {
7793
String test = "Truth Beauty";
@@ -119,24 +135,40 @@ public class IniTest {
119135
assertEquals("Truth", kv[0]);
120136
assertEquals("Beauty", kv[1]);
121137

138+
// Escape characters are to be removed from the key.
139+
// This is different behaviour compared to the XML config.
122140
test = "Tru\\th=Beauty";
123141
kv = Ini.Section.splitKeyValue(test);
124142
assertEquals("Truth", kv[0]);
125143
assertEquals("Beauty", kv[1]);
126144

127-
test = "Truth\\=Beauty";
145+
// SHIRO-530: Keep backslashes in value.
146+
test = "Truth=Beau\\ty";
128147
kv = Ini.Section.splitKeyValue(test);
129148
assertEquals("Truth", kv[0]);
130-
assertEquals("Beauty", kv[1]);
149+
assertEquals("Beau\\ty", kv[1]);
131150

132-
test = "Truth=Beau\\ty";
151+
// SHIRO-530: Keep backslashes in value.
152+
test = "Truth=Beauty\\";
133153
kv = Ini.Section.splitKeyValue(test);
134154
assertEquals("Truth", kv[0]);
135-
assertEquals("Beauty", kv[1]);
155+
assertEquals("Beauty\\", kv[1]);
136156

137-
test = "Truth=Beauty\\";
157+
// SHIRO-530: Keep backslashes in value.
158+
test = "Truth= \\ Beauty\\";
138159
kv = Ini.Section.splitKeyValue(test);
139160
assertEquals("Truth", kv[0]);
161+
assertEquals("\\ Beauty\\", kv[1]);
162+
}
163+
164+
/**
165+
* Tests if an escaped separator char will not be recognized as such.
166+
*/
167+
@Test
168+
public void testSplitKeyValueEscapedEquals() {
169+
String test = "Truth\\=Beauty";
170+
String[] kv = Ini.Section.splitKeyValue(test);
171+
assertEquals("Truth", kv[0]);
140172
assertEquals("Beauty", kv[1]);
141173
}
142174

0 commit comments

Comments
 (0)