Skip to content

chore: Sanitize titles for Slack notification #716

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 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 40 additions & 3 deletions .github/workflows/notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,43 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/github-script@v7
id: sanitize-title
with:
script: |
const isPR = !!context.payload.pull_request;
const isIssue = !!context.payload.issue;
const item = isPR ? context.payload.pull_request : isIssue ? context.payload.issue : context.payload.issue_comment.issue;

// Sanitization functions
const sanitizeTitle = (title) => {
return title
// Remove potential markdown formatting
.replace(/[*_~`]/g, '')
// Remove potential HTML tags
.replace(/<[^>]*>/g, '')
// Remove multiple spaces
.replace(/\s+/g, ' ')
// Trim whitespace
.trim()
// Enforce max length of 100
.substring(0, 100);
};

// Escape special characters for Slack
const escapeForSlack = (text) => {
return text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/[@]/g, '\\@')
.replace(/>/g, '&gt;')
.replace(/&amp;lt;/g, '&lt;')
.replace(/&amp;gt;/g, '&gt;');
};

const sanitizedTitle = escapeForSlack(sanitizeTitle(item.title));
console.log('Sanitized Title: ', sanitizedTitle);
core.setOutput('safe-title', sanitizedTitle);
- name: Send notifications on Pull Request
if: ${{ github.event_name == 'pull_request'}}
id: slack_PR
Expand All @@ -23,7 +60,7 @@ jobs:
"Notification Type": "Pull Request",
"Notification URL":"${{ github.event.pull_request.html_url }}",
"GitHub Repo": "${{ github.repository }}",
"Notification Title": "${{ github.event.pull_request.title }}"
"Notification Title": "${{ steps.sanitize-title.outputs.safe-title }}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
Expand All @@ -37,7 +74,7 @@ jobs:
"Notification Type": "Issue",
"Notification URL":"${{ github.event.issue.html_url }}",
"GitHub Repo": "${{ github.repository }}",
"Notification Title": "${{ github.event.issue.title }}"
"Notification Title": "${{ steps.sanitize-title.outputs.safe-title }}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
Expand All @@ -51,7 +88,7 @@ jobs:
"Notification Type": "Issue comment",
"Notification URL":"${{ github.event.comment.html_url }}",
"GitHub Repo": "${{ github.repository }}",
"Notification Title": "${{ github.event.issue_comment.issue.title }}"
"Notification Title": "${{ steps.sanitize-title.outputs.safe-title }}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
18 changes: 17 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78456,6 +78456,14 @@ const { isUint8Array, isArrayBuffer } = __nccwpck_require__(98253)
const { File: UndiciFile } = __nccwpck_require__(63041)
const { parseMIMEType, serializeAMimeType } = __nccwpck_require__(94322)

let random
try {
const crypto = __nccwpck_require__(77598)
random = (max) => crypto.randomInt(0, max)
} catch {
random = (max) => Math.floor(Math.random(max))
}

let ReadableStream = globalThis.ReadableStream

/** @type {globalThis['File']} */
Expand Down Expand Up @@ -78541,7 +78549,7 @@ function extractBody (object, keepalive = false) {
// Set source to a copy of the bytes held by object.
source = new Uint8Array(object.buffer.slice(object.byteOffset, object.byteOffset + object.byteLength))
} else if (util.isFormDataLike(object)) {
const boundary = `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}`
const boundary = `----formdata-undici-0${`${random(1e11)}`.padStart(11, '0')}`
const prefix = `--${boundary}\r\nContent-Disposition: form-data`

/*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
Expand Down Expand Up @@ -93357,6 +93365,14 @@ module.exports = require("node:buffer");

/***/ }),

/***/ 77598:
/***/ ((module) => {

"use strict";
module.exports = require("node:crypto");

/***/ }),

/***/ 78474:
/***/ ((module) => {

Expand Down
Loading