Skip to content

Commit c935ede

Browse files
authored
Legacy passwords may have leading or trailing comment. (#83)
* Legacy passwords may have leading or trailing comment. Fixes #81
1 parent c8a1b02 commit c935ede

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/DefaultSecDispatcher.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,17 @@ public String encrypt(String str, Map<String, String> attr) throws SecDispatcher
133133

134134
@Override
135135
public String decrypt(String str) throws SecDispatcherException, IOException {
136-
if (!isAnyEncryptedString(str)) return str;
137-
String bare = unDecorate(str);
138-
Map<String, String> attr = requireNonNull(stripAttributes(bare));
136+
String bare;
137+
Map<String, String> attr;
139138
if (isLegacyEncryptedString(str)) {
139+
bare = unDecorateLegacy(str);
140+
attr = new HashMap<>();
140141
attr.put(DISPATCHER_NAME_ATTR, LegacyDispatcher.NAME);
142+
} else if (isEncryptedString(str)) {
143+
bare = unDecorate(str);
144+
attr = requireNonNull(stripAttributes(bare));
145+
} else {
146+
return str;
141147
}
142148
String name = attr.get(DISPATCHER_NAME_ATTR);
143149
Dispatcher dispatcher = dispatchers.get(name);
@@ -172,14 +178,20 @@ public boolean isEncryptedString(String str) {
172178
*/
173179
@Override
174180
public boolean isLegacyEncryptedString(String str) {
175-
boolean looksLike = str != null
176-
&& !str.isBlank()
177-
&& str.startsWith(SHIELD_BEGIN)
178-
&& str.endsWith(SHIELD_END)
179-
&& !unDecorate(str).contains(SHIELD_BEGIN)
180-
&& !unDecorate(str).contains(SHIELD_END);
181-
if (looksLike) {
182-
return stripAttributes(unDecorate(str)).isEmpty();
181+
if (str != null && str.contains(SHIELD_BEGIN)) {
182+
str = str.substring(str.indexOf(SHIELD_BEGIN));
183+
if (str.contains(SHIELD_END)) {
184+
str = str.substring(0, str.indexOf(SHIELD_END) + 1);
185+
String undecorated = unDecorate(str);
186+
boolean looksLike = !str.isBlank()
187+
&& str.startsWith(SHIELD_BEGIN)
188+
&& str.endsWith(SHIELD_END)
189+
&& !undecorated.contains(SHIELD_BEGIN)
190+
&& !undecorated.contains(SHIELD_END);
191+
if (looksLike) {
192+
return stripAttributes(undecorated).isEmpty();
193+
}
194+
}
183195
}
184196
return false;
185197
}
@@ -309,4 +321,10 @@ protected Map<String, String> stripAttributes(String str) {
309321
protected String unDecorate(String str) {
310322
return str.substring(SHIELD_BEGIN.length(), str.length() - SHIELD_END.length());
311323
}
324+
325+
protected String unDecorateLegacy(String str) {
326+
str = str.substring(str.indexOf(SHIELD_BEGIN));
327+
str = str.substring(0, str.indexOf(SHIELD_END) + 1);
328+
return unDecorate(str);
329+
}
312330
}

src/test/java/org/codehaus/plexus/components/secdispatcher/internal/DefaultSecDispatcherTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,29 @@ void detection() {
122122

123123
assertFalse(secDispatcher.isEncryptedString("{foo}"));
124124
assertTrue(secDispatcher.isLegacyEncryptedString("{foo}"));
125+
assertFalse(secDispatcher.isEncryptedString("Oleg was here {foo}"));
126+
assertTrue(secDispatcher.isLegacyEncryptedString("Oleg was here {foo}"));
127+
assertTrue(secDispatcher.isLegacyEncryptedString("Oleg {foo} was here"));
125128

126129
assertFalse(secDispatcher.isEncryptedString("{12345678901234567890123456789012345678901234567890}"));
127130
assertTrue(secDispatcher.isLegacyEncryptedString("{12345678901234567890123456789012345678901234567890}"));
131+
assertFalse(
132+
secDispatcher.isEncryptedString("Oleg was here {12345678901234567890123456789012345678901234567890}"));
133+
assertTrue(secDispatcher.isLegacyEncryptedString(
134+
"{12345678901234567890123456789012345678901234567890} Oleg was here"));
135+
assertTrue(secDispatcher.isLegacyEncryptedString(
136+
"Oleg {12345678901234567890123456789012345678901234567890} was here"));
128137

129138
// contains {} in the middle
130139
assertFalse(secDispatcher.isEncryptedString("{KDvsYOFLlX{}gH4LU8tvpzAGg5otiosZXvfdQq0yO86LU=}"));
131140
assertFalse(secDispatcher.isLegacyEncryptedString("{KDvsYOFLlX{}gH4LU8tvpzAGg5otiosZXvfdQq0yO86LU=}"));
141+
assertFalse(secDispatcher.isLegacyEncryptedString(
142+
"Oleg was here {KDvsYOFLlX{}gH4LU8tvpzAGg5otiosZXvfdQq0yO86LU=}"));
132143

133144
assertFalse(secDispatcher.isEncryptedString("{KDvsYOFLlXgH4LU8tvpzAGg5otiosZXvfdQq0yO86LU=}"));
134145
assertTrue(secDispatcher.isLegacyEncryptedString("{KDvsYOFLlXgH4LU8tvpzAGg5otiosZXvfdQq0yO86LU=}"));
146+
assertTrue(
147+
secDispatcher.isLegacyEncryptedString("Oleg was here {KDvsYOFLlXgH4LU8tvpzAGg5otiosZXvfdQq0yO86LU=}"));
135148

136149
assertTrue(
137150
secDispatcher.isEncryptedString(
@@ -192,5 +205,11 @@ void legacy(String xml) throws Exception {
192205
SecDispatcher secDispatcher = construct();
193206
String cleartext = secDispatcher.decrypt("{L6L/HbmrY+cH+sNkphnq3fguYepTpM04WlIXb8nB1pk=}");
194207
assertEquals("password", cleartext);
208+
209+
cleartext = secDispatcher.decrypt("Oleg was here {L6L/HbmrY+cH+sNkphnq3fguYepTpM04WlIXb8nB1pk=}");
210+
assertEquals("password", cleartext);
211+
212+
cleartext = secDispatcher.decrypt("Oleg {L6L/HbmrY+cH+sNkphnq3fguYepTpM04WlIXb8nB1pk=} was here");
213+
assertEquals("password", cleartext);
195214
}
196215
}

0 commit comments

Comments
 (0)