Fix encryption of string containing curly braces (#774)

* Add test to reproduce curly brace problem

* Fix stripping prefix if string contains curly brace
This commit is contained in:
Mihaly Nyeste
2017-10-20 22:19:18 +02:00
committed by Spencer Gibb
parent a7bd68cf93
commit ac76f6e970
3 changed files with 25 additions and 1 deletions

View File

@@ -117,7 +117,7 @@ class EnvironmentPrefixHelper {
if (value.contains(ESCAPE)) {
return value.substring(value.indexOf(ESCAPE) + ESCAPE.length());
}
return value.substring(value.lastIndexOf("}") + 1);
return value.replaceFirst("^(\\{.*?:.*?\\})+", "");
}
private String removeEnvironmentPrefix(String input) {

View File

@@ -135,6 +135,20 @@ public class EncryptionControllerTests {
assertThat("Prefix must be stripped prior to encrypt", captor.getValue(), not(containsString("{key:test}")));
}
@Test
public void encryptDecyptTextWithCurlyBrace() {
this.controller = new EncryptionController(
new SingleTextEncryptorLocator(new RsaSecretEncryptor()));
String plain = "textwith}brace";
String cipher = this.controller.encrypt(plain,
MediaType.APPLICATION_FORM_URLENCODED);
String decrypt = this.controller.decrypt(cipher,
MediaType.APPLICATION_FORM_URLENCODED);
assertEquals(plain, decrypt);
}
@Test
public void addEnvironment() {
TextEncryptorLocator locator = new TextEncryptorLocator() {

View File

@@ -81,4 +81,14 @@ public class EnvironmentPrefixHelperTests {
assertEquals("mykey", keys.get("key"));
}
@Test
public void testTextWithCurlyBracesNoPrefix() {
assertEquals("textwith}brac{es", this.helper.stripPrefix("textwith}brac{es"));
}
@Test
public void testTextWithCurlyBracesPrefix() {
assertEquals("textwith}brac{es{and}prefix", this.helper
.stripPrefix("{key:foo}{name:bar}textwith}brac{es{and}prefix"));
}
}