You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SubmoduleValidator.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2018, Google LLC.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.submodule;
  44. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH;
  45. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_URL;
  46. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_SUBMODULE_SECTION;
  47. import java.io.IOException;
  48. import java.text.MessageFormat;
  49. import org.eclipse.jgit.errors.ConfigInvalidException;
  50. import org.eclipse.jgit.internal.JGitText;
  51. import org.eclipse.jgit.lib.Config;
  52. import org.eclipse.jgit.lib.ConfigConstants;
  53. /**
  54. * Validations for the git submodule fields (name, path, uri).
  55. *
  56. * Invalid values in these fields can cause security problems as reported in
  57. * CVE-2018-11235 and and CVE-2018-17456
  58. */
  59. public class SubmoduleValidator {
  60. /**
  61. * Error validating a git submodule declaration
  62. */
  63. public static class SubmoduleValidationException extends Exception {
  64. /**
  65. * @param message
  66. * Description of the problem
  67. */
  68. public SubmoduleValidationException(String message) {
  69. super(message);
  70. }
  71. private static final long serialVersionUID = 1L;
  72. }
  73. /**
  74. * Validate name for a submodule
  75. *
  76. * @param name
  77. * name of a submodule
  78. * @throws SubmoduleValidationException
  79. * name doesn't seem valid (detail in message)
  80. */
  81. public static void assertValidSubmoduleName(String name)
  82. throws SubmoduleValidationException {
  83. if (name.contains("/../") || name.contains("\\..\\") //$NON-NLS-1$ //$NON-NLS-2$
  84. || name.startsWith("../") || name.startsWith("..\\") //$NON-NLS-1$ //$NON-NLS-2$
  85. || name.endsWith("/..") || name.endsWith("\\..")) { //$NON-NLS-1$ //$NON-NLS-2$
  86. // Submodule names are used to store the submodule repositories
  87. // under $GIT_DIR/modules. Having ".." in submodule names makes a
  88. // vulnerability (CVE-2018-11235
  89. // https://bugs.eclipse.org/bugs/show_bug.cgi?id=535027#c0)
  90. // Reject names containing ".." path segments. We don't
  91. // automatically replace these characters or canonicalize by
  92. // regarding the name as a file path.
  93. // Since Path class is platform dependent, we manually check '/' and
  94. // '\\' patterns here.
  95. throw new SubmoduleValidationException(MessageFormat
  96. .format(JGitText.get().invalidNameContainsDotDot, name));
  97. }
  98. if (name.startsWith("-")) { //$NON-NLS-1$
  99. throw new SubmoduleValidationException(
  100. MessageFormat.format(
  101. JGitText.get().submoduleNameInvalid, name));
  102. }
  103. }
  104. /**
  105. * Validate URI for a submodule
  106. *
  107. * @param uri
  108. * uri of a submodule
  109. * @throws SubmoduleValidationException
  110. * uri doesn't seem valid
  111. */
  112. public static void assertValidSubmoduleUri(String uri)
  113. throws SubmoduleValidationException {
  114. if (uri.startsWith("-")) { //$NON-NLS-1$
  115. throw new SubmoduleValidationException(
  116. MessageFormat.format(
  117. JGitText.get().submoduleUrlInvalid, uri));
  118. }
  119. }
  120. /**
  121. * Validate path for a submodule
  122. *
  123. * @param path
  124. * path of a submodule
  125. * @throws SubmoduleValidationException
  126. * path doesn't look right
  127. */
  128. public static void assertValidSubmodulePath(String path)
  129. throws SubmoduleValidationException {
  130. if (path.startsWith("-")) { //$NON-NLS-1$
  131. throw new SubmoduleValidationException(
  132. MessageFormat.format(
  133. JGitText.get().submodulePathInvalid, path));
  134. }
  135. }
  136. /**
  137. * @param gitModulesContents
  138. * Contents of a .gitmodule file. They will be parsed internally.
  139. * @throws IOException
  140. * If the contents
  141. */
  142. public static void assertValidGitModulesFile(String gitModulesContents)
  143. throws IOException {
  144. // Validate .gitmodules file
  145. Config c = new Config();
  146. try {
  147. c.fromText(gitModulesContents);
  148. for (String subsection :
  149. c.getSubsections(CONFIG_SUBMODULE_SECTION)) {
  150. assertValidSubmoduleName(subsection);
  151. String url = c.getString(
  152. CONFIG_SUBMODULE_SECTION, subsection, CONFIG_KEY_URL);
  153. if (url != null) {
  154. assertValidSubmoduleUri(url);
  155. }
  156. String path = c.getString(
  157. CONFIG_SUBMODULE_SECTION, subsection, CONFIG_KEY_PATH);
  158. if (path != null) {
  159. assertValidSubmodulePath(path);
  160. }
  161. }
  162. } catch (ConfigInvalidException e) {
  163. throw new IOException(
  164. MessageFormat.format(
  165. JGitText.get().invalidGitModules,
  166. e));
  167. } catch (SubmoduleValidationException e) {
  168. throw new IOException(e.getMessage(), e);
  169. }
  170. }
  171. }