Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#13] Throw error when allowed origins are not configured #14

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/main/java/cz/cvut/kbss/study/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ private static void configureAllowedOrigins(CorsConfiguration corsConfig, Config
final List<String> allowedOrigins = new ArrayList<>();
appUrlOrigin.ifPresent(allowedOrigins::add);
final String allowedOriginsConfig = config.getConfig(ConfigParam.CORS_ALLOWED_ORIGINS);
allowedOrigins.addAll(Arrays.asList(allowedOriginsConfig.split(",")));
Arrays.stream(allowedOriginsConfig.split(",")).filter(s -> !s.isBlank()).forEach(allowedOrigins::add);
if (!allowedOrigins.isEmpty()) {
corsConfig.setAllowedOrigins(allowedOrigins);
corsConfig.setAllowCredentials(true);
LOG.debug(
"Using response header Access-Control-Allow-Origin with value {}.",
corsConfig.getAllowedOrigins()
);
} else {
corsConfig.setAllowedOrigins(null);
}
LOG.debug(
"Using response header Access-Control-Allow-Origin with value {}.",
corsConfig.getAllowedOrigins()
);
}

private static Optional<String> getApplicationUrlOrigin(ConfigReader configReader) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/cz/cvut/kbss/study/config/SecurityConfigTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.cvut.kbss.study.config;

import cz.cvut.kbss.study.exception.RecordManagerException;
import cz.cvut.kbss.study.service.ConfigReader;
import cz.cvut.kbss.study.util.ConfigParam;
import org.junit.jupiter.api.Test;
Expand All @@ -11,6 +12,8 @@
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class SecurityConfigTest {

Expand Down Expand Up @@ -55,4 +58,14 @@ void createCorsConfigurationSupportsMultipleConfiguredAllowedOrigins() {
assertThat(result.getCorsConfiguration(new MockHttpServletRequest()).getAllowedOrigins(),
hasItems(originOne, originTwo, originThree));
}

@Test
void createCorsConfigurationDoNotSetAllowedOriginsWhenAppContextAndAllowedOriginsAreNotSet() {
environment.setProperty(ConfigParam.APP_CONTEXT.toString(), "");
environment.setProperty(ConfigParam.CORS_ALLOWED_ORIGINS.toString(),"");

final CorsConfigurationSource result = SecurityConfig.createCorsConfiguration(config);
assertNotNull(result.getCorsConfiguration(new MockHttpServletRequest()));
assertNull(result.getCorsConfiguration(new MockHttpServletRequest()).getAllowedOrigins());
}
}
Loading