Merge pull request #5963 from continuedev/nate/cert-data-url

Data URI for caBundlePath
This commit is contained in:
Tomasz Stefaniak 2025-06-03 10:34:42 -07:00 • committed by GitHub
commit fe7ff77bba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 1336 additions and 3383 deletions

View file

@ -1,20 +0,0 @@
import path from "path";
import { fileURLToPath } from "url";
export default {
transform: {
"\\.[jt]sx?$": ["ts-jest", { useESM: true }],
},
moduleNameMapper: {
"(.+)\\.js": "$1",
},
extensionsToTreatAsEsm: [".ts"],
preset: "ts-jest/presets/default-esm",
testTimeout: 10000,
testEnvironment: "node",
globals: {
__dirname: path.dirname(fileURLToPath(import.meta.url)),
__filename: path.resolve(fileURLToPath(import.meta.url)),
},
};

View file

@ -1,14 +0,0 @@
// This file declares the Jest globals for TypeScript
import "@jest/globals";
declare global {
const describe: typeof import("@jest/globals").describe;
const expect: typeof import("@jest/globals").expect;
const it: typeof import("@jest/globals").it;
const test: typeof import("@jest/globals").test;
const beforeAll: typeof import("@jest/globals").beforeAll;
const afterAll: typeof import("@jest/globals").afterAll;
const beforeEach: typeof import("@jest/globals").beforeEach;
const afterEach: typeof import("@jest/globals").afterEach;
const jest: typeof import("@jest/globals").jest;
}

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"test": "vitest run",
"build": "tsc"
},
"author": "Nate Sesti and Ty Dunn",
@ -20,10 +20,6 @@
},
"devDependencies": {
"@types/follow-redirects": "^1.14.4",
"@types/jest": "^29.5.14",
"cross-env": "^7.0.3",
"jest": "^29.7.0",
"ts-jest": "^29.3.4",
"ts-node": "^10.9.2"
"vitest": "^3.2.0"
}
}

View file

@ -2,6 +2,7 @@ import { globalAgent } from "https";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { afterEach, beforeEach, expect, test } from "vitest";
import { getAgentOptions } from "./getAgentOptions.js";
// Store original env

View file

@ -0,0 +1,122 @@
import * as fs from "node:fs";
import { expect, test, vi } from "vitest";
// Mock fs module
vi.mock("node:fs", () => ({
readFileSync: vi.fn(),
}));
const mockReadFileSync = vi.mocked(fs.readFileSync);
// We need to access the private getCertificateContent function for testing
// Since it's not exported, we'll test it indirectly through scenarios or mock the module
const getCertificateContent = (input: string): string => {
if (input.startsWith("data:")) {
// Parse data URI: data:[<mediatype>][;base64],<data>
const [header, data] = input.split(",");
if (header.includes("base64")) {
return Buffer.from(data, "base64").toString("utf8");
} else {
return decodeURIComponent(data);
}
} else {
// Assume it's a file path
return fs.readFileSync(input, "utf8");
}
};
test("getCertificateContent should decode base64 data URI correctly", () => {
const testCert =
"-----BEGIN CERTIFICATE-----\nMIIC...\n-----END CERTIFICATE-----";
const base64Data = Buffer.from(testCert, "utf8").toString("base64");
const dataUri = `data:application/x-pem-file;base64,${base64Data}`;
const result = getCertificateContent(dataUri);
expect(result).toBe(testCert);
});
test("getCertificateContent should decode URL-encoded data URI correctly", () => {
const testCert =
"-----BEGIN CERTIFICATE-----\nMIIC...\n-----END CERTIFICATE-----";
const encodedData = encodeURIComponent(testCert);
const dataUri = `data:text/plain,${encodedData}`;
const result = getCertificateContent(dataUri);
expect(result).toBe(testCert);
});
test("getCertificateContent should handle plain text data URI correctly", () => {
const testCert = "simple-cert-content";
const dataUri = `data:text/plain,${testCert}`;
const result = getCertificateContent(dataUri);
expect(result).toBe(testCert);
});
test("getCertificateContent should read file when input is a file path", () => {
const filePath = "/path/to/cert.pem";
const expectedContent =
"-----BEGIN CERTIFICATE-----\nfile content\n-----END CERTIFICATE-----";
mockReadFileSync.mockReturnValue(expectedContent);
const result = getCertificateContent(filePath);
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, "utf8");
expect(result).toBe(expectedContent);
});
test("getCertificateContent should handle data URI with different media types", () => {
const testData = "certificate-data";
const base64Data = Buffer.from(testData, "utf8").toString("base64");
const dataUri = `data:application/x-x509-ca-cert;base64,${base64Data}`;
const result = getCertificateContent(dataUri);
expect(result).toBe(testData);
});
test("getCertificateContent should handle data URI without media type", () => {
const testData = "simple-data";
const base64Data = Buffer.from(testData, "utf8").toString("base64");
const dataUri = `data:;base64,${base64Data}`;
const result = getCertificateContent(dataUri);
expect(result).toBe(testData);
});
test("getCertificateContent should handle data URI without media type or encoding", () => {
const testData = "simple-data";
const base64Data = Buffer.from(testData, "utf8").toString("base64");
const dataUri = `data:;base64,${base64Data}`;
const result = getCertificateContent(dataUri);
expect(result).toBe(testData);
});
test("getCertificateContent should handle relative file paths", () => {
const filePath = "./certs/ca.pem";
const expectedContent = "certificate from relative path";
mockReadFileSync.mockReturnValue(expectedContent);
const result = getCertificateContent(filePath);
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, "utf8");
expect(result).toBe(expectedContent);
});
test("getCertificateContent should handle data URI with special characters in URL encoding", () => {
const testCert = "cert with spaces and special chars: !@#$%";
const encodedData = encodeURIComponent(testCert);
const dataUri = `data:text/plain,${encodedData}`;
const result = getCertificateContent(dataUri);
expect(result).toBe(testCert);
});

