GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(forgejo): report TLS certificate, transport error codes, and time… · pingdotgg/t3code@ba0d3eb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/source-control-forgejo/src/server
    • ForgejoCli.ts
    • ForgejoSourceControlProvider.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/source-control-forgejo/src/server/ForgejoCli.ts‎

Copy file name to clipboardExpand all lines: packages/source-control-forgejo/src/server/ForgejoCli.ts
+40-2Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
import * as Cause from "effect/Cause";
12
import * as Config from "effect/Config";
23
import * as Context from "effect/Context";
34
import * as Effect from "effect/Effect";
45
import * as Layer from "effect/Layer";
56
import * as Option from "effect/Option";
7+
import * as Predicate from "effect/Predicate";
68
import * as Schema from "effect/Schema";
79
import * as Result from "effect/Result";
810
import * as Clock from "effect/Clock";
911
import * as FileSystem from "effect/FileSystem";
1012
import * as Semaphore from "effect/Semaphore";
1113
// @effect-diagnostics-next-line nodeBuiltinImport:off - fj storage paths use explicit Windows and POSIX layouts, independently of this process's platform.
1214
import * as NodePath from "node:path";
13-
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
15+
import { FetchHttpClient, HttpClient, HttpClientError, HttpClientRequest } from "effect/http";
1416
import { ChildProcessSpawner } from "effect/process";
1517
import { VcsProcessExitError } from "@t3tools/contracts";
1618
import { decodeJsonResult } from "@t3tools/shared/schemaJson";
@@ -22,6 +24,42 @@ import type { SourceControlProviderContext } from "@t3tools/source-control-core/
2224
// A Gitea list page can hold 50 pull request bodies; gt's output is otherwise capped at 1 MB.
2325
const API_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
2426

27+
const UNTRUSTED_CERTIFICATE_CODES = new Set([
28+
"DEPTH_ZERO_SELF_SIGNED_CERT",
29+
"SELF_SIGNED_CERT_IN_CHAIN",
30+
"UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
31+
"UNABLE_TO_VERIFY_LEAF_SIGNATURE",
32+
]);
33+
34+
// Node's fetch wraps TLS and socket error codes inside generic TypeErrors.
35+
function nodeErrorCode(cause: unknown): string | undefined {
36+
let current = cause;
37+
for (let depth = 0; depth < 4 && current !== undefined && current !== null; depth += 1) {
38+
if (
39+
Predicate.hasProperty(current, "code") &&
40+
typeof current.code === "string" &&
41+
/^[A-Z0-9_]{1,64}$/.test(current.code)
42+
) {
43+
return current.code;
44+
}
45+
current = Predicate.hasProperty(current, "cause") ? current.cause : undefined;
46+
}
47+
return undefined;
48+
}
49+
50+
// Keep fj transport details bounded to failure kind or Node code so tokens never leak.
51+
function describeFjRequestFailure(error: unknown): string {
52+
if (Cause.isTimeoutError(error)) return "Forgejo API request timed out.";
53+
if (HttpClientError.isHttpClientError(error) && error.reason._tag === "TransportError") {
54+
const code = nodeErrorCode(error.reason.cause);
55+
if (code !== undefined && UNTRUSTED_CERTIFICATE_CODES.has(code)) {
56+
return `Forgejo TLS certificate is not trusted (${code}). If this server uses a private CA, add that CA to NODE_EXTRA_CA_CERTS.`;
57+
}
58+
if (code !== undefined) return `Forgejo API request failed (${code}).`;
59+
}
60+
return "Forgejo API request failed.";
61+
}
62+
2563
const encodeApiBody = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
2664
const isVcsProcessExitError = Schema.is(VcsProcessExitError);
2765

@@ -561,7 +599,7 @@ export const make = Effect.gen(function* () {
561599
: new ForgejoCliError({
562600
command: "fj",
563601
cwd: input.cwd,
564-
detail: "Forgejo API request failed or timed out.",
602+
detail: describeFjRequestFailure(error),
565603
}),
566604
),
567605
),
Collapse file

‎packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts‎

