Skip to content

Commit

Permalink
More defensive about content type
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Syer committed Sep 2, 2014
1 parent d684f05 commit fdb6d0e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ApplicationTests {
@BeforeClass
public static void startConfigServer() {
ConfigurableApplicationContext context = SpringApplication.run(
org.springframework.cloud.config.server.Application.class,
org.springframework.cloud.config.server.ConfigServerApplication.class,
"--server.port=" + configPort, "--spring.config.name=server");
configPort = ((EmbeddedWebApplicationContext) context).getEmbeddedServletContainer().getPort();
System.setProperty("config.port", "" + configPort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class Application {
public class ConfigServerApplication {

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.config.Environment;
import org.springframework.cloud.config.PropertySource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.cloud.config.Environment;
import org.springframework.cloud.config.PropertySource;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import org.springframework.security.rsa.crypto.RsaKeyHolder;
import org.springframework.security.rsa.crypto.RsaSecretEncryptor;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -150,31 +151,31 @@ public Map<String, Object> status() {
}

@RequestMapping(value = "encrypt", method = RequestMethod.POST)
public String encrypt(@RequestBody String data) {
public String encrypt(@RequestBody String data, @RequestHeader("Content-Type") MediaType type) {
if (encryptor == null) {
throw new KeyNotInstalledException();
}
data = stripFormData(data);
data = stripFormData(data, type);
return encryptor.encrypt(data);
}

@RequestMapping(value = "decrypt", method = RequestMethod.POST)
public String decrypt(@RequestBody String data) {
public String decrypt(@RequestBody String data, @RequestHeader("Content-Type") MediaType type) {
if (encryptor == null) {
throw new KeyNotInstalledException();
}
try {
data = stripFormData(data);
data = stripFormData(data, type);
return encryptor.decrypt(data);
}
catch (IllegalArgumentException e) {
throw new InvalidCipherException();
}
}

private String stripFormData(String data) {
private String stripFormData(String data, MediaType type) {

if (data.endsWith("=") && !Base64.isBase64(data.getBytes())) {
if (data.endsWith("=") && !type.equals(MediaType.TEXT_PLAIN)) {
// User posted data with content type form but meant it to be text/plain
data = data.substring(0, data.length() - 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@SpringApplicationConfiguration(classes = ConfigServerApplication.class)
@IntegrationTest("server.port:0")
@WebAppConfiguration
public class ApplicationTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.Test;
import org.springframework.cloud.config.Environment;
import org.springframework.cloud.config.PropertySource;
import org.springframework.http.MediaType;
import org.springframework.security.rsa.crypto.RsaSecretEncryptor;

/**
Expand All @@ -36,7 +37,7 @@ public class EncryptionControllerTests {

@Test(expected = KeyNotInstalledException.class)
public void cannotDecryptWithoutKey() {
controller.decrypt("foo");
controller.decrypt("foo", MediaType.TEXT_PLAIN);
}

@Test(expected = KeyFormatException.class)
Expand All @@ -52,21 +53,21 @@ public void cannotUploadPublicKeyPemFormat() {
@Test(expected = InvalidCipherException.class)
public void invalidCipher() {
controller.uploadKey("foo");
controller.decrypt("foo");
controller.decrypt("foo", MediaType.TEXT_PLAIN);
}

@Test
public void sunnyDaySymmetricKey() {
controller.uploadKey("foo");
String cipher = controller.encrypt("foo");
assertEquals("foo", controller.decrypt(cipher));
String cipher = controller.encrypt("foo", MediaType.TEXT_PLAIN);
assertEquals("foo", controller.decrypt(cipher, MediaType.TEXT_PLAIN));
}

@Test
public void sunnyDayRsaKey() {
controller.setEncryptor(new RsaSecretEncryptor());
String cipher = controller.encrypt("foo");
assertEquals("foo", controller.decrypt(cipher));
String cipher = controller.encrypt("foo", MediaType.TEXT_PLAIN);
assertEquals("foo", controller.decrypt(cipher, MediaType.TEXT_PLAIN));
}

@Test
Expand All @@ -79,7 +80,7 @@ public void publicKey() {
@Test
public void decryptEnvironment() {
controller.uploadKey("foo");
String cipher = controller.encrypt("foo");
String cipher = controller.encrypt("foo", MediaType.TEXT_PLAIN);
Environment environment = new Environment("foo", "bar");
environment.add(new PropertySource("spam", Collections
.<Object, Object> singletonMap("my", "{cipher}" + cipher)));
Expand All @@ -90,8 +91,8 @@ public void decryptEnvironment() {
@Test
public void randomizedCipher() {
controller.uploadKey("foo");
String cipher = controller.encrypt("foo");
assertNotEquals(cipher, controller.encrypt("foo"));
String cipher = controller.encrypt("foo", MediaType.TEXT_PLAIN);
assertNotEquals(cipher, controller.encrypt("foo", MediaType.TEXT_PLAIN));
}

}

0 comments on commit fdb6d0e

Please sign in to comment.