<commit_before>package org.esupportail.smsuapiadmin.web;

import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsuapiadmin.business.UserManager;

public final class AuthAndRoleAndMiscFilter implements Filter {
	
    @Autowired private UserManager userManager;

    private String authentication = "cas";
    private boolean shibUseHeaders = false;

    private String sessionAttributeName = "MY_REMOTE_USER";

    private final Logger logger = new LoggerImpl(getClass());

    public void destroy() {}
    public void init(FilterConfig config) {}

    /**
     * Wraps the HttpServletRequest in a wrapper class that delegates <code>request.isUserInRole</code> 
     */
    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String user = getRemoteUser(request);
	if (user == null) {
	    unauthorized(response);
	    return;
	}

        Set<String> rights = userManager.getUserRights(user);
        if (request.getHeader("X-Impersonate-User") != null) {
        	user = request.getHeader("X-Impersonate-User");
        	rights = userManager.getUserRights(user);
        }
        
        // ugly hack for Internet Explorer. It would need to be done somewhere else...
        response.setHeader("Cache-Control", "no-cache");
        
		filterChain.doFilter(new MyHttpServletRequestWrapper(request, user, rights), response);
    }
    <commit_msg>fix security issue: any logged user could impersonate<commit_after>package org.esupportail.smsuapiadmin.web;

import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsuapiadmin.business.UserManager;

public final class AuthAndRoleAndMiscFilter implements Filter {
	
    @Autowired private UserManager userManager;

    private String authentication = "cas";
    private boolean shibUseHeaders = false;

    private String sessionAttributeName = "MY_REMOTE_USER";
    private String rightAllowingImpersonate = "FCTN_MANAGE_USERS";

    private final Logger logger = new LoggerImpl(getClass());

    public void destroy() {}
    public void init(FilterConfig config) {}

    /**
     * Wraps the HttpServletRequest in a wrapper class that delegates <code>request.isUserInRole</code> 
     */
    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String user = getRemoteUser(request);
	if (user == null) {
	    unauthorized(response);
	    return;
	}

        Set<String> rights = userManager.getUserRights(user);
        if (request.getHeader("X-Impersonate-User") != null) {
	    if (!rights.contains(rightAllowingImpersonate))
	        throw new RuntimeException("Impersonate not allowed");

        	user = request.getHeader("X-Impersonate-User");
        	rights = userManager.getUserRights(user);
        }
        
        // ugly hack for Internet Explorer. It would need to be done somewhere else...
        response.setHeader("Cache-Control", "no-cache");
        
		filterChain.doFilter(new MyHttpServletRequestWrapper(request, user, rights), response);
    }
    
{'fraction_non_alphanumeric': 0.0882415934889698, 'fraction_numerical': 0.0, 'mean_word_length': 4.331050228310502, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4232897', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1237, 'n_words': 296}