Skip to content

Commit b4b4828

Browse files
CCCLLceki
authored andcommitted
Escape regex metacharacters of literal text in FileNamePattern regexes
FileNamePattern.toRegex() and toRegexForFixedDate() copied the literal parts of the file name pattern, and the formatted date, into the regex unchanged. Characters such as '(', '[' or '+' in a directory or file name were therefore interpreted as regex syntax: - RollingFileAppender.start() threw a PatternSyntaxException when the path contained an unbalanced '(', e.g. a Windows user directory such as "C:/Users/Michael(Financ" (#914). - With SizeAndTimeBasedRollingPolicy and a pattern such as "app.%d.(%i).log", the regex did not match existing archives. After a restart within the same period the counter started again at 0, so the next size based rollover renamed the active file onto the existing archive "(0)" and its content was lost. maxHistory and totalSizeCap never removed such archives either (#933). Literal characters with a special meaning in regular expressions are now escaped one by one. Pattern.quote() is not used because callers keep only the part of the regex after the last '/'. The strings returned by toRegex() and toRegexForFixedDate() change accordingly, e.g. "foo-2003.05.20-(\d+).txt" becomes "foo-2003\.05\.20-(\d+)\.txt". Fixes #914 Fixes #933 Signed-off-by: Jialin Chen <[email protected]>
1 parent 5491685 commit b4b4828

4 files changed

Lines changed: 90 additions & 9 deletions

File tree

‎logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public class FileNamePattern extends ContextAwareBase {
4646
CONVERTER_MAP.put(DateTokenConverter.CONVERTER_KEY, DateTokenConverter::new);
4747
}
4848

49+
// characters with a special meaning in regular expressions outside of character classes
50+
static final String REGEX_META_CHARACTERS = "\\.[]{}()*+?^$|";
51+
4952
String pattern;
5053
Converter<Object> headTokenConverter;
5154

@@ -201,11 +204,11 @@ public String toRegexForFixedDate(Date date) {
201204
Converter<Object> p = headTokenConverter;
202205
while (p != null) {
203206
if (p instanceof LiteralConverter) {
204-
buf.append(p.convert(null));
207+
buf.append(escapeRegexMetaCharacters(p.convert(null)));
205208
} else if (p instanceof IntegerTokenConverter) {
206209
buf.append("(\\d+)");
207210
} else if (p instanceof DateTokenConverter) {
208-
buf.append(p.convert(date));
211+
buf.append(escapeRegexMetaCharacters(p.convert(date)));
209212
}
210213
p = p.getNext();
211214
}
@@ -226,11 +229,11 @@ public String toRegexForFixedDate(Instant instant) {
226229
Converter<Object> p = headTokenConverter;
227230
while (p != null) {
228231
if (p instanceof LiteralConverter) {
229-
buf.append(p.convert(null));
232+
buf.append(escapeRegexMetaCharacters(p.convert(null)));
230233
} else if (p instanceof IntegerTokenConverter) {
231234
buf.append("(\\d+)");
232235
} else if (p instanceof DateTokenConverter) {
233-
buf.append(p.convert(instant));
236+
buf.append(escapeRegexMetaCharacters(p.convert(instant)));
234237
}
235238
p = p.getNext();
236239
}
@@ -246,7 +249,7 @@ public String toRegex() {
246249
Converter<Object> p = headTokenConverter;
247250
while (p != null) {
248251
if (p instanceof LiteralConverter) {
249-
buf.append(p.convert(null));
252+
buf.append(escapeRegexMetaCharacters(p.convert(null)));
250253
} else if (p instanceof IntegerTokenConverter) {
251254
buf.append("\\d+");
252255
} else if (p instanceof DateTokenConverter) {
@@ -257,4 +260,24 @@ public String toRegex() {
257260
}
258261
return buf.toString();
259262
}
263+
264+
/**
265+
* Escapes the characters of a literal part of this pattern which have a special
266+
* meaning in regular expressions, e.g. '(' in "C:/Users/John(Doe)/app.log".
267+
*
268+
* <p>Characters are escaped one by one instead of using {@link java.util.regex.Pattern#quote(String)}
269+
* because callers keep only the part of the regex after the last '/', see
270+
* {@link FileFilterUtil#afterLastSlash(String)}.</p>
271+
*/
272+
static String escapeRegexMetaCharacters(String literal) {
273+
StringBuilder buf = new StringBuilder(literal.length());
274+
for (int i = 0; i < literal.length(); i++) {
275+
char c = literal.charAt(i);
276+
if (REGEX_META_CHARACTERS.indexOf(c) != -1) {
277+
buf.append('\\');
278+
}
279+
buf.append(c);
280+
}
281+
return buf.toString();
282+
}
260283
}

‎logback-core/src/test/java/ch/qos/logback/core/rolling/RollingFileAppenderTest.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ public void testFileNameWithParenthesis() {
151151
rfa.doAppend("hello");
152152
}
153153

154+
// https://github.com/qos-ch/logback/issues/914
155+
@Test
156+
public void testFileNameWithUnbalancedParenthesis() {
157+
rfa.setContext(context);
158+
rfa.setFile(randomOutputDir + "user(trunc/toto.log");
159+
tbrp.setFileNamePattern(randomOutputDir + "user(trunc/toto-%d.log");
160+
tbrp.start();
161+
rfa.setRollingPolicy(tbrp);
162+
rfa.start();
163+
Assertions.assertTrue(rfa.isStarted());
164+
rfa.doAppend("hello");
165+
}
166+
154167
@Test
155168
public void stopTimeBasedRollingPolicy() {
156169
rfa.setContext(context);

‎logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import ch.qos.logback.core.status.testUtil.StatusChecker;
3838
import ch.qos.logback.core.util.FileSize;
3939

40+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4041
import static org.junit.jupiter.api.Assertions.assertEquals;
4142
import static org.junit.jupiter.api.Assertions.assertFalse;
4243

@@ -298,6 +299,31 @@ public void checkInitialFileSize_withoutFile() throws IOException {
298299
assertEquals(fixedContentBytes.length, tbrp1.getLengthCounter().getLength());
299300
}
300301

302+
// https://github.com/qos-ch/logback/issues/933
303+
@Test
304+
public void restartDoesNotOverwriteArchiveWithParenthesesInPattern() throws IOException {
305+
String testId = "restartDoesNotOverwriteArchiveWithParenthesesInPattern";
306+
byte[] previousRunBytes = "previous run".getBytes();
307+
308+
CachingDateFormatter cdf = new CachingDateFormatter(CoreConstants.DAILY_DATE_PATTERN);
309+
String nowString = cdf.format(currentTime);
310+
String pathToArchive0 = randomOutputDir + testId + "-" + nowString + "-(0).txt";
311+
312+
Files.createDirectories(Paths.get(randomOutputDir));
313+
Files.write(Paths.get(pathToArchive0), previousRunBytes);
314+
315+
initRollingFileAppender(rfa1, randomOutputDir + "foo.log");
316+
sizeThreshold = 300;
317+
initPolicies(rfa1, tbrp1, randomOutputDir + testId + "-%d-(%i).txt", sizeThreshold, currentTime, 0);
318+
319+
rfa1.doAppend("x".repeat(sizeThreshold + 1));
320+
// triggers a size based rollover
321+
rfa1.doAppend("y");
322+
rfa1.stop();
323+
324+
assertArrayEquals(previousRunBytes, Files.readAllBytes(Paths.get(pathToArchive0)));
325+
}
326+
301327
// @Test
302328
// public void testHistoryAsFileCount() throws IOException {
303329
// String testId = "testHistoryAsFileCount";

‎logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.time.ZoneId;
1717
import java.util.Calendar;
1818
import java.util.TimeZone;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
1921

2022
import org.junit.jupiter.api.Test;
2123

@@ -25,6 +27,7 @@
2527
import static org.junit.jupiter.api.Assertions.assertEquals;
2628
import static org.junit.jupiter.api.Assertions.assertNotNull;
2729
import static org.junit.jupiter.api.Assertions.assertNull;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
2831

2932
/**
3033
* @author Ceki
@@ -154,12 +157,12 @@ public void asRegexByDate() {
154157
{
155158
FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
156159
String regex = fnp.toRegexForFixedDate(cal.getTime());
157-
assertEquals("foo-2003.05.20-(\\d+).txt", regex);
160+
assertEquals("foo-2003\\.05\\.20-(\\d+)\\.txt", regex);
158161
}
159162
{
160163
FileNamePattern fnp = new FileNamePattern("\\toto\\foo-%d{yyyy\\MM\\dd}-%i.txt", context);
161164
String regex = fnp.toRegexForFixedDate(cal.getTime());
162-
assertEquals("/toto/foo-2003/05/20-(\\d+).txt", regex);
165+
assertEquals("/toto/foo-2003/05/20-(\\d+)\\.txt", regex);
163166
}
164167
}
165168

@@ -168,15 +171,31 @@ public void asRegex() {
168171
{
169172
FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
170173
String regex = fnp.toRegex();
171-
assertEquals("foo-\\d{4}\\.\\d{2}\\.\\d{2}-\\d+.txt", regex);
174+
assertEquals("foo-\\d{4}\\.\\d{2}\\.\\d{2}-\\d+\\.txt", regex);
172175
}
173176
{
174177
FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd'T'}-%i.txt", context);
175178
String regex = fnp.toRegex();
176-
assertEquals("foo-\\d{4}\\.\\d{2}\\.\\d{2}T-\\d+.txt", regex);
179+
assertEquals("foo-\\d{4}\\.\\d{2}\\.\\d{2}T-\\d+\\.txt", regex);
177180
}
178181
}
179182

183+
// https://github.com/qos-ch/logback/issues/914
184+
// https://github.com/qos-ch/logback/issues/933
185+
@Test
186+
public void regexMatchesLiteralRegexMetaCharacters() {
187+
Calendar cal = Calendar.getInstance();
188+
cal.set(2003, 4, 20, 17, 55);
189+
FileNamePattern fnp = new FileNamePattern("dir(x/foo+[%d{yyyy.MM.dd}].(%i).txt", context);
190+
String fileName = "dir(x/foo+[2003.05.20].(7).txt";
191+
192+
Matcher matcher = Pattern.compile(fnp.toRegexForFixedDate(cal.getTime())).matcher(fileName);
193+
assertTrue(matcher.matches());
194+
assertEquals("7", matcher.group(1));
195+
196+
assertTrue(Pattern.compile(fnp.toRegex()).matcher(fileName).matches());
197+
}
198+
180199
@Test
181200
public void convertMultipleDates() {
182201
Calendar cal = Calendar.getInstance();

0 commit comments

Comments
 (0)