/*** @name Replacement of a substring with itself* @description Replacing a substring with itself has no effect and may indicate a mistake.* @kind problem* @problem.severity warning* @security-severity 7.8* @id js/identity-replacement* @precision very-high* @tags correctness* security* external/cwe/cwe-116*/import javascript/*** Holds if `e`, when used as the first argument of `String.prototype.replace`, matches* `s` and nothing else.*/predicate matchesString(Expr e, string s) {exists(RegExpLiteral rl |rl.flow().(DataFlow::SourceNode).flowsToExpr(e) andnot rl.isIgnoreCase() andregExpMatchesString(rl.getRoot(), s))ors = e.getStringValue()}/*** Holds if `t` matches `s` and nothing else.*/language[monotonicAggregates]predicate regExpMatchesString(RegExpTerm t, string s) {t.isPartOfRegExpLiteral() and(// constants match themselvess = t.(RegExpConstant).getValue()or// assertions match the empty string(t instanceof RegExpCaret ort instanceof RegExpDollar ort instanceof RegExpWordBoundary ort instanceof RegExpNonWordBoundary ort instanceof RegExpLookahead ort instanceof RegExpLookbehind) ands = ""or// groups match their contentregExpMatchesString(t.(RegExpGroup).getAChild(), s)or// single-character classes match that characterexists(RegExpCharacterClass recc | recc = t and not recc.isInverted() |recc.getNumChild() = 1 andregExpMatchesString(recc.getChild(0), s))or// sequences match the concatenation of their elementsexists(RegExpSequence seq | seq = t |s =concat(int i, RegExpTerm child |child = seq.getChild(i)|any(string subs | regExpMatchesString(child, subs)) order by i)))}from MethodCallExpr repl, string s, string friendlywhererepl.getMethodName() = ["replace", "replaceAll"] andmatchesString(repl.getArgument(0), s) andrepl.getArgument(1).getStringValue() = s and(if s = "" then friendly = "the empty string" else friendly = "'" + s + "'")select repl.getArgument(0), "This replaces " + friendly + " with itself."
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。