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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 java.io.IOException;
  45. import java.text.MessageFormat;
  46. import org.eclipse.jgit.errors.ConfigInvalidException;
  47. import org.eclipse.jgit.internal.JGitText;
  48. import org.eclipse.jgit.lib.Config;
  49. import org.eclipse.jgit.lib.ConfigConstants;
  50. /**
  51. * Validations for the git submodule fields (name, path, uri).
  52. *
  53. * Invalid values in these fields can cause security problems as reported in
  54. * CVE-2018-11235 and and CVE-2018-17456
  55. */
  56. public class SubmoduleValidator {
  57. /**
  58. * Error validating a git submodule declaration
  59. */
  60. public static class SubmoduleValidationException extends Exception {
  61. /**
  62. * @param message
  63. * Description of the problem
  64. */
  65. public SubmoduleValidationException(String message) {
  66. super(message);
  67. }
  68. private static final long serialVersionUID = 1L;
  69. }
  70. /**
  71. * Validate name for a submodule
  72. *
  73. * @param name
  74. * name of a submodule
  75. * @throws SubmoduleValidationException
  76. * name doesn't seem valid (detail in message)
  77. */
  78. public static void assertValidSubmoduleName(String name)
  79. throws SubmoduleValidationException {
  80. if (name.contains("/../") || name.contains("\\..\\") //$NON-NLS-1$ //$NON-NLS-2$
  81. || name.startsWith("../") || name.startsWith("..\\") //$NON-NLS-1$ //$NON-NLS-2$
  82. || name.endsWith("/..") || name.endsWith("\\..")) { //$NON-NLS-1$ //$NON-NLS-2$
  83. // Submodule names are used to store the submodule repositories
  84. // under $GIT_DIR/modules. Having ".." in submodule names makes a
  85. // vulnerability (CVE-2018-11235
  86. // https://bugs.eclipse.org/bugs/show_bug.cgi?id=535027#c0)
  87. // Reject names containing ".." path segments. We don't
  88. // automatically replace these characters or canonicalize by
  89. // regarding the name as a file path.
  90. // Since Path class is platform dependent, we manually check '/' and
  91. // '\\' patterns here.
  92. throw new SubmoduleValidationException(MessageFormat
  93. .format(JGitText.get().invalidNameContainsDotDot, name));
  94. }
  95. if (name.startsWith("-")) { //$NON-NLS-1$
  96. throw new SubmoduleValidationException(
  97. MessageFormat.format(
  98. JGitText.get().submoduleNameInvalid, name));
  99. }
  100. }
  101. /**
  102. * Validate URI for a submodule
  103. *
  104. * @param uri
  105. * uri of a submodule
  106. * @throws SubmoduleValidationException
  107. * uri doesn't seem valid
  108. */
  109. public static void assertValidSubmoduleUri(String uri)
  110. throws SubmoduleValidationException {
  111. if (uri.startsWith("-")) { //$NON-NLS-1$
  112. throw new SubmoduleValidationException(
  113. MessageFormat.format(
  114. JGitText.get().submoduleUrlInvalid, uri));
  115. }
  116. }
  117. /**
  118. * Validate path for a submodule
  119. *
  120. * @param path
  121. * path of a submodule
  122. * @throws SubmoduleValidationException
  123. * path doesn't look right
  124. */
  125. public static void assertValidSubmodulePath(String path)
  126. throws SubmoduleValidationException {
  127. if (path.startsWith("-")) { //$NON-NLS-1$
  128. throw new SubmoduleValidationException(
  129. MessageFormat.format(
  130. JGitText.get().submodulePathInvalid, path));
  131. }
  132. }
  133. /**
  134. * @param gitModulesContents
  135. * Contents of a .gitmodule file. They will be parsed internally.
  136. * @throws IOException
  137. * If the contents
  138. */
  139. public static void assertValidGitModulesFile(String gitModulesContents)
  140. throws IOException {
  141. // Validate .gitmodules file
  142. Config c = new Config();
  143. try {
  144. c.fromText(gitModulesContents);
  145. for (String subsection : c.getSubsections(
  146. ConfigConstants.CONFIG_SUBMODULE_SECTION)) {
  147. String url = c.getString(
  148. ConfigConstants.CONFIG_SUBMODULE_SECTION,
  149. subsection, ConfigConstants.CONFIG_KEY_URL);
  150. assertValidSubmoduleUri(url);
  151. assertValidSubmoduleName(subsection);
  152. String path = c.getString(
  153. ConfigConstants.CONFIG_SUBMODULE_SECTION, subsection,
  154. ConfigConstants.CONFIG_KEY_PATH);
  155. assertValidSubmodulePath(path);
  156. }
  157. } catch (ConfigInvalidException e) {
  158. throw new IOException(
  159. MessageFormat.format(
  160. JGitText.get().invalidGitModules,
  161. e));
  162. } catch (SubmoduleValidationException e) {
  163. throw new IOException(e.getMessage(), e);
  164. }
  165. }
  166. }