Skip to content

Commit cc48582

Browse files
committed
fix none value
1 parent bca05a6 commit cc48582

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/scrapy_settings_log/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ def spider_closed(self, spider):
5353
settings = {k: v for k, v in settings.items() if re.search(regex, k)}
5454
if settings.get("MASKED_SENSITIVE_SETTINGS_ENABLED", True):
5555
default_regexes = [
56-
"(?i)(api[\W_]*key)", # apikey and possible variations
57-
"(?i)(AWS[\W_]*SECRET[\W_]*ACCESS[\W_]*KEY)" # AWS_SECRET_ACCESS_KEY and possible variations
56+
".*(?i)(api[\W_]*key).*", # apikey and possible variations e.g: shub_apikey or SC_APIKEY
57+
".*(?i)(AWS[\W_]*SECRET[\W_]*ACCESS[\W_]*KEY).*", # AWS_SECRET_ACCESS_KEY and possible variations
58+
".*(?i)([\W_]*password[\W_]*).*" # password word
5859
]
5960
regex_list = settings.get("MASKED_SENSITIVE_SETTINGS_REGEX_LIST", default_regexes)
6061
for reg in regex_list:
61-
updated_settings = {k: '*'*len(v) for k, v in settings.items() if re.search(reg, k)}
62+
updated_settings = {k: '*'*len(v) if v else v for k, v in settings.items() if re.match(reg, k)}
6263
settings = {**settings, **updated_settings}
6364

6465
self.output_settings(settings, spider)

tests/test_code.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,23 @@ def test_log_all_should_not_return_aws_secret_key_value_by_default(caplog):
156156
assert 'secret_value' not in caplog.text
157157

158158

159+
def test_log_all_should_not_return_password_value_by_default(caplog):
160+
settings = {
161+
"SETTINGS_LOGGING_ENABLED": True,
162+
"test_password": 'secret_value1',
163+
"PASSWORD_TEST": 'secret_value2',
164+
}
165+
166+
spider = MockSpider(settings)
167+
logger = SpiderSettingsLogging()
168+
with caplog.at_level(logging.INFO):
169+
logger.spider_closed(spider)
170+
171+
assert '"test_password": "*************"' in caplog.text
172+
assert '"PASSWORD_TEST": "*************"' in caplog.text
173+
assert 'secret_value' not in caplog.text
174+
175+
159176
def test_log_all_should_return_only_the_custom_regex_data_masked_if_MASKED_SENSITIVE_SETTINGS_REGEX_LIST_configured(caplog):
160177
settings = {
161178
"SETTINGS_LOGGING_ENABLED": True,

0 commit comments

Comments
 (0)