Skip to content

Commit

Permalink
fix: logger with more masking of sensitive data (All-Hands-AI#2470)
Browse files Browse the repository at this point in the history
* fix: more logger sensitive masking

* fix: test_config.py updated for more sensitive patterns

* added one more...
  • Loading branch information
tobitege authored Jun 16, 2024
1 parent 798921c commit d2509a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion opendevin/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,12 @@ def __str__(self):
attr_name = f.name
attr_value = getattr(self, f.name)

if attr_name in ['e2b_api_key', 'github_token']:
if attr_name in [
'e2b_api_key',
'github_token',
'jwt_secret',
'ssh_password',
]:
attr_value = '******' if attr_value else None

attr_str.append(f'{attr_name}={repr(attr_value)}')
Expand Down
4 changes: 4 additions & 0 deletions opendevin/core/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ def filter(self, record):
'aws_secret_access_key',
'e2b_api_key',
'github_token',
'jwt_secret',
'ssh_password',
]

# add env var names
env_vars = [attr.upper() for attr in sensitive_patterns]
sensitive_patterns.extend(env_vars)

# and some special cases
sensitive_patterns.append('JWT_SECRET')
sensitive_patterns.append('LLM_API_KEY')
sensitive_patterns.append('GITHUB_TOKEN')
sensitive_patterns.append('SANDBOX_ENV_GITHUB_TOKEN')

# this also formats the message with % args
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,15 @@ def test_api_keys_repr_str():
llm=llm_config,
agent=agent_config,
e2b_api_key='my_e2b_api_key',
jwt_secret='my_jwt_secret',
ssh_password='my_ssh_password',
)
assert "e2b_api_key='******'" in repr(app_config)
assert "e2b_api_key='******'" in str(app_config)
assert "jwt_secret='******'" in repr(app_config)
assert "jwt_secret='******'" in str(app_config)
assert "ssh_password='******'" in repr(app_config)
assert "ssh_password='******'" in str(app_config)

# Check that no other attrs in AppConfig have 'key' or 'token' in their name
# This will fail when new attrs are added, and attract attention
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def test_sensitive_env_vars_masking(test_handler):
'AWS_SECRET_ACCESS_KEY': 'AWS_SECRET_ACCESS_KEY_VALUE',
'E2B_API_KEY': 'E2B_API_KEY_VALUE',
'GITHUB_TOKEN': 'GITHUB_TOKEN_VALUE',
'JWT_SECRET': 'JWT_SECRET_VALUE',
}

log_message = ' '.join(
Expand Down

0 comments on commit d2509a1

Please sign in to comment.