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

Configure Signup to only be allowed from same origin #186

Open
R-Sandor opened this issue Aug 29, 2024 · 0 comments
Open

Configure Signup to only be allowed from same origin #186

R-Sandor opened this issue Aug 29, 2024 · 0 comments

Comments

@R-Sandor
Copy link
Owner

R-Sandor commented Aug 29, 2024

Sample code from research on the topic:

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DomainAndUserAgentFilter extends HttpFilter {

    private static final String ALLOWED_DOMAIN = "mywebsite.com";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Initialization code, if needed
    }

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        String referer = request.getHeader("Referer");
        String origin = request.getHeader("Origin");
        String userAgent = request.getHeader("User-Agent");

        if ((referer != null && referer.contains(ALLOWED_DOMAIN)) || 
            (origin != null && origin.contains(ALLOWED_DOMAIN)) ||
            (userAgent != null && isBrowser(userAgent))) {
            chain.doFilter(request, response);
        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access denied");
        }
    }

    private boolean isBrowser(String userAgent) {
        // Simple check for common browser user agents
        return userAgent.contains("Mozilla") || userAgent.contains("Chrome") || userAgent.contains("Safari");
    }

    @Override
    public void destroy() {
        // Cleanup code, if needed
    }
}
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean<DomainAndUserAgentFilter> domainAndUserAgentFilter() {
        FilterRegistrationBean<DomainAndUserAgentFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new DomainAndUserAgentFilter());
        registrationBean.addUrlPatterns("/your-endpoint/*"); // Specify the endpoints you want to protect
        return registrationBean;
    }
}
@R-Sandor R-Sandor self-assigned this Aug 29, 2024
@R-Sandor R-Sandor removed their assignment Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant