GeezerVison is showing github.com — fix(forgejo): report TLS certificate, transport error codes, and time… · pingdotgg/t3code@ba0d3eb
File treeExpand file treeCollapse file tree Open diff view settings packages/source-control-forgejo/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
| | 1 | +import * as Cause from "effect/Cause"; | 1 | 2 | import * as Config from "effect/Config"; | 2 | 3 | import * as Context from "effect/Context"; | 3 | 4 | import * as Effect from "effect/Effect"; | 4 | 5 | import * as Layer from "effect/Layer"; | 5 | 6 | import * as Option from "effect/Option"; | | 7 | +import * as Predicate from "effect/Predicate"; | 6 | 8 | import * as Schema from "effect/Schema"; | 7 | 9 | import * as Result from "effect/Result"; | 8 | 10 | import * as Clock from "effect/Clock"; | 9 | 11 | import * as FileSystem from "effect/FileSystem"; | 10 | 12 | import * as Semaphore from "effect/Semaphore"; | 11 | 13 | // @effect-diagnostics-next-line nodeBuiltinImport:off - fj storage paths use explicit Windows and POSIX layouts, independently of this process's platform. | 12 | 14 | import * as NodePath from "node:path"; | 13 | | -import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http"; | | 15 | +import { FetchHttpClient, HttpClient, HttpClientError, HttpClientRequest } from "effect/http"; | 14 | 16 | import { ChildProcessSpawner } from "effect/process"; | 15 | 17 | import { VcsProcessExitError } from "@t3tools/contracts"; | 16 | 18 | import { decodeJsonResult } from "@t3tools/shared/schemaJson"; | @@ -22,6 +24,42 @@ import type { SourceControlProviderContext } from "@t3tools/source-control-core/ | 22 | 24 | // A Gitea list page can hold 50 pull request bodies; gt's output is otherwise capped at 1 MB. | 23 | 25 | const API_MAX_OUTPUT_BYTES = 16 * 1024 * 1024; | 24 | 26 |
| | 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 | + | 25 | 63 | const encodeApiBody = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); | 26 | 64 | const isVcsProcessExitError = Schema.is(VcsProcessExitError); | 27 | 65 |
| @@ -561,7 +599,7 @@ export const make = Effect.gen(function* () { | 561 | 599 | : new ForgejoCliError({ | 562 | 600 | command: "fj", | 563 | 601 | cwd: input.cwd, | 564 | | - detail: "Forgejo API request failed or timed out.", | | 602 | + detail: describeFjRequestFailure(error), | 565 | 603 | }), | 566 | 604 | ), | 567 | 605 | ), | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import { SourceControlProviderKind } from "@t3tools/contracts"; | 2 | 2 | import { assert, it } from "@effect/vitest"; | 3 | 3 | import * as Effect from "effect/Effect"; | 4 | 4 | import * as Exit from "effect/Exit"; | | 5 | +import * as Fiber from "effect/Fiber"; | 5 | 6 | import * as FileSystem from "effect/FileSystem"; | 6 | 7 | import * as Layer from "effect/Layer"; | 7 | 8 | import * as Option from "effect/Option"; | @@ -10,6 +11,7 @@ import * as Schema from "effect/Schema"; | 10 | 11 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; | 11 | 12 | import * as TestClock from "effect/testing/TestClock"; | 12 | 13 | import * as HttpClient from "effect/http/HttpClient"; | | 14 | +import * as HttpClientError from "effect/http/HttpClientError"; | 13 | 15 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; | 14 | 16 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; | 15 | 17 | import { | @@ -3043,3 +3045,112 @@ it.effect("exposes fixed not-found details only for repository lookup", () => | 3043 | 3045 | ), | 3044 | 3046 | ), | 3045 | 3047 | ); | | 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 | +); |
|