{"record":{"id":"8479ab3dc511aaeb","repo":"gchq/CyberChef","slug":"err-tostring","errorCode":null,"errorMessage":"${err.toString()}","messagePattern":"\\$\\{err\\.toString\\(\\)\\}","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/BSONDeserialise.mjs","lineNumber":43,"sourceCode":"        this.infoURL = \"https://wikipedia.org/wiki/BSON\";\n        this.inputType = \"ArrayBuffer\";\n        this.outputType = \"string\";\n        this.args = [];\n    }\n\n    /**\n     * @param {ArrayBuffer} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        if (!input.byteLength) return \"\";\n\n        try {\n            const data = deserialize(new Uint8Array(input));\n            return JSON.stringify(data, null, 2);\n        } catch (err) {\n            throw new OperationError(err.toString());\n        }\n    }\n\n}\n\nexport default BSONDeserialise;\n","sourceCodeStart":25,"sourceCodeEnd":50,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/BSONDeserialise.mjs#L25-L50","documentation":"Generic catch-all thrown by BSONDeserialise.run when the underlying bson deserialize() throws on the input bytes. BSON is a strict binary format with length-prefixed documents, typed fields, and terminator bytes; any structural defect (truncation, bad length prefix, unknown/invalid element type, bad UTF-8 string) causes the library to throw, and the original Error.toString() is forwarded verbatim as the OperationError message.","triggerScenarios":"Feeding non-BSON bytes (e.g. JSON text, MessagePack, or random binary), a truncated BSON document, a document whose declared length does not match the buffer, or a string field with invalid UTF-8.","commonSituations":"Piping JSON text into BSONDeserialise by mistake; copy-paste truncating the binary buffer; upstream 'From Hex' with odd-length or corrupted hex.","solutions":["Confirm the input is genuine BSON binary produced by a BSON serialiser.","If you have JSON text, use BSONSerialise's inverse or a JSON parser instead.","Inspect the forwarded Error.toString() message for the specific BSON defect.","Re-serialize the source document and re-copy the full buffer."],"exampleFix":"// before - feeding JSON text\nchef.bSONDeserialise('{\"a\":1}');\n\n// after - feeding BSON bytes from BSONSerialise\nconst bsonBytes = chef.bSONSerialise('{\"a\":1}');\nchef.bSONDeserialise(bsonBytes);","handlingStrategy":"try-catch","validationCode":"function looksLikeBSON(buf) {\n  if (!(buf instanceof ArrayBuffer) || buf.byteLength < 5) return false;\n  const dv = new DataView(buf);\n  const docLen = dv.getInt32(0, true);\n  return docLen >= 5 && docLen <= buf.byteLength;\n}","typeGuard":"function isLikelyBSON(buf) {\n  if (!(buf instanceof ArrayBuffer) || buf.byteLength < 5) return false;\n  const dv = new DataView(buf);\n  const docLen = dv.getInt32(0, true);\n  return docLen >= 5 && docLen <= buf.byteLength;\n}","tryCatchPattern":"try {\n  chef.bSONDeserialise(buf);\n} catch (e) {\n  if (/bson|truncat|length|type/i.test(e.message)) {\n    throw new Error(`Not valid BSON: ${e.message}`);\n  }\n  throw e;\n}","preventionTips":["Confirm the bytes came from a BSON serialiser, not raw JSON text.","Validate the BSON length prefix against the buffer size.","Forward the original library error to diagnose the exact field defect."],"tags":["bson","serialization","format","input-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}