/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.message.HttpServletRequestKnob
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.InjectionThreatProtectionAssertion
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.message.HttpServletRequestKnob;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.InjectionThreatProtectionAssertion;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Map;

public abstract class ServerInjectionThreatProtectionAssertion<AT extends InjectionThreatProtectionAssertion>
extends AbstractMessageTargetableServerAssertion<AT> {
    private static final String[] g;

    public ServerInjectionThreatProtectionAssertion(AT AT) {
        super(AT);
    }

    protected AssertionStatus applyThreatProtection(PolicyEnforcementContext policyEnforcementContext, Message message, String string) throws IOException {
        AssertionStatus assertionStatus;
        block30: {
            block28: {
                HttpServletRequestKnob httpServletRequestKnob;
                block29: {
                    block27: {
                        boolean bl2;
                        block25: {
                            block26: {
                                bl2 = policyEnforcementContext.isPostRouting();
                                httpServletRequestKnob = null;
                                try {
                                    try {
                                        if (!this.isRequest()) break block25;
                                        if (!bl2) break block26;
                                    }
                                    catch (IOException iOException) {
                                        throw ServerInjectionThreatProtectionAssertion.b(iOException);
                                    }
                                    this.logAndAuditRequestAlreadyRouted();
                                    return AssertionStatus.FAILED;
                                }
                                catch (IOException iOException) {
                                    throw ServerInjectionThreatProtectionAssertion.b(iOException);
                                }
                            }
                            httpServletRequestKnob = (HttpServletRequestKnob)message.getKnob(HttpServletRequestKnob.class);
                            break block27;
                        }
                        try {
                            try {
                                if (!this.isResponse() || bl2) break block27;
                            }
                            catch (IOException iOException) {
                                throw ServerInjectionThreatProtectionAssertion.b(iOException);
                            }
                            this.logAndAuditResponseNotRouted();
                            return AssertionStatus.NONE;
                        }
                        catch (IOException iOException) {
                            throw ServerInjectionThreatProtectionAssertion.b(iOException);
                        }
                    }
                    assertionStatus = AssertionStatus.NONE;
                    try {
                        if (null == httpServletRequestKnob) break block28;
                        if (!((InjectionThreatProtectionAssertion)this.assertion).isIncludeUrlPath()) break block29;
                    }
                    catch (IOException iOException) {
                        throw ServerInjectionThreatProtectionAssertion.b(iOException);
                    }
                    assertionStatus = this.scanHttpRequestUrlPath(httpServletRequestKnob);
                    try {
                        if (assertionStatus != AssertionStatus.NONE) {
                            this.logAndAuditAttackRejected();
                            return assertionStatus;
                        }
                    }
                    catch (IOException iOException) {
                        throw ServerInjectionThreatProtectionAssertion.b(iOException);
                    }
                }
                if (((InjectionThreatProtectionAssertion)this.assertion).isIncludeUrlQueryString()) {
                    assertionStatus = this.scanHttpRequestUrlQueryString(httpServletRequestKnob);
                    try {
                        if (assertionStatus != AssertionStatus.NONE) {
                            this.logAndAuditAttackRejected();
                            return assertionStatus;
                        }
                        break block30;
                    }
                    catch (IOException iOException) {
                        throw ServerInjectionThreatProtectionAssertion.b(iOException);
                    }
                }
                break block30;
            }
            try {
                try {
                    if (!((InjectionThreatProtectionAssertion)this.assertion).isIncludeUrlPath() && !((InjectionThreatProtectionAssertion)this.assertion).isIncludeUrlQueryString()) break block30;
                }
                catch (IOException iOException) {
                    throw ServerInjectionThreatProtectionAssertion.b(iOException);
                }
                this.logAndAuditMessageNotHttp();
            }
            catch (IOException iOException) {
                throw ServerInjectionThreatProtectionAssertion.b(iOException);
            }
        }
        if (((InjectionThreatProtectionAssertion)this.assertion).isIncludeBody()) {
            assertionStatus = this.scanBody(message, string);
        }
        try {
            if (assertionStatus != AssertionStatus.NONE) {
                this.logAndAuditAttackRejected();
            }
        }
        catch (IOException iOException) {
            throw ServerInjectionThreatProtectionAssertion.b(iOException);
        }
        return assertionStatus;
    }

    protected AssertionStatus scanHttpRequestUrlPath(HttpServletRequestKnob httpServletRequestKnob) throws IOException {
        this.logAndAuditScanningUrlPath();
        StringBuilder stringBuilder = new StringBuilder();
        String string = httpServletRequestKnob.getRequestUri();
        String string2 = URLDecoder.decode(string, g[0]);
        String string3 = this.scan(string2, stringBuilder);
        try {
            if (string3 != null) {
                this.logAndAuditAttackDetectedInUrlPath(stringBuilder, string, string3);
                return this.getBadMessageStatus();
            }
        }
        catch (IOException iOException) {
            throw ServerInjectionThreatProtectionAssertion.b(iOException);
        }
        return AssertionStatus.NONE;
    }

    protected AssertionStatus scanHttpRequestUrlQueryString(HttpServletRequestKnob httpServletRequestKnob) {
        Map map;
        this.logAndAuditScanningUrlQueryString();
        StringBuilder stringBuilder = new StringBuilder();
        try {
            map = httpServletRequestKnob.getRawQueryParameterMap();
        }
        catch (IllegalArgumentException illegalArgumentException) {
            this.logAndAuditCannotParse(g[1], illegalArgumentException.getMessage());
            return this.getBadMessageStatus();
        }
        for (Map.Entry entry : map.entrySet()) {
            String string = (String)entry.getKey();
            for (String string2 : (String[])entry.getValue()) {
                String string3 = this.scan(string2, stringBuilder);
                try {
                    if (string3 == null) continue;
                    this.logAndAuditAttackDetectedInQueryParameter(stringBuilder, string, string3);
                    return this.getBadMessageStatus();
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw ServerInjectionThreatProtectionAssertion.b(illegalArgumentException);
                }
            }
        }
        return AssertionStatus.NONE;
    }

    protected abstract AssertionStatus scanBody(Message var1, String var2) throws IOException;

    protected abstract String scan(String var1, StringBuilder var2);

    protected abstract void logAndAuditRequestAlreadyRouted();

    protected abstract void logAndAuditResponseNotRouted();

    protected abstract void logAndAuditMessageNotHttp();

    protected abstract void logAndAuditScanningUrlPath();

    protected abstract void logAndAuditAttackDetectedInUrlPath(StringBuilder var1, String var2, String var3);

    protected abstract void logAndAuditScanningUrlQueryString();

    protected abstract void logAndAuditAttackDetectedInQueryParameter(StringBuilder var1, String var2, String var3);

    protected abstract void logAndAuditAttackRejected();

    protected abstract void logAndAuditCannotParse(String var1, String var2);

    private static Exception b(Exception exception) {
        return exception;
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[2];
        int n2 = 0;
        String string = "L\u0010\n<}\u000bK!=d \u0019\r9\u0011\u001e]";
        int n3 = "L\u0010\n<}\u000bK!=d \u0019\r9\u0011\u001e]".length();
        int n4 = 5;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = ServerInjectionThreatProtectionAssertion.c(40, ServerInjectionThreatProtectionAssertion.g(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                g = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static char[] g(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x51);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String c(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 49;
                    case 1 -> 108;
                    case 2 -> 100;
                    case 3 -> 57;
                    case 4 -> 109;
                    case 5 -> 66;
                    default -> 81;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

