問題:
私はこの人工的なサンプル関数を持っています:
def test_function(target, words):
pattern = re.compile(r"|".join(words))
return bool(pattern.search(target))
単語のリストを受け取り、リスト内の単語を適切にエスケープせずに正規表現パターンを動的に構築します。
使用例:
text = "hello world!"
print(test_function(text, ["test"])) # prints False
print(test_function(text, ["hello"])) # prints True
print(test_function(text, ["test", "world"])) # prints True
質問:
この関数をテストして、適切な正規表現エスケープまたは入力サニタイズがないことを証明するにはどうすればよいですか?
言い換えれば、words
この機能を「壊す」には、リスト内のどの項目を提供すればよいのでしょうか?
(x+x+)+y
壊滅的なバックトラッキングをシミュレートし、関数をorのように強制的にハングさせるために、いくつかの「邪悪な」正規表現を試しましたが、関数はすぐ(a+)+
に戻り、問題の兆候はありません。False