Copy file name to clipboardExpand all lines: packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts
+111Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { SourceControlProviderKind } from "@t3tools/contracts";
22
import { assert, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as Exit from "effect/Exit";
5+
import * as Fiber from "effect/Fiber";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Layer from "effect/Layer";
78
import * as Option from "effect/Option";
@@ -10,6 +11,7 @@ import * as Schema from "effect/Schema";
1011
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1112
import * as TestClock from "effect/testing/TestClock";
1213
import * as HttpClient from "effect/http/HttpClient";
14+
import * as HttpClientError from "effect/http/HttpClientError";
1315
import * as HttpClientRequest from "effect/http/HttpClientRequest";
1416
import * as HttpClientResponse from "effect/http/HttpClientResponse";
1517
import {
@@ -3043,3 +3045,112 @@ it.effect("exposes fixed not-found details only for repository lookup", () =>
30433045
),
30443046
),
30453047
);
3048+
3049+
it.effect(
3050+
"reports TLS certificate, transport error codes, and timeouts for fj API requests",
3051+
() => {
3052+
const token = "fj-secret-token";
3053+
const tlsCause = Object.assign(new Error("unable to verify the first certificate"), {
3054+
code: "UNABLE_TO_VERIFY_LEAF_SIGNATURE",
3055+
});
3056+
const refusedCause = Object.assign(new Error("connect ECONNREFUSED"), {
3057+
code: "ECONNREFUSED",
3058+
});
3059+
const untrustedCodeCause = Object.assign(new Error(`bad ${token}`), {
3060+
code: `leaked ${token}`,
3061+
});
3062+
3063+
const runScenario = (
3064+
handler: (
3065+
request: HttpClientRequest.HttpClientRequest,
3066+
) => Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError>,
3067+
) =>
3068+
Effect.gen(function* () {
3069+
const cli = yield* ForgejoCli.make;
3070+
return yield* Effect.flip(
3071+
cli.api({
3072+
cwd: "/repo",
3073+
repository: "https://forgejo.test/maria/project",
3074+
path: "repos/maria/project/pulls/1",
3075+
}),
3076+
);
3077+
}).pipe(
3078+
Effect.provideService(HttpClient.HttpClient, HttpClient.make(handler)),
3079+
Effect.provideService(
3080+
FileSystem.FileSystem,
3081+
FileSystem.makeNoop({
3082+
exists: () => Effect.succeed(true),
3083+
readFileString: () =>
3084+
Effect.succeed(
3085+
encodeJson({
3086+
hosts: {
3087+
"forgejo.test": { type: "Application", token },
3088+
},
3089+
}),
3090+
),
3091+
}),
3092+
),
3093+
Effect.provide(
3094+
TestSourceControlHost.layer({
3095+
process: {
3096+
run: (input) => {
3097+
assert.strictEqual(input.command, "fj");
3098+
return Effect.succeed(processOutput(""));
3099+
},
3100+
},
3101+
}),
3102+
),
3103+
);
3104+
3105+
return Effect.gen(function* () {
3106+
const untrustedCert = yield* runScenario((request) =>
3107+
Effect.fail(
3108+
new HttpClientError.HttpClientError({
3109+
reason: new HttpClientError.TransportError({
3110+
request,
3111+
cause: new TypeError("fetch failed", { cause: tlsCause }),
3112+
}),
3113+
}),
3114+
),
3115+
);
3116+
assert.strictEqual(
3117+
untrustedCert.detail,
3118+
"Forgejo TLS certificate is not trusted (UNABLE_TO_VERIFY_LEAF_SIGNATURE). If this server uses a private CA, add that CA to NODE_EXTRA_CA_CERTS.",
3119+
);
3120+
assert.isUndefined(untrustedCert.cause);
3121+
3122+
const refused = yield* runScenario((request) =>
3123+
Effect.fail(
3124+
new HttpClientError.HttpClientError({
3125+
reason: new HttpClientError.TransportError({
3126+
request,
3127+
cause: new TypeError("fetch failed", { cause: refusedCause }),
3128+
}),
3129+
}),
3130+
),
3131+
);
3132+
assert.strictEqual(refused.detail, "Forgejo API request failed (ECONNREFUSED).");
3133+
3134+
const invalidCode = yield* runScenario((request) =>
3135+
Effect.fail(
3136+
new HttpClientError.HttpClientError({
3137+
reason: new HttpClientError.TransportError({
3138+
request,
3139+
cause: untrustedCodeCause,
3140+
}),
3141+
}),
3142+
),
3143+
);
3144+
assert.strictEqual(invalidCode.detail, "Forgejo API request failed.");
3145+
assert.notInclude(invalidCode.detail, token);
3146+
3147+
const hanging = yield* Effect.forkChild(
3148+
runScenario(() => Effect.never),
3149+
{ startImmediately: true },
3150+
);
3151+
yield* TestClock.adjust("30 seconds");
3152+
const timedOut = yield* Fiber.join(hanging);
3153+
assert.strictEqual(timedOut.detail, "Forgejo API request timed out.");
3154+
});
3155+
},
3156+
);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.