Skip to content

Commit e46bb6b

Browse files
authored
#129 protection vs null pointer (#134)
* Added LogContext and ServerLogFormatter, amended ServerLogging to use new formatter. * Minor change to ConnectionHandler to generate and set connection ID, and LoggingFilter to use the logger, ensuring connection ID is included in the logs * created a test, ConnectionIdLoggingTest, to verify that the connection ID is correctly included in the logs * Updated LoggingFilterTest to check the java.util.logging.Logger instead of System.out, reflecting recent changes to LoggingFilter * Updated LoggingFilterTest to check the java.util.logging.Logger instead of System.out, reflecting recent changes to LoggingFilter (#95) * Apply Spotless formatting to resolve CI failure (#95) * Include stack traces in log output as suggested by code review (#95) * build: add JSpecify, Error Prone and NullAway for null safety * feat: add NullMarked to all packages * fix: resolve nullability issues identified by NullAway * build: update Error Prone/NullAway versions and fix compiler args * fix: additional nullability issues in StaticFileHandler and ConfigLoader * fix: resolve remaining nullability issues in tests and supporting classes * style: apply spotless formatting fixes * fix: mark LogContext.getConnectionId as @nullable * style: fix formatting in LogContext.java * test: pin logger level and use SimpleFormatter in LoggingFilterTest * refactor: update log formatter to use record instant and add null protection to ConnectionHandler * fix: resolve NullAway errors in HttpResponse, CorsFilter and CorsFilterTest
1 parent 24c1a34 commit e46bb6b

31 files changed

Lines changed: 154 additions & 40 deletions

pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<junit.jupiter.version>6.0.2</junit.jupiter.version>
2222
<assertj.core.version>3.27.7</assertj.core.version>
2323
<mockito.version>5.21.0</mockito.version>
24+
<errorprone.version>2.45.0</errorprone.version>
25+
<nullaway.version>0.13.1</nullaway.version>
2426
<build.time>${maven.build.timestamp}</build.time>
2527
<argLine/>
2628
</properties>
@@ -72,6 +74,11 @@
7274
<version>1.21.4</version>
7375
<scope>test</scope>
7476
</dependency>
77+
<dependency>
78+
<groupId>org.jspecify</groupId>
79+
<artifactId>jspecify</artifactId>
80+
<version>1.0.0</version>
81+
</dependency>
7582
</dependencies>
7683
<build>
7784
<plugins>
@@ -84,6 +91,36 @@
8491
<groupId>org.apache.maven.plugins</groupId>
8592
<artifactId>maven-compiler-plugin</artifactId>
8693
<version>3.15.0</version>
94+
<configuration>
95+
<fork>true</fork>
96+
<compilerArgs>
97+
<arg>-XDcompilePolicy=simple</arg>
98+
<arg>--should-stop=ifError=FLOW</arg>
99+
<arg>-Xplugin:ErrorProne -Xep:NullAway:ERROR -XepOpt:NullAway:AnnotatedPackages=org.juv25d</arg>
100+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
101+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
102+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
103+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
104+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
105+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
106+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
107+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
108+
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
109+
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
110+
</compilerArgs>
111+
<annotationProcessorPaths>
112+
<path>
113+
<groupId>com.google.errorprone</groupId>
114+
<artifactId>error_prone_core</artifactId>
115+
<version>${errorprone.version}</version>
116+
</path>
117+
<path>
118+
<groupId>com.uber.nullaway</groupId>
119+
<artifactId>nullaway</artifactId>
120+
<version>${nullaway.version}</version>
121+
</path>
122+
</annotationProcessorPaths>
123+
</configuration>
87124
</plugin>
88125
<plugin>
89126
<groupId>org.apache.maven.plugins</groupId>

src/main/java/org/juv25d/ConnectionHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public void run() {
3232
var out = socket.getOutputStream();
3333

3434
HttpRequest parsed = httpParser.parse(in);
35-
String remoteIp = socket.getInetAddress().getHostAddress();
35+
java.net.InetAddress inetAddress = socket.getInetAddress();
36+
String remoteIp = (inetAddress != null) ? inetAddress.getHostAddress() : "UNKNOWN";
3637

3738
HttpRequest request = new HttpRequest(
3839
parsed.method(),

src/main/java/org/juv25d/config/FilterConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.juv25d.config;
22

33
import java.util.Map;
4+
import org.jspecify.annotations.Nullable;
45

56
public class FilterConfig {
67

@@ -10,7 +11,7 @@ public FilterConfig(Map<String, String> params) {
1011
this.params = params;
1112
}
1213

13-
public String get(String key) {
14+
@Nullable public String get(String key) {
1415
return params.get(key);
1516
}
1617
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package org.juv25d.config;
3+
4+
import org.jspecify.annotations.NullMarked;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package org.juv25d.di;
3+
4+
import org.jspecify.annotations.NullMarked;

src/main/java/org/juv25d/filter/CorsFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void doFilter(HttpRequest req, HttpResponse res, FilterChain chain) throw
6363
chain.doFilter(req, res);
6464
}
6565

66-
private String header(Map<String, String> headers, String key) {
66+
@org.jspecify.annotations.Nullable private String header(Map<String, String> headers, @org.jspecify.annotations.Nullable String key) {
67+
if (key == null) return null;
6768
for (var entry : headers.entrySet()) {
6869
if (entry.getKey() != null && entry.getKey().equalsIgnoreCase(key)) {
6970
return entry.getValue();
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.juv25d.filter;
22

3-
public record FilterRegistration(Filter filter, int order, String pattern)
3+
import org.jspecify.annotations.Nullable;
4+
5+
public record FilterRegistration(Filter filter, int order, @Nullable String pattern)
46
implements Comparable<FilterRegistration> {
57

68
@Override
79
public int compareTo(FilterRegistration o) {
810
return Integer.compare(this.order, o.order);
911
}
10-
}
12+
}

src/main/java/org/juv25d/filter/IpFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.juv25d.http.HttpRequest;
66
import org.juv25d.http.HttpResponse;
77

8+
import org.jspecify.annotations.Nullable;
9+
810
import java.io.IOException;
911
import java.nio.charset.StandardCharsets;
1012
import java.util.HashSet;
@@ -17,7 +19,7 @@ public class IpFilter implements Filter {
1719

1820
private final boolean allowByDefault;
1921

20-
public IpFilter(Set<String> whitelist, Set<String> blacklist, boolean allowByDefault) {
22+
public IpFilter(@Nullable Set<String> whitelist, @Nullable Set<String> blacklist, boolean allowByDefault) {
2123
if (whitelist != null) {
2224
this.whitelist.addAll(whitelist);
2325
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package org.juv25d.filter.annotation;
3+
4+
import org.jspecify.annotations.NullMarked;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package org.juv25d.filter;
3+
4+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)