Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/user-data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { db } from "@/lib/db";

const logError = (functionName: string, params: Record<string, unknown>, error: unknown) => {
console.error(
JSON.stringify({
timestamp: new Date().toISOString(),
level: "error",
context: functionName,
params,
error: error instanceof Error ? error.message : "Unknown error",
stack: error instanceof Error ? error.stack : undefined,
})
);
};
Comment on lines +3 to +14

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Consider redacting PII before logging.

The params object is logged directly, which means email addresses will appear in logs when getUserByEmail fails (line 38). Logging user emails can create compliance issues under GDPR/CCPA and increases data breach risk.

Consider masking sensitive fields:

🛡️ Proposed fix to mask email
 const logError = (functionName: string, params: Record<string, unknown>, error: unknown) => {
+    // Mask sensitive fields before logging
+    const safeParams = { ...params };
+    if (typeof safeParams.email === 'string') {
+        safeParams.email = safeParams.email.replace(/(.{2})(.*)(@.*)/, '$1***$3');
+    }
     console.error(
         JSON.stringify({
             timestamp: new Date().toISOString(),
             level: "error",
             context: functionName,
-            params,
+            params: safeParams,
             error: error instanceof Error ? error.message : "Unknown error",
             stack: error instanceof Error ? error.stack : undefined,
         })
     );
 };
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/user-data.ts` around lines 3 - 14, The logs currently include raw params
in logError, causing emails to be written when getUserByEmail fails; update
logging to redact PII by either (A) adding a small sanitizer function (e.g.,
redactParams or maskPII) that detects and masks common sensitive keys like
"email", "phone", "ssn" and use it inside logError before stringifying params,
or (B) call redactParams(getUserByEmailParams) when invoking logError from
getUserByEmail; reference the existing logError function and getUserByEmail call
sites and ensure the masked object (not the original) is what gets logged.


export const getUserById = async (id: string) => {
try {
const user = await db.user.findUnique({
Expand All @@ -9,9 +22,9 @@ export const getUserById = async (id: string) => {
}
});
return user;
} catch (error) {
console.log(error);
return null;
} catch (err) {
logError("getUserById", { id }, err);
throw err;
}
};

Expand All @@ -21,9 +34,9 @@ export const getUserByEmail = async (email: string) => {
where: { email }
});
return user;
} catch (error) {
console.log(error);
return null;
} catch (err) {
logError("getUserByEmail", { email }, err);
throw err;
}
};

Expand All @@ -35,8 +48,8 @@ export const getAccountByUserId = async (userId: string) => {
}
});
return account;
} catch (error) {
console.log(error);
return null;
} catch (err) {
logError("getAccountByUserId", { userId }, err);
throw err;
}
};