diff --git a/.changeset/proxy-fetch-undici-version-mismatch.md b/.changeset/proxy-fetch-undici-version-mismatch.md new file mode 100644 index 00000000..32ac79ce --- /dev/null +++ b/.changeset/proxy-fetch-undici-version-mismatch.md @@ -0,0 +1,5 @@ +--- +"@slack/slack-github-action": patch +--- + +fix: send requests through the configured proxy using the undici version that matches the node runtime diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 6188e456..de55e88d 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -9,6 +9,9 @@ updates: versions: - "25.x" - "26.x" + - dependency-name: "undici" # https://undici.nodejs.org/#long-term-support + versions: + - "8.x" - package-ecosystem: "github-actions" directory: "/" schedule: diff --git a/package-lock.json b/package-lock.json index 9d149829..91b9786d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,7 @@ "flat": "^6.0.1", "js-yaml": "^5.5.0", "markup-js": "^1.5.21", - "undici": "^8.12.4" + "undici": "^7.30.2" }, "devDependencies": { "@biomejs/biome": "^2.6.8", @@ -872,6 +872,12 @@ "undici-types": ">=7.24.0 <7.24.7" } }, + "node_modules/@types/node/node_modules/undici-types": { + "version": "7.30.2", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.30.2.tgz", + "integrity": "sha512-Vx15rZHtDo7X+sZVzcecz08ooRvEa0cjSs8z85v/jRRf81BT1m9eaoMI4gxvaNwgQg2IgkfjJ45dMpKrSl0zEQ==", + "license": "MIT" + }, "node_modules/@types/retry": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz", @@ -1612,20 +1618,14 @@ } }, "node_modules/undici": { - "version": "8.12.4", - "resolved": "https://registry.npmjs.org/undici/-/undici-8.12.4.tgz", - "integrity": "sha512-8Mx+uNKKER2bA4AdtedkqRbwUCIJFr2nJckXRe5yrDqzePqwLP7VNcctN8/XdmDSbF7mS+jJKIszlL+vbam1VA==", + "version": "7.30.2", + "resolved": "https://registry.npmjs.org/undici/-/undici-7.30.2.tgz", + "integrity": "sha512-mLyPQnHwUxaM/rideoK/+KL8DaAJLRl4aBCfmX444aCSLnIaomqPLtNRe2jdCYcsk5wZzXrDIQC8ZeYNRAYyIA==", "license": "MIT", "engines": { - "node": ">=22.19.0" + "node": ">=20.18.1" } }, - "node_modules/undici-types": { - "version": "8.12.4", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.12.4.tgz", - "integrity": "sha512-GNCQH3MbVGB8OAVAoTiLH8pu4wx5Ni3DWKpnBzGj3WDsi8EwmK0aOVBooNEt+g5YL1eHPxcS9XUTouwFU6j1kQ==", - "license": "MIT" - }, "node_modules/universal-user-agent": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-7.0.3.tgz", diff --git a/package.json b/package.json index 183552b6..3bc5690d 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "flat": "^6.0.1", "js-yaml": "^5.5.0", "markup-js": "^1.5.21", - "undici": "^8.12.4" + "undici": "^7.30.2" }, "overrides": { "undici-types": "$undici" diff --git a/test/proxies.spec.js b/test/proxies.spec.js index e2b92812..6fa5e946 100644 --- a/test/proxies.spec.js +++ b/test/proxies.spec.js @@ -1,5 +1,7 @@ import assert from "node:assert"; +import http from "node:http"; import { beforeEach, describe, it } from "node:test"; +import { errors as undiciErrors } from "undici"; import Config from "../src/config.js"; import SlackError from "../src/errors.js"; import { fetch, proxies } from "../src/proxies.js"; @@ -20,6 +22,70 @@ describe("proxies", () => { assert.strictEqual(typeof fetchFn, "function"); }); + it("dispatches requests through the proxy with correct request handler", async () => { + mocks.inputs.set("method", "chat.postMessage"); + mocks.inputs.set("proxy", "http://127.0.0.1:1"); + mocks.inputs.set("token", "xoxb-example"); + mocks.fetch.mock.restore(); + const config = new Config(mocks.core); + const fetchFn = fetch(config); + await assert.rejects( + // ".invalid" is reserved by RFC 2606 to never resolve. + fetchFn("https://slack-github-action.invalid/api/chat.postMessage", { + method: "POST", + }), + (err) => { + assert.ok(!(err.cause instanceof undiciErrors.InvalidArgumentError)); + return true; + }, + ); + }); + + /** + * @see {@link https://github.com/slackapi/slack-github-action/pull/675} + */ + it("sends form data through the proxy as a multipart body", async () => { + const received = {}; + const server = http.createServer((req, res) => { + const chunks = []; + req.on("data", (chunk) => chunks.push(chunk)); + req.on("end", () => { + received.host = req.headers.host; + received.type = req.headers["content-type"]; + received.body = Buffer.concat(chunks).toString(); + res.end("{}"); + }); + }); + server.on("connect", (req, socket, head) => { + received.tunnel = req.url; + socket.write("HTTP/1.1 200 Connection Established\r\n\r\n"); + socket.unshift(head); + server.emit("connection", socket); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + mocks.inputs.set("method", "files.uploadV2"); + mocks.inputs.set("proxy", `http://127.0.0.1:${server.address().port}`); + mocks.inputs.set("token", "xoxb-example"); + mocks.fetch.mock.restore(); + mocks.fs.readFileSync.mock.restore(); + const config = new Config(mocks.core); + const fetchFn = fetch(config); + const body = new FormData(); + body.append("file", new Blob(["example"]), "example.txt"); + try { + await fetchFn("http://slack-github-action.invalid/upload", { + method: "POST", + body, + }); + } finally { + server.closeAllConnections(); + server.close(); + } + assert.strictEqual(received.host, "slack-github-action.invalid"); + assert.match(received.type, /^multipart\/form-data; boundary=/); + assert.match(received.body, /filename="example.txt"[\s\S]*example/); + }); + it("returns undefined when no proxy is configured", async () => { mocks.inputs.set("method", "chat.postMessage"); mocks.inputs.set("token", "xoxb-example");