Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(net): send a User-Agent from pinnedFetch (#332)

Egor Vasilyevfa5a519ef6ed
diff --git a/packages/net/src/__tests__/pinned-fetch.test.ts b/packages/net/src/__tests__/pinned-fetch.test.ts
index 835befa1..b92b241f 100644
--- a/packages/net/src/__tests__/pinned-fetch.test.ts
+++ b/packages/net/src/__tests__/pinned-fetch.test.ts
@@ -1,4 +1,5 @@
import { describe, it, expect, afterEach } from 'bun:test';
+import { createServer, type IncomingHttpHeaders } from 'node:http';
import { assertPublicHttpUrl, pinnedFetch, UrlNotAllowedError } from '../index';
// The guard is strict under NODE_ENV=test, so these exercise the production rules.
@@ -76,3 +77,42 @@ describe('SSRF_ALLOWED_HOSTS', () => {
);
});
});
+
+// node sends no User-Agent of its own, unlike the fetch this replaced, and GitHub
+// answers 403 to a request without one. The requests go to a loopback server, which
+// the guard admits only outside production and test, so NODE_ENV is relaxed around
+// them and restored afterwards.
+describe('pinnedFetch User-Agent', () => {
+ const saved = process.env.NODE_ENV;
+ afterEach(() => {
+ process.env.NODE_ENV = saved;
+ });
+
+ async function headersSeenBy(init?: { headers: Record<string, string> }) {
+ let seen: IncomingHttpHeaders = {};
+ const server = createServer((req, res) => {
+ seen = req.headers;
+ res.end('ok');
+ });
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
+ const address = server.address();
+ const port = typeof address === 'object' && address ? address.port : 0;
+ process.env.NODE_ENV = 'development';
+ try {
+ await pinnedFetch(`http://127.0.0.1:${port}/`, init);
+ } finally {
+ process.env.NODE_ENV = saved;
+ server.close();
+ }
+ return seen;
+ }
+
+ it('sends a default User-Agent when the caller set none', async () => {
+ expect((await headersSeenBy())['user-agent']).toBe('itsaplan/1');
+ });
+
+ it("keeps the caller's own User-Agent", async () => {
+ const headers = await headersSeenBy({ headers: { 'User-Agent': 'itsaplan-webhooks/1' } });
+ expect(headers['user-agent']).toBe('itsaplan-webhooks/1');
+ });
+});
diff --git a/packages/net/src/index.ts b/packages/net/src/index.ts
index 90a7177d..216d1ef0 100644
--- a/packages/net/src/index.ts
+++ b/packages/net/src/index.ts
@@ -132,6 +132,11 @@ export async function assertPublicHttpUrl(raw: string): Promise<URL> {
return (await vet(raw)).url;
}
+// node's http client sends no User-Agent of its own, and GitHub answers 403 with an
+// HTML body to a request that carries none. The global fetch this replaced always sent
+// one, so callers never had to.
+const DEFAULT_USER_AGENT = 'itsaplan/1';
+
export interface PinnedRequestInit {
method?: string;
headers?: Record<string, string> | Headers;
@@ -149,7 +154,12 @@ export async function pinnedFetch(raw: string, init: PinnedRequestInit = {}): Pr
const { url, pin } = await vet(raw);
const send = url.protocol === 'https:' ? httpsRequest : httpRequest;
const headers =
- init.headers instanceof Headers ? Object.fromEntries(init.headers) : (init.headers ?? {});
+ init.headers instanceof Headers
+ ? Object.fromEntries(init.headers)
+ : { ...(init.headers ?? {}) };
+ if (!Object.keys(headers).some((name) => name.toLowerCase() === 'user-agent')) {
+ headers['user-agent'] = DEFAULT_USER_AGENT;
+ }
return new Promise<Response>((resolve, reject) => {
const req = send(