1 | package com.reallifedeveloper.common.domain; | |
2 | ||
3 | import org.checkerframework.checker.nullness.qual.Nullable; | |
4 | ||
5 | import lombok.experimental.UtilityClass; | |
6 | ||
7 | /** | |
8 | * A utility class to simplify working with logs. | |
9 | * | |
10 | * @author RealLifeDeveloper | |
11 | */ | |
12 | @UtilityClass | |
13 | public class LogUtil { | |
14 | ||
15 | /** | |
16 | * Removes all occurrences of carriage return ({@code \r}) and linefeed ({@code \n}) from a string. | |
17 | * <p> | |
18 | * This is useful when logging something that originates from a user, in order to avoid CRLF injection. See | |
19 | * <a href="https://www.invicti.com/learn/crlf-injection/">https://www.invicti.com/learn/crlf-injection/</a>. | |
20 | * <p> | |
21 | * The method is null-safe and returns {@code null} if the input string is {@code null}. | |
22 | * | |
23 | * @param s the string from which to remove CRLF | |
24 | * | |
25 | * @return {@code s} with all occurrences of CR and LF removed | |
26 | */ | |
27 | @SuppressWarnings({ "checkstyle:noReturnNull" }) | |
28 | public static @Nullable String removeCRLF(@Nullable String s) { | |
29 |
1
1. removeCRLF : negated conditional → KILLED |
if (s == null) { |
30 |
1
1. removeCRLF : replaced return value with "" for com/reallifedeveloper/common/domain/LogUtil::removeCRLF → KILLED |
return null; |
31 | } else { | |
32 |
1
1. removeCRLF : replaced return value with "" for com/reallifedeveloper/common/domain/LogUtil::removeCRLF → KILLED |
return s.replaceAll("[\r\n]", ""); |
33 | } | |
34 | } | |
35 | ||
36 | /** | |
37 | * Given a non-null object, calls the {@code toString} method on the object and returns the result of calling | |
38 | * {@link #removeCRLF(String)} on the string representation of the object. | |
39 | * <p> | |
40 | * For {@code null} simply returns {@code null}. | |
41 | * | |
42 | * @param o the object for which to remove CRLF from its string representation | |
43 | * | |
44 | * @return {@code o.toString()} with all occurrences of CR and LF removed, or {@code null} if {@code o} is {@code null} | |
45 | */ | |
46 | @SuppressWarnings({ "checkstyle:noReturnNull" }) | |
47 | public static @Nullable String removeCRLF(@Nullable Object o) { | |
48 |
1
1. removeCRLF : negated conditional → KILLED |
if (o == null) { |
49 |
1
1. removeCRLF : replaced return value with "" for com/reallifedeveloper/common/domain/LogUtil::removeCRLF → KILLED |
return null; |
50 | } else { | |
51 |
1
1. removeCRLF : replaced return value with "" for com/reallifedeveloper/common/domain/LogUtil::removeCRLF → KILLED |
return removeCRLF(o.toString()); |
52 | } | |
53 | } | |
54 | } | |
Mutations | ||
29 |
1.1 |
|
30 |
1.1 |
|
32 |
1.1 |
|
48 |
1.1 |
|
49 |
1.1 |
|
51 |
1.1 |