Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #45 from Skyscanner/html
Browse files Browse the repository at this point in the history
HTML plugin
  • Loading branch information
adeptex authored Mar 6, 2021
2 parents 3a9e2c8 + b5b6731 commit 031d6be
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 4 deletions.
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
# make freeze
#
astroid==2.5.1 # via whispers (setup.py)
beautifulsoup4==4.9.3 # via whispers (setup.py)
jproperties==2.1.0 # via whispers (setup.py)
lazy-object-proxy==1.5.2 # via astroid
luhn==0.2.0 # via whispers (setup.py)
lxml==4.6.2 # via whispers (setup.py)
python-levenshtein==0.12.2 # via whispers (setup.py)
pyyaml==5.4.1 # via whispers (setup.py)
six==1.15.0 # via astroid, jproperties
wrapt==1.12.1 # via astroid

# The following packages are considered to be unsafe in a requirements file:
# setuptools
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def get_version():
return import_module("whispers.__version__").__version__


install_requires = ["luhn==0.2.0", "lxml==4.6.2", "pyyaml==5.3.1", "astroid==2.4.2", "jproperties==2.1.0", "python-levenshtein==0.12.0"]
install_requires = ["luhn>=0.2.0", "lxml>=4.6.2", "pyyaml>=5.3.1", "astroid>=2.4.2", "jproperties>=2.1.0", "python-levenshtein>=0.12.0", "beautifulsoup4>=4.9.3"]

dev_requires = [
"black>=19.10b0",
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/language.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<title>Whispers HTML fixture</title>
</head>
<body>
<!-- TODO: hardcoded comment 01 -->
<!--
TODO: hardcoded comment 02
-->
<!--
TODO: hardcoded comment 03
has multiple lines
-->
This is not a hardcoded comment
</body>
</html>
1 change: 1 addition & 0 deletions tests/unit/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def test_detection_by_key(src, keys):
("language.java", 3),
("language.go", 9),
("language.php", 4),
("language.html", 3),
("plaintext.txt", 2),
("uri.yml", 2),
("java.properties", 3),
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,24 @@
similar_strings,
simple_string,
strip_string,
truncate_all_space,
)


@pytest.mark.parametrize(
("rawstr", "expected"),
[
("", ""),
("whis\npers", "whis pers"),
("whis\tpers", "whis pers"),
("whis\n\n\n\npers", "whis pers"),
("whis\n pers", "whis pers"),
],
)
def test_truncate_all_space(rawstr, expected):
assert truncate_all_space(rawstr) == expected


@pytest.mark.parametrize(
"rawstr",
[
Expand Down
2 changes: 1 addition & 1 deletion whispers/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (1, 4, 3)
VERSION = (1, 4, 4)

__version__ = ".".join(map(str, VERSION))
3 changes: 3 additions & 0 deletions whispers/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from whispers.plugins.config import Config
from whispers.plugins.dockerfile import Dockerfile
from whispers.plugins.go import Go
from whispers.plugins.html import Html
from whispers.plugins.htpasswd import Htpasswd
from whispers.plugins.java import Java
from whispers.plugins.javascript import Javascript
Expand Down Expand Up @@ -67,6 +68,8 @@ def load_plugin(self) -> Optional[object]:
return Htpasswd()
elif self.filetype == "txt":
return Plaintext()
elif self.filetype.startswith("htm"):
return Html()
elif self.filetype == "py":
return Python()
elif self.filetype == "js":
Expand Down
15 changes: 15 additions & 0 deletions whispers/plugins/html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pathlib import Path

from bs4 import BeautifulSoup, Comment

from whispers.utils import truncate_all_space


class Html:
def pairs(self, filepath: Path):
soup = BeautifulSoup(filepath.read_text(), "lxml")
comments = soup.find_all(text=lambda t: isinstance(t, Comment))
for comment in comments:
comment = truncate_all_space(comment)
if len(comment):
yield "comment", comment
8 changes: 8 additions & 0 deletions whispers/rules/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
comments:
description: Potential information leak through comments
message: Comment
severity: MINOR
key:
regex: "^comment$"
ignorecase: False
minlen: 1
6 changes: 6 additions & 0 deletions whispers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
escaped_chars = str.maketrans({"'": r"\'", '"': r"\""})


def truncate_all_space(value: str) -> str:
if not value:
return ""
return re.sub(r"\s+", " ", value)


def strip_string(value: str) -> str:
"""
Strips leading and trailing quotes and spaces
Expand Down

0 comments on commit 031d6be

Please sign in to comment.