Skip to content

Commit 5c56ca2

Browse files
committed
Initial updates for secure-actions-webhook action
1 parent ea1321b commit 5c56ca2

File tree

5 files changed

+572
-144
lines changed

5 files changed

+572
-144
lines changed

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
FROM node:12.10.0-alpine
22

3-
LABEL "version"="0.1.0"
4-
LABEL "repository"="https://github.com/Ybrin/secure-actions-webhook"
5-
LABEL "homepage"="https://github.com/Ybrin/secure-actions-webhook"
6-
LABEL "maintainer"="Koray Koska <[email protected]>"
3+
LABEL "version"="0.1.3"
4+
LABEL "repository"="https://github.com/Greenlight-Simulation/secure-actions-webhook"
5+
LABEL "homepage"="https://github.com/Greenlight-Simulation/secure-actions-webhook"
6+
LABEL "maintainer"="Dan Marcucci <[email protected]>"
77
LABEL "com.github.actions.name"="Secure Actions Webhook"
88
LABEL "com.github.actions.description"="Post data and an hmac signature to an endpoint"
99
LABEL "com.github.actions.icon"="message-square"

README.md

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

99
```yaml
1010
- name: Webhook
11-
uses: ybrin/[email protected].2
11+
uses: Greenlight-Simulation/[email protected].3
1212
env:
1313
REQUEST_URI: ${{ secrets.REQUEST_URI }}
1414
REQUEST_DATA: "something_interesting"
@@ -19,13 +19,12 @@ Sending a json string:
1919
2020
```yaml
2121
- name: Webhook
22-
uses: ybrin/[email protected].2
22+
uses: Greenlight-Simulation/[email protected].3
2323
env:
2424
REQUEST_URI: ${{ secrets.REQUEST_URI }}
2525
REQUEST_DATA: '{ "something": "interesting" }'
2626
HMAC_SECRET: "secret_used_to_generate_signature"
2727
```
2828
29-
The request will include the header `X-Hub-Signature`, which is the hmac signature of the raw body just like in Github webhooks
30-
(sha1=<hmac_signature>).
29+
The request will include the header `X-Request-Signature`, which is the HMAC signature of the raw body (SHA256, Base64).
3130
Verify it on your endpoint for integrity.

main.js

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,52 @@
11
const request = require("request");
2-
const crypto = require("crypto");
2+
const CryptoJS = require("crypto-js");
33

44
const hmacSecret = process.env.HMAC_SECRET;
55
if (!hmacSecret || hmacSecret === "" || hmacSecret.trim() === "") {
6-
console.warn(
7-
"The hmac secret seems empty. This doesn't seem like what you want."
8-
);
6+
console.warn("The hmac secret seems empty. This doesn't seem like what you want.");
97
}
108
if (hmacSecret.length < 32) {
11-
console.warn(
12-
"The hmac secret seems week. You should use at least 32 secure random hex chars."
13-
);
9+
console.warn("The hmac secret seems week. You should use at least 32 secure random hex chars.");
1410
}
1511

1612
const createHmacSignature = body => {
17-
const hmac = crypto.createHmac("sha1", hmacSecret);
18-
const bodySignature = hmac.update(JSON.stringify(body)).digest("hex");
19-
20-
return `sha1=${bodySignature}`;
13+
return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(JSON.stringify(body), hmacSecret));
2114
};
2215

2316
function isJsonString(str) {
24-
try {
25-
const json = JSON.parse(str);
26-
return typeof json === "object";
27-
} catch (e) {
28-
return false;
29-
}
17+
try {
18+
const json = JSON.parse(str);
19+
return typeof json === "object";
20+
} catch (e) {
21+
return false;
22+
}
3023
}
3124

3225
const uri = process.env.REQUEST_URI;
3326
const data = {
34-
data: isJsonString(process.env.REQUEST_DATA)
35-
? JSON.parse(process.env.REQUEST_DATA)
36-
: process.env.REQUEST_DATA
27+
data: isJsonString(process.env.REQUEST_DATA) ? JSON.parse(process.env.REQUEST_DATA) : process.env.REQUEST_DATA
3728
};
3829

3930
const signature = createHmacSignature(data);
4031

4132
request(
42-
{
43-
method: "POST",
44-
uri: uri,
45-
46-
json: true,
47-
body: data,
48-
headers: {
49-
"X-Hub-Signature": signature
50-
}
51-
},
52-
(error, response, body) => {
53-
if (error || response.statusCode < 200 || response.statusCode > 299) {
54-
// Something went wrong
55-
console.error(`Request failed with status code ${response.statusCode}!`);
56-
console.error(response.body);
57-
58-
process.exit(1);
59-
} else {
60-
// Success
61-
process.exit();
33+
{
34+
method: `${process.env.METHOD ? process.env.METHOD : 'POST'}`,
35+
uri: uri,
36+
json: true,
37+
body: data,
38+
headers: { "X-Request-Signature": signature }
39+
},
40+
(error, response, body) => {
41+
if (error || response.statusCode < 200 || response.statusCode > 299) {
42+
// Something went wrong
43+
console.error(`Request failed with status code ${response.statusCode}!`);
44+
console.error(response.body);
45+
46+
process.exit(1);
47+
} else {
48+
// Success
49+
process.exit();
50+
}
6251
}
63-
}
6452
);

0 commit comments

Comments
 (0)