Skip to content

Bug fix/slack msg hash issue #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions hawk_scanner/internals/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,18 +511,24 @@ def SlackNotify(msg, args):
connections = get_connection(args)
if not args.no_write:
db = TinyDB('previous_alerts.json')

if 'notify' in connections:
notify_config = connections['notify']
# Check if suppress_duplicates is set to True
suppress_duplicates = notify_config.get('suppress_duplicates', False)

original_msg = msg
if suppress_duplicates and not args.no_write:
# Calculate the hash of the message
msg_hash = calculate_msg_hash(msg)
## check if "msg" has "Message Link" in any line, then remove that complete line
if "Message Link" in msg:
msg = msg.split("\n")
msg = [line for line in msg if "Message Link" not in line]
msg = "\n".join(msg)

msg_hash = calculate_msg_hash(msg)
# Check if the message hash already exists in the previous alerts database
Alert = Query()
if db.contains(Alert.msg_hash == msg_hash):
alert_query = Query()
if db.search(alert_query['msg_hash'] == msg_hash):
print_info(args, "Duplicate message detected. Skipping webhook trigger.")
return

Expand All @@ -531,11 +537,10 @@ def SlackNotify(msg, args):
if webhook_url and webhook_url.startswith('https://hooks.slack.com/services/'):
try:
payload = {
'text': msg,
'text': original_msg,
}
headers = {'Content-Type': 'application/json'}
requests.post(webhook_url, data=json.dumps(payload), headers=headers)

if suppress_duplicates and not args.no_write:
# Store the message hash in the previous alerts database
db.insert({'msg_hash': msg_hash})
Expand Down Expand Up @@ -655,7 +660,29 @@ def get_jira_accId(args, email):
return None

def create_jira_ticket(args, issue_data, message):
orig_msg = message
config = get_connection(args)
if not args.no_write:
db = TinyDB('previous_alerts.json')
if 'notify' in config:
notify_config = config['notify']
# Check if suppress_duplicates is set to True
suppress_duplicates = notify_config.get('suppress_duplicates', False)
if suppress_duplicates and not args.no_write:
# Calculate the hash of the message
## check if "msg" has "Message Link" in any line, then remove that complete line
if "Message Link" in message:
message = message.split("\n")
message = [line for line in message if "Message Link" not in line]
message = "\n".join(message)

msg_hash = calculate_msg_hash(message)
# Check if the message hash already exists in the previous alerts database
alert_query = Query()
if db.search(alert_query['msg_hash'] == msg_hash):
print_info(args, "Duplicate message detected. Skipping ticket creation")
return

"""Creates a Jira ticket using the provided configuration and issue data."""
jira_config = config.get('notify', {}).get('jira', {})

Expand All @@ -675,7 +702,7 @@ def create_jira_ticket(args, issue_data, message):
summary = "Found " + str(total_matches) + " " + issue_data.get('pattern_name') + " in " + issue_data.get('data_source')
summary = issue_fields.get('summary_prefix', '') + summary
description_template = issue_fields.get('description_template', '')
description = description_template.format(details=message, **issue_data)
description = description_template.format(details=orig_msg, **issue_data)

payload = {
"fields": {
Expand Down
2 changes: 1 addition & 1 deletion hawk_scanner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ def main():
title=f"[bold blue]Total {len(group_data)} findings in {group}[/bold blue]")
table.add_column("Sl. No.")
table.add_column("Vulnerable Profile")
print(group_data)
add_columns_to_table(group, table)

for i, result in enumerate(group_data, 1):
records_mini = ', '.join(result['matches']) if len(result['matches']) < 25 else ', '.join(result['matches'][:25]) + f" + {len(result['matches']) - 25} more"
slack_message = format_slack_message(group, result, records_mini)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "0.3.28"
VERSION = "0.3.29"

from setuptools import setup, find_packages

Expand Down