Skip to content

Commit a3a2f06

Browse files
authored
Merge pull request #1 from dmarcucci/main
Update logic to use native node features (e.g., crypto and fetch)
2 parents 1a45fcf + 182d7b0 commit a3a2f06

File tree

8 files changed

+54
-850
lines changed

8 files changed

+54
-850
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
.DS_Store
2-
node_modules/
1+
.DS_Store

Dockerfile

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
FROM node:12.10.0-alpine
1+
FROM node:22.11.0-bookworm-slim
22

3-
LABEL "version"="0.1.4"
3+
LABEL "version"="1.0.0"
44
LABEL "repository"="https://github.com/Greenlight-Simulation/secure-actions-webhook"
55
LABEL "homepage"="https://github.com/Greenlight-Simulation/secure-actions-webhook"
66
LABEL "maintainer"="Dan Marcucci <[email protected]>"
@@ -9,16 +9,12 @@ LABEL "com.github.actions.description"="Post data and an hmac signature to an en
99
LABEL "com.github.actions.icon"="message-square"
1010
LABEL "com.github.actions.color"="gray-dark"
1111

12-
# Add the entry point
1312
RUN mkdir /app
14-
ADD main.js /app/main.js
15-
ADD package.json /app/package.json
13+
ADD main.mjs /app/main.mjs
1614
ADD entrypoint.sh /app/entrypoint.sh
1715
RUN chmod +x /app/entrypoint.sh
1816

1917
RUN cd /app
2018
WORKDIR /app
21-
RUN npm install
2219

23-
# Load the entry point
2420
ENTRYPOINT ["/app/entrypoint.sh"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Sending a string:
88

99
```yaml
1010
- name: Webhook
11-
uses: Greenlight-Simulation/secure-actions-webhook@0.1.4
11+
uses: Greenlight-Simulation/secure-actions-webhook@1.0.0
1212
env:
1313
REQUEST_URI: ${{ secrets.REQUEST_URI }}
1414
REQUEST_DATA: "something_interesting"
@@ -19,7 +19,7 @@ Sending a json string:
1919
2020
```yaml
2121
- name: Webhook
22-
uses: Greenlight-Simulation/secure-actions-webhook@0.1.4
22+
uses: Greenlight-Simulation/secure-actions-webhook@1.0.0
2323
env:
2424
REQUEST_URI: ${{ secrets.REQUEST_URI }}
2525
REQUEST_DATA: '{ "something": "interesting" }'

entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
set -eu
33

4-
node /app/main.js
4+
node /app/main.mjs

main.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

main.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const hmacSecret = process.env.HMAC_SECRET
2+
3+
if (!hmacSecret || hmacSecret === "" || hmacSecret.trim() === "") {
4+
console.warn("HMAC_SECRET secret seems empty. This doesn't seem like what you want.")
5+
}
6+
7+
if (hmacSecret.length < 32) {
8+
console.warn("HMAC_SECRET seems weak. You should use at least 32 secure random hex chars.")
9+
}
10+
11+
const createHmacSignature = async (jsonBody) => {
12+
const key =
13+
await crypto.subtle.importKey("raw",
14+
new TextEncoder().encode(hmacSecret),
15+
{ name: "HMAC", hash: "SHA-256" },
16+
false,
17+
["sign"])
18+
19+
const signature = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(jsonBody))
20+
21+
return Buffer.from(new Uint8Array(signature)).toString('base64')
22+
}
23+
24+
const jsonBody = process.env.REQUEST_DATA
25+
26+
try {
27+
JSON.parse(jsonBody)
28+
} catch (e) {
29+
console.error("REQUEST_DATA is not a valid JSON string.")
30+
process.exit(1)
31+
}
32+
33+
const signature = await createHmacSignature(jsonBody)
34+
35+
const response =
36+
await fetch(process.env.REQUEST_URI, {
37+
method: `${process.env.METHOD ? process.env.METHOD : 'POST'}`,
38+
body: jsonBody,
39+
headers: { 'X-Request-Signature': signature }
40+
})
41+
42+
if (response.status < 200 || response.status > 299) {
43+
console.error(`Request failed with status code ${response.status}!`)
44+
process.exit(1)
45+
} else process.exit()

0 commit comments

Comments
 (0)