11package org .owasp .esapi .reference ;
22
33import java .util .HashMap ;
4- import org .apache .log4j .Level ;
54
5+ import javax .servlet .http .HttpServletRequest ;
66import javax .servlet .http .HttpSession ;
77
8+ import org .apache .log4j .Level ;
89import org .owasp .esapi .ESAPI ;
910import org .owasp .esapi .LogFactory ;
1011import org .owasp .esapi .Logger ;
@@ -265,16 +266,18 @@ private void log(Level level, EventType type, String message, Throwable throwabl
265266 // create a random session number for the user to represent the user's 'session', if it doesn't exist already
266267 String userSessionIDforLogging = "unknown" ;
267268
268- try {
269- HttpSession session = ESAPI .httpUtilities ().getCurrentRequest ().getSession ( false );
270- userSessionIDforLogging = (String )session .getAttribute ("ESAPI_SESSION" );
271- // if there is no session ID for the user yet, we create one and store it in the user's session
272- if ( userSessionIDforLogging == null ) {
273- userSessionIDforLogging = "" + ESAPI .randomizer ().getRandomInteger (0 , 1000000 );
274- session .setAttribute ("ESAPI_SESSION" , userSessionIDforLogging );
275- }
276- } catch ( NullPointerException e ) {
277- // continue
269+ // add a session token to log if there is an HTTP session
270+ HttpServletRequest request = ESAPI .httpUtilities ().getCurrentRequest ();
271+ if ( request != null ) {
272+ HttpSession session = request .getSession ( false );
273+ if ( session != null ) {
274+ userSessionIDforLogging = (String )session .getAttribute ("ESAPI_SESSION" );
275+ // if there is no session ID for the user yet, we create one and store it in the user's session
276+ if ( userSessionIDforLogging == null ) {
277+ userSessionIDforLogging = "" + ESAPI .randomizer ().getRandomInteger (0 , 1000000 );
278+ session .setAttribute ("ESAPI_SESSION" , userSessionIDforLogging );
279+ }
280+ }
278281 }
279282
280283 // ensure there's something to log
@@ -300,14 +303,14 @@ private void log(Level level, EventType type, String message, Throwable throwabl
300303 boolean logAppName = ((DefaultSecurityConfiguration )ESAPI .securityConfiguration ()).getLogApplicationName ();
301304 boolean logServerIP = ((DefaultSecurityConfiguration )ESAPI .securityConfiguration ()).getLogServerIP ();
302305
303- if (!logServerIP ) {
306+ if (!logServerIP || ESAPI . currentRequest () == null ) {
304307 if (logAppName ) {
305308 jlogger .log (level , applicationName + " " + moduleName + " " + msg , throwable );
306309 } else { //!logAppName
307310 jlogger .log (level , moduleName + " " + msg , throwable );
308311 }
309312 } else { //logServerIP
310- if (logAppName ) {
313+ if (logAppName && ESAPI . currentRequest () != null ) {
311314 jlogger .log (level , applicationName + ":" + ESAPI .currentRequest ().getServerName () + ":" + ESAPI .currentRequest ().getLocalPort () + " " + moduleName + " " + msg , throwable );
312315 } else { //!logAppName
313316 jlogger .log (level , ESAPI .currentRequest ().getServerName () + ":" + ESAPI .currentRequest ().getLocalPort () + " " +moduleName + " " + msg , throwable );
0 commit comments