@@ -3,6 +3,31 @@ import dbConnect from '@/lib/mongodb';
33import { Notification } from '@/models/Notification' ;
44import { NotificationPayload , NotificationResponse } from '@/types/index' ;
55
6+ /**
7+ * Masks an email address to prevent PII exposure in unauthenticated responses.
8+ * Example: "john.doe@gmail.com" → "jo***@gm***.com"
9+ */
10+ function maskEmail ( email : string ) : string {
11+ const [ local , domain ] = email . split ( '@' ) ;
12+ if ( ! local || ! domain ) return '***@***.***' ;
13+
14+ const maskedLocal = local . slice ( 0 , Math . min ( 2 , local . length ) ) + '***' ;
15+
16+ const dotIndex = domain . lastIndexOf ( '.' ) ;
17+ if ( dotIndex === - 1 ) {
18+ // Domain without a TLD (e.g., "localhost") — mask without trailing dot
19+ const maskedDomain = domain . slice ( 0 , Math . min ( 2 , domain . length ) ) + '***' ;
20+ return `${ maskedLocal } @${ maskedDomain } ` ;
21+ }
22+
23+ const domainName = domain . slice ( 0 , dotIndex ) ;
24+ const tld = domain . slice ( dotIndex + 1 ) ;
25+
26+ const maskedDomain = domainName . slice ( 0 , Math . min ( 2 , domainName . length ) ) + '***' ;
27+
28+ return `${ maskedLocal } @${ maskedDomain } .${ tld } ` ;
29+ }
30+
631// ─── POST /api/notify ────────────────────────────────────────────────────────
732// Register or update email notification preferences for a user
833export async function POST ( req : NextRequest ) : Promise < NextResponse < NotificationResponse > > {
@@ -106,13 +131,15 @@ export async function GET(req: NextRequest): Promise<NextResponse<NotificationRe
106131 ) ;
107132 }
108133
134+ // Mask the email to prevent PII exposure in unauthenticated GET responses.
135+ // The full email is only accepted on POST (write) — never returned on GET (read).
109136 return NextResponse . json (
110137 {
111138 success : true ,
112139 message : 'Notification preferences fetched successfully.' ,
113140 data : {
114141 username : notification . username ,
115- email : notification . email ,
142+ email : maskEmail ( notification . email ) ,
116143 frequency : notification . frequency ,
117144 preferences : {
118145 notifyOnCommit : notification . notifyOnCommit ,
0 commit comments