Skip to content

Commit

Permalink
fix: remove password from logs (#3723)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludtkemorgan authored Nov 22, 2023
1 parent 7f93f63 commit cb32ea1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions shared-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 25 additions & 0 deletions shared-helpers/src/utilities/maskData.ts
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion sites/partners/src/pages/api/adapter/[...backendUrl].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion sites/public/src/pages/api/adapter/[...backendUrl].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cb32ea1

Please sign in to comment.