* conditions are met:
*
* - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
package org.eclipse.jgit.transport;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import org.eclipse.jgit.errors.PackProtocolException;
import org.eclipse.jgit.lib.ObjectId;
import org.junit.Test;
}
@Test
- public void parseCommand() {
- String input = "0000000000000000000000000000000000000000"
- + " deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
- + " refs/heads/master";
- ReceiveCommand cmd = BaseReceivePack.parseCommand(input);
+ public void parseCommand() throws Exception {
+ String o = "0000000000000000000000000000000000000000";
+ String n = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
+ String r = "refs/heads/master";
+ ReceiveCommand cmd = BaseReceivePack.parseCommand(o + " " + n + " " + r);
assertEquals(ObjectId.zeroId(), cmd.getOldId());
assertEquals("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
cmd.getNewId().name());
assertEquals("refs/heads/master", cmd.getRefName());
+
+ assertParseCommandFails(null);
+ assertParseCommandFails("");
+ assertParseCommandFails(o.substring(35) + " " + n.substring(35)
+ + " " + r + "\n");
+ assertParseCommandFails(o + " " + n + " " + r + "\n");
+ assertParseCommandFails(o + " " + n + " " + "refs^foo");
+ assertParseCommandFails(o + " " + n.substring(10) + " " + r);
+ assertParseCommandFails(o.substring(10) + " " + n + " " + r);
+ assertParseCommandFails("X" + o.substring(1) + " " + n + " " + r);
+ assertParseCommandFails(o + " " + "X" + n.substring(1) + " " + r);
+ }
+
+ private void assertParseCommandFails(String input) {
+ try {
+ BaseReceivePack.parseCommand(input);
+ fail();
+ } catch (PackProtocolException e) {
+ // Expected.
+ }
}
}
continue;
}
- if (line.length() < 83) {
- final String m = JGitText.get().errorInvalidProtocolWantedOldNewRef;
- sendError(m);
- throw new PackProtocolException(m);
+ ReceiveCommand cmd;
+ try {
+ cmd = parseCommand(line);
+ } catch (PackProtocolException e) {
+ sendError(e.getMessage());
+ throw e;
}
-
- final ReceiveCommand cmd = parseCommand(line);
if (cmd.getRefName().equals(Constants.HEAD)) {
cmd.setResult(Result.REJECTED_CURRENT_BRANCH);
} else {
return line;
}
- static ReceiveCommand parseCommand(String line) {
- ObjectId oldId = ObjectId.fromString(line.substring(0, 40));
- ObjectId newId = ObjectId.fromString(line.substring(41, 81));
+ static ReceiveCommand parseCommand(String line) throws PackProtocolException {
+ if (line == null || line.length() < 83) {
+ throw new PackProtocolException(
+ JGitText.get().errorInvalidProtocolWantedOldNewRef);
+ }
+ String oldStr = line.substring(0, 40);
+ String newStr = line.substring(41, 81);
+ ObjectId oldId, newId;
+ try {
+ oldId = ObjectId.fromString(oldStr);
+ newId = ObjectId.fromString(newStr);
+ } catch (IllegalArgumentException e) {
+ throw new PackProtocolException(
+ JGitText.get().errorInvalidProtocolWantedOldNewRef, e);
+ }
String name = line.substring(82);
+ if (!Repository.isValidRefName(name)) {
+ throw new PackProtocolException(
+ JGitText.get().errorInvalidProtocolWantedOldNewRef);
+ }
return new ReceiveCommand(oldId, newId, name);
}