Skip to content

Commit

Permalink
feat: Validate Slack Webhook URL (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunal Nagar authored Aug 14, 2021
1 parent e81c996 commit 71c0afe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
20 changes: 15 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/destinations/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ const createAlertBlock = (alert: Alert): KnownBlock => {
}
}

export const validateSlackWebhookUrl = (url: string): boolean => {
const regexPattern = new RegExp(
/^https:\/\/hooks\.slack\.com\/services\/T[a-zA-Z0-9_]{10}\/B[a-zA-Z0-9_]{10}\/[a-zA-Z0-9_]{24}/,
)
return regexPattern.test(url)
}

export const sendAlertsToSlack = async (
webhookUrl: string,
alerts: Alert[],
Expand Down
8 changes: 6 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getInput, setFailed } from '@actions/core'
import { sendAlertsToSlack, validateSlackWebhookUrl } from './destinations'
import { context } from '@actions/github'
import { fetchAlerts } from './fetch-alerts'
import { sendAlertsToSlack } from './destinations'

async function run(): Promise<void> {
try {
Expand All @@ -13,7 +13,11 @@ async function run(): Promise<void> {
const alerts = await fetchAlerts(token, repo, owner, count)
if (alerts.length > 0) {
if (slackWebhookUrl) {
await sendAlertsToSlack(slackWebhookUrl, alerts)
if (!validateSlackWebhookUrl(slackWebhookUrl)) {
setFailed(new Error('Invalid Slack Webhook URL'))
} else {
await sendAlertsToSlack(slackWebhookUrl, alerts)
}
}
}
} catch (err) {
Expand Down

0 comments on commit 71c0afe

Please sign in to comment.