Skip to content

Commit bbf431a

Browse files
kwwallxeno6696
authored andcommitted
Log special (#451)
* Close issue #448 * Close issue #444. Delete deprecated decodeToObject() method and 2 related encodeObject() methods. General whitespace clean-up. Delete EncoderTest.testBase64decodToObject() method which is no longer relevant. * Close issue #385. Close issue #386. NOTE: Was unwilling to comply with request in these 2 issues to make logSpecial() 'protected'. Could not do so without introducing potential security vulnerabilities. However, since the intent of the user submitting these 2 GitHub issues was only to override logSpecial() in order to completely suppress the output from them to stdout or stderr, I have arranged it so that setting the System property 'org.owasp.esapi.logSpecial.discard' to 'true' will do exactly this...it will suppress all output from logSpecial. (Also, the calls to System.err.println() and System.out.println() have been replaced by calls to logSpecial(), which will log to System.out if not suppressed. So the end result is all or nothing. I suspect that most will keep the default behavior, which is to print to System.out rather than suppressing all output.
1 parent 32dd026 commit bbf431a

File tree

3 files changed

+85
-14
lines changed

3 files changed

+85
-14
lines changed

src/main/java/org/owasp/esapi/configuration/AbstractPrioritizedPropertyLoader.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public String name() {
4444
/**
4545
* Initializes properties object and fills it with data from configuration file.
4646
*/
47-
protected void initProperties() {
47+
private void initProperties() {
4848
properties = new Properties();
4949
File file = new File(filename);
5050
if (file.exists() && file.isFile()) {
5151
loadPropertiesFromFile(file);
5252
} else {
53-
System.err.println("Configuration file " + filename + " does not exist");
53+
logSpecial("Configuration file " + filename + " does not exist");
5454
}
5555
}
5656

@@ -59,4 +59,33 @@ protected void initProperties() {
5959
* @param file
6060
*/
6161
protected abstract void loadPropertiesFromFile(File file);
62+
63+
/**
64+
* Used to log errors to the console during the loading of the properties file itself. Can't use
65+
* standard logging in this case, since the Logger may not be initialized yet. Output is sent to
66+
* {@code PrintStream} {@code System.out}. Output is discarded if the {@code System} property
67+
* "org.owasp.esapi.logSpecial.discard" is set to {@code true}.
68+
*
69+
* @param msg The message to log to the console.
70+
* @param t Associated exception that was caught.
71+
*/
72+
protected final void logSpecial(String msg, Throwable t) {
73+
// Note: It is really distasteful to tie this class to DefaultSecurityConfiguration
74+
// like this, but the alternative is to move the logSpecial() and
75+
// logToStdout() some utilities class and that is even more
76+
// distasteful because it may encourage people to use these. -kwwall
77+
org.owasp.esapi.reference.DefaultSecurityConfiguration.logToStdout(msg, t);
78+
}
79+
80+
/**
81+
* Used to log errors to the console during the loading of the properties file itself. Can't use
82+
* standard logging in this case, since the Logger may not be initialized yet. Output is sent to
83+
* {@code PrintStream} {@code System.out}. Output is discarded if the {@code System} property
84+
* "org.owasp.esapi.logSpecial.discard" is set to {@code true}.
85+
*
86+
* @param msg The message to log to the console.
87+
*/
88+
protected final void logSpecial(String msg) {
89+
logSpecial(msg, null);
90+
}
6291
}

src/main/java/org/owasp/esapi/configuration/StandardEsapiPropertyLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ protected void loadPropertiesFromFile(File file) {
9090
input = new FileInputStream(file);
9191
properties.load(input);
9292
} catch (IOException ex) {
93-
System.err.println("Loading " + file.getName() + " via file I/O failed. Exception was: " + ex);
93+
logSpecial("Loading " + file.getName() + " via file I/O failed.", ex);
9494
} finally {
9595
if (input != null) {
9696
try {
9797
input.close();
9898
} catch (IOException e) {
99-
System.err.println("Could not close stream");
99+
logSpecial("Could not close stream");
100100
}
101101
}
102102
}

src/main/java/org/owasp/esapi/reference/DefaultSecurityConfiguration.java

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ public static SecurityConfiguration getInstance() {
153153
public static final String VALIDATION_PROPERTIES_MULTIVALUED = "Validator.ConfigurationFile.MultiValued";
154154
public static final String ACCEPT_LENIENT_DATES = "Validator.AcceptLenientDates";
155155

156+
/**
157+
* Special {@code System} property that, if set to {@code true}, will
158+
* disable logging from {@code DefaultSecurityConfiguration.logToStdout()}
159+
* methods, which is called from various {@code logSpecial()} methods.
160+
* @see org.owasp.esapi.reference.DefaultSecurityConfiguration#logToStdout(String msg, Throwable t)
161+
* @see org.owasp.esapi.reference.DefaultSecurityConfiguration#logToStdout(String msg)
162+
*/
163+
public static final String DISCARD_LOGSPECIAL = "org.owasp.esapi.logSpecial.discard";
164+
165+
// We assume that this does not change in the middle of processing the
166+
// ESAPI.properties files and thus only fetch its value once.
167+
private static final String logSpecialValue = System.getProperty(DISCARD_LOGSPECIAL, "false");
156168

157169

158170
/**
@@ -702,34 +714,64 @@ private Properties loadConfigurationFromClasspath(String fileName) throws Illega
702714
return result;
703715
}
704716

717+
/**
718+
* Log to standard output (i.e., {@code System.out}. This method is
719+
* synchronized to reduce the possibility of interleaving the message
720+
* output (since the {@code System.out} {@code PrintStream} is buffered)
721+
* it invoked from multiple threads. Output is discarded if the
722+
* {@code System} property "org.owasp.esapi.logSpecial.discard" is set to
723+
* {@code true}.
724+
*
725+
* @param msg Message to be logged.
726+
* @param t Associated exception that was caught. The class name and
727+
* exception message is also logged.
728+
* @see #logToStdout(String msg)
729+
*/
730+
public final synchronized static void logToStdout(String msg, Throwable t) {
731+
// Note that this class was made final because it is called from this class'
732+
// CTOR and we want to prohibit someone from <i>easily</i> doing sneaky
733+
// things like subclassing this class and inserting a malicious code as a
734+
// shim. Of course, really in hindsight, this entire class should have been
735+
// declared 'final', but doing so at this point would likely break someone's
736+
// code, including possibly some of our own test code. But since this is a
737+
// new method, we can get away with it here.
738+
boolean discard = logSpecialValue.trim().equalsIgnoreCase("true");
739+
if ( discard ) {
740+
return; // Output is discarded!
741+
}
742+
if ( t == null ) {
743+
System.out.println("ESAPI: " + msg);
744+
} else {
745+
System.out.println("ESAPI: " + msg +
746+
". Caught " + t.getClass().getName() +
747+
"; exception message was: " + t);
748+
}
749+
}
750+
705751
/**
706752
* Used to log errors to the console during the loading of the properties file itself. Can't use
707753
* standard logging in this case, since the Logger may not be initialized yet. Output is sent to
708-
* {@code PrintStream} {@code System.out}.
754+
* {@code PrintStream} {@code System.out}. Output is discarded if the {@code System} property
755+
* "org.owasp.esapi.logSpecial.discard" is set to {@code true}.
709756
*
710757
* @param message The message to send to the console.
711758
* @param e The error that occurred. (This value printed via {@code e.toString()}.)
712759
*/
713760
private void logSpecial(String message, Throwable e) {
714-
StringBuffer msg = new StringBuffer(message);
715-
if (e != null) {
716-
msg.append(" Exception was: ").append( e.toString() );
717-
}
718-
System.out.println( msg.toString() );
719-
// if ( e != null) e.printStackTrace(); // TODO ??? Do we want this?
761+
logToStdout(message, e);
720762
}
721763

722764
/**
723765
* Used to log errors to the console during the loading of the properties file itself. Can't use
724766
* standard logging in this case, since the Logger may not be initialized yet. Output is sent to
725-
* {@code PrintStream} {@code System.out}.
767+
* {@code PrintStream} {@code System.out}. Output is discarded if the {@code System} property
768+
* "org.owasp.esapi.logSpecial.discard" is set to {@code true}.
726769
*
727770
* @param message The message to send to the console.
728771
*/
729772
private void logSpecial(String message) {
730-
System.out.println(message);
773+
logToStdout(message, null);
731774
}
732-
733775
/**
734776
* {@inheritDoc}
735777
*/

0 commit comments

Comments
 (0)