From cb32ea196a3127b8415bdac4242a45b9d4a9dfce Mon Sep 17 00:00:00 2001 From: Morgan Ludtke <42942267+ludtkemorgan@users.noreply.github.com> Date: Tue, 21 Nov 2023 18:24:48 -0600 Subject: [PATCH] fix: remove password from logs (#3723) --- shared-helpers/index.ts | 1 + shared-helpers/src/utilities/maskData.ts | 25 +++++++++++++++++++ .../src/pages/api/adapter/[...backendUrl].ts | 6 ++++- .../src/pages/api/adapter/[...backendUrl].ts | 6 ++++- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 shared-helpers/src/utilities/maskData.ts diff --git a/shared-helpers/index.ts b/shared-helpers/index.ts index 6adfb676e6..74a81269f2 100644 --- a/shared-helpers/index.ts +++ b/shared-helpers/index.ts @@ -17,6 +17,7 @@ export * from "./src/utilities/token" export * from "./src/utilities/unitTypes" export * from "./src/utilities/DateFormat" export * from "./src/utilities/constants" +export * from "./src/utilities/maskData" export * from "./src/views/multiselectQuestions" export * from "./src/views/occupancyFormatting" export * from "./src/views/summaryTables" diff --git a/shared-helpers/src/utilities/maskData.ts b/shared-helpers/src/utilities/maskData.ts new file mode 100644 index 0000000000..8d8c122e63 --- /dev/null +++ b/shared-helpers/src/utilities/maskData.ts @@ -0,0 +1,25 @@ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export const maskAxiosResponse = (response: any) => { + const configData = response?.config?.data ? JSON.parse(response.config.data) : undefined + const maskedResponse = !configData + ? response + : { + ...response, + config: { ...response.config, data: maskData(configData) }, + } + + return maskedResponse +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export const maskData = (data: any) => { + const maskedData = { ...data } + if (data.password) { + maskedData.password = "*******" + } + if (data.email) { + const emailChunks = data.email.split("@") + maskedData.email = emailChunks.length === 2 ? `****@${emailChunks[1]}` : data.email + } + return maskedData +} diff --git a/sites/partners/src/pages/api/adapter/[...backendUrl].ts b/sites/partners/src/pages/api/adapter/[...backendUrl].ts index b2a0822489..4ae107903e 100644 --- a/sites/partners/src/pages/api/adapter/[...backendUrl].ts +++ b/sites/partners/src/pages/api/adapter/[...backendUrl].ts @@ -4,6 +4,7 @@ import qs from "qs" import { getConfigs } from "@bloom-housing/backend-core/types" import { wrapper } from "axios-cookiejar-support" import { CookieJar } from "tough-cookie" +import { maskAxiosResponse } from "@bloom-housing/shared-helpers" /* This file exists as per https://nextjs.org/docs/api-routes/dynamic-api-routes @@ -51,7 +52,10 @@ export default async (req: NextApiRequest, res: NextApiResponse) => { res.statusMessage = response.statusText res.status(response.status).json(response.data) } catch (e) { - console.error("partner's backend url adapter error:", { e }) + console.error( + "partner's backend url adapter error:", + e.response ? maskAxiosResponse(e.response) : e + ) if (e.response) { res.statusMessage = e.response.statusText res.status(e.response.status).json(e.response.data) diff --git a/sites/public/src/pages/api/adapter/[...backendUrl].ts b/sites/public/src/pages/api/adapter/[...backendUrl].ts index f02ea6d6ba..bb42c7183b 100644 --- a/sites/public/src/pages/api/adapter/[...backendUrl].ts +++ b/sites/public/src/pages/api/adapter/[...backendUrl].ts @@ -4,6 +4,7 @@ import qs from "qs" import { getConfigs } from "@bloom-housing/backend-core/types" import { wrapper } from "axios-cookiejar-support" import { CookieJar } from "tough-cookie" +import { maskAxiosResponse } from "@bloom-housing/shared-helpers" /* This file exists as per https://nextjs.org/docs/api-routes/dynamic-api-routes @@ -51,7 +52,10 @@ export default async (req: NextApiRequest, res: NextApiResponse) => { res.statusMessage = response.statusText res.status(response.status).json(response.data) } catch (e) { - console.error("public's backend url adapter error:", { e }) + console.error( + "public backend url adapter error:", + e.response ? maskAxiosResponse(e.response) : e + ) if (e.response) { res.statusMessage = e.response.statusText res.status(e.response.status).json(e.response.data)