我有該人工示例功能:
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
或(a+)+
,但功能只是返回False
立即並沒有出現問題的信息。
一個空字符串,總是返回「真」 *(無論換句話說)*。 –