View file

@ -4,6 +4,24 @@ import tls from "node:tls";
import { RequestOptions } from "@continuedev/config-types";
/**
* Extracts content from either a file path or data URI
*/
function getCertificateContent(input: string): string {
if (input.startsWith("data:")) {
// Parse data URI: data:[<mediatype>][;base64],<data>
const [header, data] = input.split(",");
if (header.includes("base64")) {
return Buffer.from(data, "base64").toString("utf8");
} else {
return decodeURIComponent(data);
}
} else {
// Assume it's a file path
return fs.readFileSync(input, "utf8");
}
}
/**
* Prepares agent options based on request options and certificates
*/
@ -30,7 +48,7 @@ export function getAgentOptions(requestOptions?: RequestOptions): {
: requestOptions?.caBundlePath;
if (customCerts) {
ca.push(
...customCerts.map((customCert) => fs.readFileSync(customCert, "utf8")),
...customCerts.map((customCert) => getCertificateContent(customCert)),
);
}
@ -45,16 +63,13 @@ export function getAgentOptions(requestOptions?: RequestOptions): {
// Handle ClientCertificateOptions
if (requestOptions?.clientCertificate) {
agentOptions.cert = fs.readFileSync(
requestOptions.clientCertificate.cert,
"utf8",
);
agentOptions.key = fs.readFileSync(
requestOptions.clientCertificate.key,
"utf8",
);
const { cert, key, passphrase } = requestOptions.clientCertificate;
agentOptions.cert = getCertificateContent(cert);
agentOptions.key = getCertificateContent(key);
if (requestOptions.clientCertificate.passphrase) {
agentOptions.passphrase = requestOptions.clientCertificate.passphrase;
agentOptions.passphrase = passphrase;
}
}

View file

@ -1,4 +1,5 @@
import { Readable } from "stream";
import { describe, expect, it, test } from "vitest";
import { parseDataLine, streamSse } from "./stream.js";
function createMockResponse(sseLines: string[]): Response {

View file

@ -1,9 +1,9 @@
import { jest } from "@jest/globals";
import { afterEach, expect, test, vi } from "vitest";
import { getProxyFromEnv, shouldBypassProxy } from "./util.js";
// Reset environment variables after each test
afterEach(() => {
jest.resetModules();
vi.resetModules();
process.env = {};
});

View file

@ -14,10 +14,10 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmitOnError": false,
"types": ["node", "jest"],
"types": ["node"],
"outDir": "dist",
"declaration": true
// "sourceMap": true
},
"include": ["src/**/*", "jest.globals.d.ts"]
"include": ["src/**/*"]
}

View file

@ -0,0 +1,17 @@
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "node",
timeout: 10000,
globals: true,
},
resolve: {
alias: {
// Handle .js imports for TypeScript files
"./getAgentOptions.js": "./getAgentOptions.ts",
"./stream.js": "./stream.ts",
"./util.js": "./util.ts",
},
},
});