【问题标题】:Denial of service:regular expression : fortify pointed out a issue拒绝服务:正则表达式:fortify 指出了一个问题
【发布时间】:2020-08-19 20:01:24
【问题描述】:

您好,我收到拒绝服务:以下行的常规表达式警告

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

【问题讨论】:

  • "获取不是a-zA-Z0-9 的任何内容并将其删除 - 以实际 ID 中的 '\' 为前缀"?这甚至正确吗? - 如果是这样,只需通过字符串缓冲区构建billingApplicationAcctId 并逐个复制位于此范围之外的字符来从a-zA-Z0-9 中删除任何字符。用一个简单的循环来做,逐个字符地检查字符值是否在排除范围内......
  • @TreffnonX 我不清楚你能不能举个例子来详细说明一下

标签: java security fortify


【解决方案1】:

如果您想摆脱正则表达式代码,可以按字符比较输入。直接替换

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 2022-01-12
    • 2022-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多