-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
42 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,22 @@ | ||
import re | ||
from typing import Any, List | ||
|
||
from ..core import BaseTextClassifier, Score | ||
|
||
|
||
class MarkdownInjectionClassifier(BaseTextClassifier[bool]): | ||
class MarkdownInjectionClassifier(BaseTextClassifier[List[Any]]): | ||
"""A text classifier to detect Markdown injection in input text.""" | ||
|
||
def score(self, input: str) -> Score[bool]: | ||
def score(self, input: str) -> Score[List[Any]]: | ||
# !\[.*?\]\((.*?)\) - This is for the inline image format in Markdown, which is ![alt_text](url). | ||
# !\[.*?\]\[(.*?)\] - This is for the reference-style image format in Markdown, which is ![alt_text][image_reference]. | ||
pattern = r"!\s*\[.*?\]\((.*?)\)|!\s*\[.*?\]\[(.*?)\]" | ||
|
||
matches = re.findall(pattern, input) | ||
|
||
if matches: | ||
return Score( | ||
flagged=True, | ||
value=True, | ||
description="Markdown Injection detected", | ||
explanation="True when a markdown injection is detected, else False", | ||
) | ||
else: | ||
return Score( | ||
flagged=False, | ||
value=False, | ||
description="Markdown Injection not detected", | ||
explanation="True when a markdown injection is detected, else False", | ||
) | ||
return Score[List[Any]]( | ||
flagged=True if len(matches) > 0 else False, | ||
value=matches, | ||
description="Returns True if a markdown injection is detected, else False.", | ||
explanation="Markdown Injection detected" if matches else "Markdown Injection not detected", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters