From 5b25cb2b559cbd46f7edf11e855c27f2cc6bd8ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Falk=20Ku=CC=88hnel?= Date: Sat, 14 Nov 2026 21:36:53 +0100 Subject: [PATCH] fix: don't print undecodable bank responses outside debug mode When a response can't be decoded, HttpClient printed its whole content. It holds account data (balances, bookings, names), which then ended up in application logs. It is now printed only in debug mode; the error is still thrown. --- src/httpClient.ts | 8 ++++-- src/tests/httpClientErrorLog.test.ts | 41 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/tests/httpClientErrorLog.test.ts diff --git a/src/httpClient.ts b/src/httpClient.ts index 174c623..429a957 100644 --- a/src/httpClient.ts +++ b/src/httpClient.ts @@ -50,8 +50,12 @@ export class HttpClient { } return responseMessage; } catch (error) { - console.error('Error decoding response message:', error); - console.error('Response Message Content:\n', responseText.split("'").join('\n')); + // The response carries account data (balances, bookings, names), so it is only + // printed when debugging was asked for + if (this.debug) { + console.error('Error decoding response message:', error); + console.error('Response Message Content:\n', responseText.split("'").join('\n')); + } throw error; } } else { diff --git a/src/tests/httpClientErrorLog.test.ts b/src/tests/httpClientErrorLog.test.ts new file mode 100644 index 0000000..0f2fd3c --- /dev/null +++ b/src/tests/httpClientErrorLog.test.ts @@ -0,0 +1,41 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { HttpClient } from '../httpClient.js'; +import { type CustomerMessage, Message } from '../message.js'; + +const message = { + encode: () => "HNHBK:1:3'", + toString: () => 'HNHBK', +} as unknown as CustomerMessage; + +describe('HttpClient with an undecodable response', () => { + afterEach(() => { + vi.restoreAllMocks(); + vi.unstubAllGlobals(); + }); + + it('does not print the response (it holds account data) unless debugging', async () => { + const secret = "HIRMG:3:2+0010::Saldo 1.234,56 Max Mustermann DE99'"; + vi.stubGlobal( + 'fetch', + vi + .fn() + .mockImplementation( + async () => new Response(Buffer.from(`garbage${secret}`, 'latin1').toString('base64')), + ), + ); + vi.spyOn(Message, 'decode').mockImplementation(() => { + throw new Error('cannot decode'); + }); + const errors = vi.spyOn(console, 'error').mockImplementation(() => {}); + + await expect(new HttpClient('http://bank.example').sendMessage(message)).rejects.toThrow( + 'cannot decode', + ); + expect(errors).not.toHaveBeenCalled(); + + await expect(new HttpClient('http://bank.example', true).sendMessage(message)).rejects.toThrow( + 'cannot decode', + ); + expect(errors).toHaveBeenCalled(); + }); +});