Skip to content

Commit

Permalink
don't include stack for defects (prevent leaking server information)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Mar 21, 2024
1 parent 937151a commit 556a227
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 9 additions & 5 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6053,11 +6053,11 @@ export const TaggedError = <Self = never>(identifier?: string) =>
identifier: identifier ?? tag,
fields: extendFields({ [TAG]: literal(tag) }, fields),
toStringOverride(self) {
if (typeof self.message !== "string" || self.message.length === 0) {
if (!(Predicate.isString(self.message) && self.message.length > 0)) {
return Pretty.make(self.constructor as any)(self)
}
let message = `${self._tag}: ${self.message}`
if ("stack" in self) {
if (Predicate.isString(self.stack)) {
message = `${message}\n${self.stack.split("\n").slice(1).join("\n")}`
}
return message
Expand Down Expand Up @@ -6628,16 +6628,20 @@ export const causeDefectUnknown: $unknown = transform(
unknown,
(u) => {
if (Predicate.isObject(u) && "message" in u && typeof u.message === "string") {
return Object.assign(new Error(u.message, { cause: u }), u)
const err = new Error(u.message, { cause: u })
if ("name" in u && typeof u.name === "string") {
err.name = u.name
}
err.stack = "stack" in u && typeof u.stack === "string" ? u.stack : ""
return err
}
return String(u)
},
(defect) => {
if (defect instanceof Error) {
return {
name: defect.name,
message: defect.message,
stack: defect.stack
message: defect.message
}
}
return String(defect)
Expand Down
4 changes: 1 addition & 3 deletions packages/schema/test/Cause/cause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,13 @@ describe("Cause > cause", () => {
assert(failWithStack._tag === "Die")
assert(
typeof failWithStack.defect === "object" && failWithStack.defect !== null && "message" in failWithStack.defect &&
"stack" in failWithStack.defect && "name" in failWithStack.defect
"name" in failWithStack.defect
)
assert.strictEqual(failWithStack.defect.name, "Error")
assert.strictEqual(failWithStack.defect.message, "fail")
assert.include(failWithStack.defect.stack, "cause.test.ts")

failWithStack = S.encodeSync(schemaUnknown)(Cause.die(new Error("fail")))
assert(failWithStack._tag === "Die")
assert.strictEqual((failWithStack.defect as Error).message, "fail")
assert.include((failWithStack.defect as any).stack, "cause.test.ts")
})
})

0 comments on commit 556a227

Please sign in to comment.