Skip to content

Commit 162cbae

Browse files
juliusmarmingecodex
andcommitted
fix(acp): sanitize generator diagnostics
Co-authored-by: codex <[email protected]>
1 parent fcda98a commit 162cbae

4 files changed

Lines changed: 64 additions & 21 deletions

File tree

‎packages/effect-acp/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"generate": "node scripts/generate.ts"
3939
},
4040
"dependencies": {
41+
"@t3tools/shared": "workspace:*",
4142
"effect": "catalog:"
4243
},
4344
"devDependencies": {

‎packages/effect-acp/scripts/generate.test.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ function processHandle(exitCode: number) {
3434
}
3535

3636
describe("ACP schema generator errors", () => {
37-
it.effect("retains the URL, output path, and HTTP cause when a download fails", () =>
37+
it.effect("retains safe URL diagnostics, output path, and HTTP cause when a download fails", () =>
3838
Effect.gen(function* () {
3939
const fs = yield* FileSystem.FileSystem;
4040
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "acp-generator-test-" });
41-
const url = "https://example.test/schema.json";
41+
const url =
42+
"https://generator-user:[email protected]/private/schema.json?token=generator-secret#fragment";
4243
const outputPath = `${directory}/schema.json`;
4344
const error = yield* Generator.downloadFile(url, outputPath).pipe(
4445
Effect.provideService(
@@ -49,11 +50,20 @@ describe("ACP schema generator errors", () => {
4950
);
5051

5152
assert(isDownloadError(error));
52-
expect(error.url).toBe(url);
53+
expect(error).toMatchObject({
54+
urlInputLength: url.length,
55+
urlProtocol: "https:",
56+
urlHostname: "example.test",
57+
});
58+
expect(error).not.toHaveProperty("url");
5359
expect(error.outputPath).toBe(outputPath);
5460
expect(error.stage).toBe("request");
5561
expect(error.cause).toBeDefined();
5662
expect(error.message).toContain(outputPath);
63+
const { cause: _, ...directDiagnostics } = error;
64+
expect(JSON.stringify(directDiagnostics)).not.toMatch(
65+
/generator-user|generator-password|private|token|generator-secret|fragment/,
66+
);
5767
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
5868
);
5969

@@ -71,7 +81,12 @@ describe("ACP schema generator errors", () => {
7181
);
7282

7383
assert(isDownloadFileError(error));
74-
expect(error.url).toBe(url);
84+
expect(error).toMatchObject({
85+
urlInputLength: url.length,
86+
urlProtocol: "https:",
87+
urlHostname: "example.test",
88+
});
89+
expect(error).not.toHaveProperty("url");
7590
expect(error.outputPath).toBe(outputPath);
7691
expect(error.stage).toBe("write-file");
7792
expect(error.cause).toBeDefined();
@@ -116,12 +131,13 @@ describe("ACP schema generator errors", () => {
116131

117132
assert(isFormatExitError(error));
118133
expect(error.command).toBe("bun");
119-
expect(error.args).toEqual(["oxfmt", generatedDir]);
134+
expect(error.argumentCount).toBe(2);
135+
expect(error).not.toHaveProperty("args");
120136
expect(error.generatedDir).toBe(generatedDir);
121137
expect(error.exitCode).toBe(23);
122138
expect(error.message).toContain("23");
123139
expect(spawned?.command).toBe("bun");
124-
expect(spawned?.args).toEqual(error.args);
140+
expect(spawned?.args).toEqual(["oxfmt", generatedDir]);
125141
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
126142
});
127143
});

‎packages/effect-acp/scripts/generate.ts‎

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
44
import * as NodeServices from "@effect/platform-node/NodeServices";
55
import { make as makeJsonSchemaGenerator } from "@effect/openapi-generator/JsonSchemaGenerator";
6+
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
67
import * as Effect from "effect/Effect";
78
import * as FileSystem from "effect/FileSystem";
89
import * as Layer from "effect/Layer";
@@ -23,31 +24,47 @@ interface GeneratedPaths {
2324
readonly metaOutputPath: string;
2425
}
2526

27+
const urlDiagnosticsSchema = {
28+
urlInputLength: Schema.Number,
29+
urlProtocol: Schema.optionalKey(Schema.String),
30+
urlHostname: Schema.optionalKey(Schema.String),
31+
};
32+
33+
function urlDiagnosticFields(url: string) {
34+
const diagnostics = getUrlDiagnostics(url);
35+
return {
36+
urlInputLength: diagnostics.inputLength,
37+
...(diagnostics.protocol === undefined ? {} : { urlProtocol: diagnostics.protocol }),
38+
...(diagnostics.hostname === undefined ? {} : { urlHostname: diagnostics.hostname }),
39+
};
40+
}
41+
2642
export class AcpGeneratorDownloadError extends Schema.TaggedErrorClass<AcpGeneratorDownloadError>()(
2743
"AcpGeneratorDownloadError",
2844
{
29-
url: Schema.String,
45+
...urlDiagnosticsSchema,
3046
outputPath: Schema.String,
3147
stage: Schema.Literals(["request", "read-response"]),
3248
cause: Schema.Defect(),
3349
},
3450
) {
3551
override get message(): string {
36-
return `Failed to download ${this.url} to ${this.outputPath} during ${this.stage}.`;
52+
const source = this.urlHostname === undefined ? "the configured source" : this.urlHostname;
53+
return `Failed to download the ACP generator input from ${source} to ${this.outputPath} during ${this.stage}.`;
3754
}
3855
}
3956

4057
export class AcpGeneratorDownloadFileError extends Schema.TaggedErrorClass<AcpGeneratorDownloadFileError>()(
4158
"AcpGeneratorDownloadFileError",
4259
{
43-
url: Schema.String,
60+
...urlDiagnosticsSchema,
4461
outputPath: Schema.String,
4562
stage: Schema.Literals(["create-directory", "write-file"]),
4663
cause: Schema.Defect(),
4764
},
4865
) {
4966
override get message(): string {
50-
return `Failed to store the download from ${this.url} at ${this.outputPath} during ${this.stage}.`;
67+
return `Failed to store the ACP generator download at ${this.outputPath} during ${this.stage}.`;
5168
}
5269
}
5370

@@ -69,27 +86,27 @@ export class AcpGeneratorFormatProcessError extends Schema.TaggedErrorClass<AcpG
6986
{
7087
stage: Schema.Literals(["spawn", "wait-for-exit"]),
7188
command: Schema.String,
72-
args: Schema.Array(Schema.String),
89+
argumentCount: Schema.Number,
7390
generatedDir: Schema.String,
7491
cause: Schema.Defect(),
7592
},
7693
) {
7794
override get message(): string {
78-
return `ACP generator formatting command ${JSON.stringify([this.command, ...this.args])} failed during ${this.stage} for ${this.generatedDir}.`;
95+
return `ACP generator formatting command ${this.command} failed during ${this.stage} for ${this.generatedDir}.`;
7996
}
8097
}
8198

8299
export class AcpGeneratorFormatExitError extends Schema.TaggedErrorClass<AcpGeneratorFormatExitError>()(
83100
"AcpGeneratorFormatExitError",
84101
{
85102
command: Schema.String,
86-
args: Schema.Array(Schema.String),
103+
argumentCount: Schema.Number,
87104
generatedDir: Schema.String,
88105
exitCode: Schema.Number,
89106
},
90107
) {
91108
override get message(): string {
92-
return `ACP generator formatting command ${JSON.stringify([this.command, ...this.args])} exited with code ${this.exitCode} for ${this.generatedDir}.`;
109+
return `ACP generator formatting command ${this.command} exited with code ${this.exitCode} for ${this.generatedDir}.`;
93110
}
94111
}
95112

@@ -157,7 +174,7 @@ export const downloadFile = Effect.fn("downloadFile")(function* (url: string, ou
157174
Effect.mapError(
158175
(cause) =>
159176
new AcpGeneratorDownloadFileError({
160-
url,
177+
...urlDiagnosticFields(url),
161178
outputPath,
162179
stage: "create-directory",
163180
cause,
@@ -168,14 +185,20 @@ export const downloadFile = Effect.fn("downloadFile")(function* (url: string, ou
168185
const response = yield* HttpClient.get(url).pipe(
169186
Effect.flatMap(HttpClientResponse.filterStatusOk),
170187
Effect.mapError(
171-
(cause) => new AcpGeneratorDownloadError({ url, outputPath, stage: "request", cause }),
188+
(cause) =>
189+
new AcpGeneratorDownloadError({
190+
...urlDiagnosticFields(url),
191+
outputPath,
192+
stage: "request",
193+
cause,
194+
}),
172195
),
173196
);
174197
const text = yield* response.text.pipe(
175198
Effect.mapError(
176199
(cause) =>
177200
new AcpGeneratorDownloadError({
178-
url,
201+
...urlDiagnosticFields(url),
179202
outputPath,
180203
stage: "read-response",
181204
cause,
@@ -187,7 +210,7 @@ export const downloadFile = Effect.fn("downloadFile")(function* (url: string, ou
187210
Effect.mapError(
188211
(cause) =>
189212
new AcpGeneratorDownloadFileError({
190-
url,
213+
...urlDiagnosticFields(url),
191214
outputPath,
192215
stage: "write-file",
193216
cause,
@@ -250,7 +273,7 @@ export const formatGeneratedFiles = Effect.fn("formatGeneratedFiles")(function*
250273
new AcpGeneratorFormatProcessError({
251274
stage: "spawn",
252275
command,
253-
args,
276+
argumentCount: args.length,
254277
generatedDir,
255278
cause,
256279
}),
@@ -262,7 +285,7 @@ export const formatGeneratedFiles = Effect.fn("formatGeneratedFiles")(function*
262285
new AcpGeneratorFormatProcessError({
263286
stage: "wait-for-exit",
264287
command,
265-
args,
288+
argumentCount: args.length,
266289
generatedDir,
267290
cause,
268291
}),
@@ -272,7 +295,7 @@ export const formatGeneratedFiles = Effect.fn("formatGeneratedFiles")(function*
272295
if (exitCode !== 0) {
273296
return yield* new AcpGeneratorFormatExitError({
274297
command,
275-
args,
298+
argumentCount: args.length,
276299
generatedDir,
277300
exitCode,
278301
});

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)