Config: reject NUL bytes in values and subsection names Config.readValue() and Config.readSubsectionName() accepted NUL (0x00) bytes, even though C git cannot represent them and Config.escapeValue() / Config.escapeSubsection() already reject NUL bytes with IllegalArgumentException. Reading a config containing NUL therefore succeeded, while serializing it back via toText() failed. Reject NUL bytes in Config.readValue() and Config.readSubsectionName() (both literal and backslash-escaped) by throwing ConfigInvalidException, reusing the existing JGitText keys configValueContainsNullByte and configSubsectionContainsNullByte. Change-Id: I2c88c12f0f0f4993c38707d6821809bf6a6a6964
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java index 2d87226..bfb7631 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
@@ -1442,12 +1442,23 @@ public void testParseInvalidValues() { assertInvalidValue(JGitText.get().endOfFileInEscape, "x\\"); assertInvalidValue( MessageFormat.format(JGitText.get().badEscape, 'q'), "x\\q"); + assertInvalidValue(JGitText.get().configValueContainsNullByte, "x\0y"); + assertInvalidValue( + JGitText.get().configValueContainsNullByte, "\"x\0y\""); + assertInvalidValue( + JGitText.get().configValueContainsNullByte, "x\\\0y"); + assertInvalidValue( + JGitText.get().configValueContainsNullByte, "\"x\\\0y\""); } @Test public void testParseInvalidSubsections() { assertInvalidSubsection( JGitText.get().newlineInQuotesNotAllowed, "\"x\ny\""); + assertInvalidSubsection( + JGitText.get().configSubsectionContainsNullByte, "\"x\0y\""); + assertInvalidSubsection( + JGitText.get().configSubsectionContainsNullByte, "\"x\\\0y\""); } @Test
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java index 345cb22..28e7739 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java
@@ -1614,6 +1614,10 @@ private static String readSubsectionName(StringReader in) if (c < 0) { break; } + if (c == 0) { + throw new ConfigInvalidException( + JGitText.get().configSubsectionContainsNullByte); + } if ('\n' == c) { throw new ConfigInvalidException( @@ -1625,6 +1629,10 @@ private static String readSubsectionName(StringReader in) case -1: throw new ConfigInvalidException(JGitText.get().endOfFileInEscape); + case 0: + throw new ConfigInvalidException( + JGitText.get().configSubsectionContainsNullByte); + case '\\': case '"': r.append((char) c); @@ -1658,6 +1666,10 @@ private static String readValue(StringReader in) if (c < 0) { break; } + if (c == 0) { + throw new ConfigInvalidException( + JGitText.get().configValueContainsNullByte); + } if ('\n' == c) { if (quote) { throw new ConfigInvalidException( @@ -1723,6 +1735,9 @@ private static String readValue(StringReader in) } break; } + case 0: + throw new ConfigInvalidException( + JGitText.get().configValueContainsNullByte); default: break; }