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.

FileResolver.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  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.http.server.resolver;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import javax.servlet.http.HttpServletRequest;
  47. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  48. import org.eclipse.jgit.lib.Repository;
  49. import org.eclipse.jgit.lib.RepositoryCache;
  50. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  51. /** Default resolver serving from a single root path in local filesystem. */
  52. public class FileResolver implements RepositoryResolver {
  53. private final File basePath;
  54. private final boolean exportAll;
  55. /**
  56. * Create a new resolver for the given path.
  57. *
  58. * @param basePath
  59. * the base path all repositories are rooted under.
  60. * @param exportAll
  61. * if true, exports all repositories, ignoring the check for the
  62. * {@code git-daemon-export-ok} files.
  63. */
  64. public FileResolver(final File basePath, final boolean exportAll) {
  65. this.basePath = basePath;
  66. this.exportAll = exportAll;
  67. }
  68. public Repository open(final HttpServletRequest req,
  69. final String repositoryName) throws RepositoryNotFoundException,
  70. ServiceNotEnabledException {
  71. if (isUnreasonableName(repositoryName))
  72. throw new RepositoryNotFoundException(repositoryName);
  73. final Repository db;
  74. try {
  75. final File gitdir = new File(basePath, repositoryName);
  76. db = RepositoryCache.open(FileKey.lenient(gitdir), true);
  77. } catch (IOException e) {
  78. throw new RepositoryNotFoundException(repositoryName, e);
  79. }
  80. try {
  81. if (isExportOk(req, repositoryName, db)) {
  82. // We have to leak the open count to the caller, they
  83. // are responsible for closing the repository if we
  84. // complete successfully.
  85. return db;
  86. } else
  87. throw new ServiceNotEnabledException();
  88. } catch (RuntimeException e) {
  89. db.close();
  90. throw new RepositoryNotFoundException(repositoryName, e);
  91. } catch (IOException e) {
  92. db.close();
  93. throw new RepositoryNotFoundException(repositoryName, e);
  94. } catch (ServiceNotEnabledException e) {
  95. db.close();
  96. throw e;
  97. }
  98. }
  99. /** @return {@code true} if all repositories are to be exported. */
  100. protected boolean isExportAll() {
  101. return exportAll;
  102. }
  103. /**
  104. * Check if this repository can be served over HTTP.
  105. * <p>
  106. * The default implementation of this method returns true only if either
  107. * {@link #isExportAll()} is true, or the {@code git-daemon-export-ok} file
  108. * is present in the repository's directory.
  109. *
  110. * @param req
  111. * the current HTTP request.
  112. * @param repositoryName
  113. * name of the repository, as present in the URL.
  114. * @param db
  115. * the opened repository instance.
  116. * @return true if the repository is accessible; false if not.
  117. * @throws IOException
  118. * the repository could not be accessed, the caller will claim
  119. * the repository does not exist.
  120. */
  121. protected boolean isExportOk(HttpServletRequest req, String repositoryName,
  122. Repository db) throws IOException {
  123. if (isExportAll())
  124. return true;
  125. else
  126. return new File(db.getDirectory(), "git-daemon-export-ok").exists();
  127. }
  128. private static boolean isUnreasonableName(final String name) {
  129. if (name.length() == 0)
  130. return true; // no empty paths
  131. if (name.indexOf('\\') >= 0)
  132. return true; // no windows/dos style paths
  133. if (new File(name).isAbsolute())
  134. return true; // no absolute paths
  135. if (name.startsWith("../"))
  136. return true; // no "l../etc/passwd"
  137. if (name.contains("/../"))
  138. return true; // no "foo/../etc/passwd"
  139. if (name.contains("/./"))
  140. return true; // "foo/./foo" is insane to ask
  141. if (name.contains("//"))
  142. return true; // double slashes is sloppy, don't use it
  143. return false; // is a reasonable name
  144. }
  145. }