拒绝服务:正则表达式:要指出一个问题

问题描述 投票:0回答:1

您好,我遭到拒绝服务:下一行定期表达警告

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警告

java security fortify
1个回答
0
投票

如果您不想使用正则表达式代码,则可以按字符对输入进行比较。只需替换

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, "");
}

with:

String rawInput = payment.getBillingApplicationAccntId();
StringBuilder sb = new StringBuilder();
for (char c : rawInput.toCharArray()) {
    if ((c >= 'a' && c <= 'z')
            || (c >= 'A' && c <= 'A')
            || (c >= '0' && c <= '9')) {
        sb.append(c);
    }
}
billingApplicationAcctId = sb.toString();
© www.soinside.com 2019 - 2024. All rights reserved.