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.

GitFilter.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.text.MessageFormat;
  18. import com.gitblit.Constants.AccessRestrictionType;
  19. import com.gitblit.models.RepositoryModel;
  20. import com.gitblit.models.UserModel;
  21. import com.gitblit.utils.StringUtils;
  22. /**
  23. * The GitFilter is an AccessRestrictionFilter which ensures that Git client
  24. * requests for push, clone, or view restricted repositories are authenticated
  25. * and authorized.
  26. *
  27. * @author James Moger
  28. *
  29. */
  30. public class GitFilter extends AccessRestrictionFilter {
  31. protected static final String gitReceivePack = "/git-receive-pack";
  32. protected static final String gitUploadPack = "/git-upload-pack";
  33. protected static final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD",
  34. "/objects" };
  35. /**
  36. * Extract the repository name from the url.
  37. *
  38. * @param url
  39. * @return repository name
  40. */
  41. public static String getRepositoryName(String value) {
  42. String repository = value;
  43. // get the repository name from the url by finding a known url suffix
  44. for (String urlSuffix : suffixes) {
  45. if (repository.indexOf(urlSuffix) > -1) {
  46. repository = repository.substring(0, repository.indexOf(urlSuffix));
  47. }
  48. }
  49. return repository;
  50. }
  51. /**
  52. * Extract the repository name from the url.
  53. *
  54. * @param url
  55. * @return repository name
  56. */
  57. @Override
  58. protected String extractRepositoryName(String url) {
  59. return GitFilter.getRepositoryName(url);
  60. }
  61. /**
  62. * Analyze the url and returns the action of the request. Return values are
  63. * either "/git-receive-pack" or "/git-upload-pack".
  64. *
  65. * @param serverUrl
  66. * @return action of the request
  67. */
  68. @Override
  69. protected String getUrlRequestAction(String suffix) {
  70. if (!StringUtils.isEmpty(suffix)) {
  71. if (suffix.startsWith(gitReceivePack)) {
  72. return gitReceivePack;
  73. } else if (suffix.startsWith(gitUploadPack)) {
  74. return gitUploadPack;
  75. } else if (suffix.contains("?service=git-receive-pack")) {
  76. return gitReceivePack;
  77. } else if (suffix.contains("?service=git-upload-pack")) {
  78. return gitUploadPack;
  79. } else {
  80. return gitUploadPack;
  81. }
  82. }
  83. return null;
  84. }
  85. /**
  86. * Determine if the repository can receive pushes.
  87. *
  88. * @param repository
  89. * @param action
  90. * @return true if the action may be performed
  91. */
  92. @Override
  93. protected boolean isActionAllowed(RepositoryModel repository, String action) {
  94. if (!StringUtils.isEmpty(action)) {
  95. if (action.equals(gitReceivePack)) {
  96. // Push request
  97. if (!repository.isBare) {
  98. logger.warn("Gitblit does not allow pushes to repositories with a working copy");
  99. return false;
  100. }
  101. }
  102. }
  103. return true;
  104. }
  105. /**
  106. * Determine if the repository requires authentication.
  107. *
  108. * @param repository
  109. * @param action
  110. * @return true if authentication required
  111. */
  112. @Override
  113. protected boolean requiresAuthentication(RepositoryModel repository, String action) {
  114. if (gitUploadPack.equals(action)) {
  115. // send to client
  116. return repository.accessRestriction.atLeast(AccessRestrictionType.CLONE);
  117. } else if (gitReceivePack.equals(action)) {
  118. // receive from client
  119. return repository.accessRestriction.atLeast(AccessRestrictionType.PUSH);
  120. }
  121. return false;
  122. }
  123. /**
  124. * Determine if the user can access the repository and perform the specified
  125. * action.
  126. *
  127. * @param repository
  128. * @param user
  129. * @param action
  130. * @return true if user may execute the action on the repository
  131. */
  132. @Override
  133. protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {
  134. if (!GitBlit.getBoolean(Keys.git.enableGitServlet, true)) {
  135. // Git Servlet disabled
  136. return false;
  137. }
  138. boolean readOnly = repository.isFrozen;
  139. if (readOnly || repository.accessRestriction.atLeast(AccessRestrictionType.PUSH)) {
  140. boolean authorizedUser = user.canAccessRepository(repository);
  141. if (action.equals(gitReceivePack)) {
  142. // Push request
  143. if (!readOnly && authorizedUser) {
  144. // clone-restricted or push-authorized
  145. return true;
  146. } else {
  147. // user is unauthorized to push to this repository
  148. logger.warn(MessageFormat.format("user {0} is not authorized to push to {1}",
  149. user.username, repository));
  150. return false;
  151. }
  152. } else if (action.equals(gitUploadPack)) {
  153. // Clone request
  154. boolean cloneRestricted = repository.accessRestriction
  155. .atLeast(AccessRestrictionType.CLONE);
  156. if (!cloneRestricted || (cloneRestricted && authorizedUser)) {
  157. // push-restricted or clone-authorized
  158. return true;
  159. } else {
  160. // user is unauthorized to clone this repository
  161. logger.warn(MessageFormat.format("user {0} is not authorized to clone {1}",
  162. user.username, repository));
  163. return false;
  164. }
  165. }
  166. }
  167. return true;
  168. }
  169. }