Skip to content

Security Vulnerability Slack Notification #32

Security Vulnerability Slack Notification

Security Vulnerability Slack Notification #32

name: Security Vulnerability Slack Notification
on:
schedule:
- cron: '0 * * * *' # Runs every hour
workflow_dispatch:
jobs:
check-alerts:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Check for Recent Alerts
env:
# Use a PAT instead of the default token
GH_TOKEN: ${{ secrets.DEPENDABOT_PAT }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
echo "--- TEST MODE ACTIVE ---"
echo "Fetching ALL alerts (Open, Fixed, Dismissed) to verify Slack connection..."
# 1. Fetch alerts via API
# We use '?state=all' to find old/closed alerts
# We use 'per_page=1' because we only need one example to test
RESPONSE=$(curl -s -H "Authorization: token $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${{ github.repository }}/dependabot/alerts?q=is%3Aclosed")
# 2. Check for Authentication Errors
if echo "$RESPONSE" | grep -q "Bad credentials"; then
echo "::error::Authentication Failed! Please check your DEPENDABOT_PAT secret."
exit 1
fi
# 3. Parse the result
# We just grab the first alert found. No time filter. No severity filter.
ALERTS=$(echo "$RESPONSE" | jq '.')
# Check if the list is empty (Repo has NEVER had an alert)
LENGTH=$(echo "$ALERTS" | jq 'length')
if [ "$LENGTH" -eq 0 ]; then
echo "::warning:: No alerts found (Open or Closed). This repo has clean history!"
exit 0
fi
echo "Found historical alert data. Sending Slack notification..."
# 4. Extract details from the first alert found
PACKAGE=$(echo "$ALERTS" | jq -r '.[0] | .dependency.package.name')
SEVERITY=$(echo "$ALERTS" | jq -r '.[0] | .security_advisory.severity')
URL=$(echo "$ALERTS" | jq -r '.[0] | .html_url')
STATE=$(echo "$ALERTS" | jq -r '.[0] | .state')
# 5. Send Slack Notification
# Build message text using jq to safely handle user input
MESSAGE_TEXT=$(jq -n \
--arg repo "$REPO_NAME" \
--arg title "$ISSUE_TITLE" \
--arg user "$ISSUE_USER" \
--arg url "$ISSUE_URL" \
'*📢 New Dependabot Alert \($repo) 📢*\n\n*Issue Title:* \($title)\n*Opened By:* \($user)\n\n*View Issue:* \($url)')
# Build Slack payload
SLACK_PAYLOAD=$(jq -n \
--arg text "$MESSAGE_TEXT" \
'{
"channel": "#docs-devdocs-notifications",
"username": "Security Vulnerability Slack Notification",
"icon_emoji": ":rotating_light:",
"text": $text
}')
# Send to Slack
curl -X POST \
-H 'Content-type: application/json' \
--data "$SLACK_PAYLOAD" \
"$SLACK_WEBHOOK"