Skip to content

Commit 6b646be

Browse files
authored
Escape special control characters (#157)
1 parent a090173 commit 6b646be

File tree

6 files changed

+93
-12
lines changed

6 files changed

+93
-12
lines changed

dist/index.js

Lines changed: 44 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
module.exports = {
1+
/** @type {import('jest').Config} */
2+
export default {
23
clearMocks: true,
34
moduleFileExtensions: ['js', 'ts'],
45
testMatch: ['**/*.test.ts'],

src/__tests__/postMessage.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('postMessage', () => {
165165
data: {
166166
commit: {
167167
message:
168-
'COMMIT-MESSAGE\n\nCo-authored-by: Nick <[email protected]>',
168+
'<COMMIT> & MESSAGE\n\nCo-authored-by: Nick <[email protected]>',
169169
url: 'github.com/commit'
170170
}
171171
}
@@ -196,13 +196,13 @@ describe('postMessage', () => {
196196
icon_url: 'slack.com/nick',
197197
username: 'Nick (via GitHub)',
198198
unfurl_links: false,
199-
text: 'Nick is deploying action-testing: COMMIT-MESSAGE',
199+
text: 'Nick is deploying action-testing: <COMMIT> & MESSAGE',
200200
blocks: [
201201
{
202202
type: 'section',
203203
text: {
204204
type: 'mrkdwn',
205-
text: ':black_square_button: <@U123> is deploying *action-testing*: <github.com/commit|COMMIT-MESSAGE>'
205+
text: ':black_square_button: <@U123> is deploying *action-testing*: <github.com/commit|&lt;COMMIT&gt; &amp; MESSAGE>'
206206
}
207207
},
208208
{

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {EnvironmentVariable, getEnv, getRequiredEnv} from './input'
66
import {postMessage} from './postMessage'
77
import {SlackClient} from './slack/SlackClient'
88

9+
run()
10+
911
async function run(): Promise<void> {
1012
try {
1113
const octokit = createOctokitClient()
@@ -43,5 +45,3 @@ function createOctokitClient(): OctokitClient {
4345

4446
return getOctokit(token)
4547
}
46-
47-
run()

src/slack/mrkdwn.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
import {Link} from './types'
2+
import {escapeText} from './utils/escapeText'
23

4+
/**
5+
* Bold and escape the specified `text`.
6+
*
7+
* @see https://api.slack.com/reference/surfaces/formatting#visual-styles
8+
*/
39
export function bold(text: string): string {
4-
return `*${text}*`
10+
return `*${escapeText(text)}*`
511
}
612

13+
/**
14+
* Return an emoji with the specified `name`.
15+
*
16+
* @see https://api.slack.com/reference/surfaces/formatting#emoji
17+
*/
718
export function emoji(name: string): string {
819
return `:${name}:`
920
}
1021

1122
/**
23+
* Return a link with the specified `text` and `url`.
24+
*
25+
* The `text` is escaped.
26+
*
1227
* @see https://api.slack.com/reference/surfaces/formatting#linking-urls
1328
*/
1429
export function link({text, url}: Link): string {
15-
return `<${url}|${text}>`
30+
return `<${url}|${escapeText(text)}>`
1631
}

src/slack/utils/escapeText.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Replace special control characters in the specified `text`.
3+
*
4+
* @see https://docs.slack.dev/messaging/formatting-message-text#escaping
5+
*/
6+
export function escapeText(text: string): string {
7+
return text.replace(
8+
CONTROL_CHARACTER_REGEX,
9+
match =>
10+
CONTROL_CHARACTER_HTML_ENTITY_MAP[
11+
match as keyof typeof CONTROL_CHARACTER_HTML_ENTITY_MAP
12+
]
13+
)
14+
}
15+
16+
const CONTROL_CHARACTER_HTML_ENTITY_MAP = {
17+
'&': '&amp;',
18+
'<': '&lt;',
19+
'>': '&gt;'
20+
} as const satisfies Record<string, string>
21+
22+
const CONTROL_CHARACTER_REGEX = new RegExp(
23+
`[${Object.keys(CONTROL_CHARACTER_HTML_ENTITY_MAP).join('')}]`,
24+
'g'
25+
)

0 commit comments

Comments
 (0)