Отказ в обслуживании: регулярное выражение: fortify указал на проблему - PullRequest
0 голосов
/ 05 мая 2020

Привет, я получаю отказ в обслуживании: регулярное экспресс-предупреждение в строке ниже

billingApplicationAcctId = billingApplicationAcctId.replaceAll ("\" + s, "");

вы можете увидеть ниже код для дальнейшего использования

   if (null != formatBillingAcctIdInd && formatBillingAcctIdInd.equals("Y")
                    && billingApplicationCode.equalsIgnoreCase(EPWFReferenceDataConstants.BILLING_APPICATION_ID.KENAN.name())) {
                Pattern pt = Pattern.compile("[^a-zA-Z0-9]");
                Matcher match = pt.matcher(payment.getBillingApplicationAccntId());
                while (match.find()) {
                    String s = match.group();
                    billingApplicationAcctId = billingApplicationAcctId.replaceAll("\\" + s, "");
                }
            }

что мне делать вместо кода выше, чтобы я не получил предупреждение DOS

1 Ответ

0 голосов
/ 05 мая 2020

Если вы хотите go уйти от кода регулярного выражения, вы можете сравнить вводимые символы. Просто замените

Pattern pt = Pattern.compile("[^a-zA-Z0-9]");
Matcher match = pt.matcher(payment.getBillingApplicationAccntId());
while (match.find()) {
    String s = match.group();
    billingApplicationAcctId = billingApplicationAcctId.replaceAll("\\" + s, "");
}

на:

String rawInput = payment.getBillingApplicationAccntId();
StringBuilder sb = new StringBuilder();
for (char c : rawInput.toCharArray()) {
    // any char that is an english letter or 0-9 is included. The rest is thrown away...
    if ((c >= 'a' && c <= 'z')
            || (c >= 'A' && c <= 'Z')
            || (c >= '0' && c <= '9')) {
        sb.append(c);
    }
}
billingApplicationAcctId = sb.toString();
...