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

[Application] Use Spring Security for API Key validation. #30

Closed
binchoo opened this issue Sep 24, 2022 · 1 comment
Closed

[Application] Use Spring Security for API Key validation. #30

binchoo opened this issue Sep 24, 2022 · 1 comment
Assignees
Labels
hotfix update this feature to current release mdl: application stk: paimonganyu-skill

Comments

@binchoo
Copy link
Owner

binchoo commented Sep 24, 2022

As-is:
No API keys are required for calling the paimonganyu-skill endpoints.
If an attacker who uses a fake KakaoTalk chatbot sends a request to our endpoints via the ikakao skill connectors, he may find a botUserId matching one in the PaimonGanyu system with a very low probability.

To-be:
Although the probability is significantly low, I do not want to have this vulnerability affect my system. So I'll implement an API key validation using Spring Security filter chain.

@binchoo binchoo added hotfix update this feature to current release mdl: application stk: paimonganyu-skill labels Sep 24, 2022
@binchoo
Copy link
Owner Author

binchoo commented Sep 24, 2022

Change Impact

  • Spring Security configuration
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${auth.apikey}")
    private String expectedApiKey;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/ikakao/**")
                .authorizeRequests().anyRequest().permitAll()
                .and()
                .addFilterBefore(new ApiKeyValidationFilter(expectedApiKey), BasicAuthenticationFilter.class)
            .csrf().disable();
    }
}
  • API Key validation filter
@Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    var httpRequest = (HttpServletRequest) request;
    var httpResponse = (HttpServletResponse) response;

    String actualApiKey = httpRequest.getHeader(HEADER_X_API_KEY);

    if (expectedApiKey.equals(actualApiKey)) {
        chain.doFilter(request, response);
    } else {
        httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        String clientIp = httpRequest.getHeader(HEADER_X_FORWARDED_FOR);
        logger.warn("[Security] Unauthorized api-key: {} from {}", actualApiKey, clientIp);
    }
}

@binchoo binchoo closed this as completed Sep 24, 2022
@binchoo binchoo self-assigned this Sep 27, 2022
@binchoo binchoo mentioned this issue Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotfix update this feature to current release mdl: application stk: paimonganyu-skill
Projects
None yet
Development

No branches or pull requests

1 participant