@@ -21,32 +21,31 @@ export default defineEventHandler(async (event) => {
2121 // 3. For API calls, we allow session or fallback to API Key + IP Whitelist
2222 // Note: We check specifically for /api/release/*
2323 if ( url . pathname . startsWith ( '/api/release/' ) ) {
24- if ( user ) {
25- // User logged in via UI session, allow
24+ const clientIp = getRequestIP ( event , { xForwardedFor : true } ) ;
25+ console . log ( `Auth Middleware: Client IP - ${ clientIp } , User - ${ user ? user . username : 'None' } ` ) ;
26+ // Internal Check: If SSR/Internal Fetch (!clientIp) or Localhost
27+ const isInternal = ! clientIp || clientIp === '127.0.0.1' || clientIp === '::1' || clientIp === 'localhost' ;
28+
29+ // Whitelist Check: IP matches explicitly allowed list
30+ const allowedIps = ( config . notifyReleaseMailAllowedIps || '' ) . split ( ',' ) . map ( ip => ip . trim ( ) ) . filter ( ip => ip ) ;
31+ const isWhitelisted = clientIp && allowedIps . includes ( clientIp ) ;
32+
33+ if ( user || isInternal || isWhitelisted ) {
2634 return ;
2735 }
2836
29- // No session, check API Key and IP
30- const clientIp = getRequestIP ( event , { xForwardedFor : true } ) ;
37+ // No session/whitelist, check API Key and IP requirement
3138 const requestApiKey = getHeader ( event , 'x-api-key' ) ;
32- const allowedIps = ( config . notifyReleaseMailAllowedIps || '' ) . split ( ',' ) . map ( ip => ip . trim ( ) ) . filter ( ip => ip ) ;
33-
34- // Check IP
35- if ( allowedIps . length > 0 && ( ! clientIp || ! allowedIps . includes ( clientIp ) ) ) {
39+
40+ // Check IP separately for more specific error message if whitelist is configured
41+ if ( allowedIps . length > 0 && ! isWhitelisted ) {
3642 throw createError ( { statusCode : 403 , statusMessage : `Forbidden: IP ${ clientIp } not allowed` } ) ;
3743 }
3844
3945 // Check API Key
4046 if ( config . notifyReleaseMailApiKey && requestApiKey !== config . notifyReleaseMailApiKey ) {
4147 throw createError ( { statusCode : 401 , statusMessage : 'Unauthorized: Invalid API Key' } ) ;
4248 }
43-
44- // If neither session nor apiKey present (if apiKey configured)
45- if ( ! config . notifyReleaseMailApiKey && ! user && ! allowedIps . length ) {
46- // If nothing is configured, maybe it's wide open (as before) or we want to block?
47- // Let's allow it for now or force auth if required.
48- // But for safety, let's at least expect a session or key if we're adding auth.
49- }
5049 }
5150
5251 // Store user in context for downstream handlers if needed
0 commit comments