Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

GitFilter.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.Constants.AuthorizationControl;
  20. import com.gitblit.models.RepositoryModel;
  21. import com.gitblit.models.UserModel;
  22. import com.gitblit.utils.StringUtils;
  23. /**
  24. * The GitFilter is an AccessRestrictionFilter which ensures that Git client
  25. * requests for push, clone, or view restricted repositories are authenticated
  26. * and authorized.
  27. *
  28. * @author James Moger
  29. *
  30. */
  31. public class GitFilter extends AccessRestrictionFilter {
  32. protected static final String gitReceivePack = "/git-receive-pack";
  33. protected static final String gitUploadPack = "/git-upload-pack";
  34. protected static final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD",
  35. "/objects" };
  36. /**
  37. * Extract the repository name from the url.
  38. *
  39. * @param url
  40. * @return repository name
  41. */
  42. public static String getRepositoryName(String value) {
  43. String repository = value;
  44. // get the repository name from the url by finding a known url suffix
  45. for (String urlSuffix : suffixes) {
  46. if (repository.indexOf(urlSuffix) > -1) {
  47. repository = repository.substring(0, repository.indexOf(urlSuffix));
  48. }
  49. }
  50. return repository;
  51. }
  52. /**
  53. * Extract the repository name from the url.
  54. *
  55. * @param url
  56. * @return repository name
  57. */
  58. @Override
  59. protected String extractRepositoryName(String url) {
  60. return GitFilter.getRepositoryName(url);
  61. }
  62. /**
  63. * Analyze the url and returns the action of the request. Return values are
  64. * either "/git-receive-pack" or "/git-upload-pack".
  65. *
  66. * @param serverUrl
  67. * @return action of the request
  68. */
  69. @Override
  70. protected String getUrlRequestAction(String suffix) {
  71. if (!StringUtils.isEmpty(suffix)) {
  72. if (suffix.startsWith(gitReceivePack)) {
  73. return gitReceivePack;
  74. } else if (suffix.startsWith(gitUploadPack)) {
  75. return gitUploadPack;
  76. } else if (suffix.contains("?service=git-receive-pack")) {
  77. return gitReceivePack;
  78. } else if (suffix.contains("?service=git-upload-pack")) {
  79. return gitUploadPack;
  80. } else {
  81. return gitUploadPack;
  82. }
  83. }
  84. return null;
  85. }
  86. /**
  87. * Determine if a non-existing repository can be created using this filter.
  88. *
  89. * @return true if the server allows repository creation on-push
  90. */
  91. @Override
  92. protected boolean isCreationAllowed() {
  93. return GitBlit.getBoolean(Keys.git.allowCreateOnPush, true);
  94. }
  95. /**
  96. * Determine if the repository can receive pushes.
  97. *
  98. * @param repository
  99. * @param action
  100. * @return true if the action may be performed
  101. */
  102. @Override
  103. protected boolean isActionAllowed(RepositoryModel repository, String action) {
  104. // the log here has been moved into ReceiveHook to provide clients with
  105. // error messages
  106. return true;
  107. }
  108. @Override
  109. protected boolean requiresClientCertificate() {
  110. return GitBlit.getBoolean(Keys.git.requiresClientCertificate, false);
  111. }
  112. /**
  113. * Determine if the repository requires authentication.
  114. *
  115. * @param repository
  116. * @param action
  117. * @return true if authentication required
  118. */
  119. @Override
  120. protected boolean requiresAuthentication(RepositoryModel repository, String action) {
  121. if (gitUploadPack.equals(action)) {
  122. // send to client
  123. return repository.accessRestriction.atLeast(AccessRestrictionType.CLONE);
  124. } else if (gitReceivePack.equals(action)) {
  125. // receive from client
  126. return repository.accessRestriction.atLeast(AccessRestrictionType.PUSH);
  127. }
  128. return false;
  129. }
  130. /**
  131. * Determine if the user can access the repository and perform the specified
  132. * action.
  133. *
  134. * @param repository
  135. * @param user
  136. * @param action
  137. * @return true if user may execute the action on the repository
  138. */
  139. @Override
  140. protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {
  141. if (!GitBlit.getBoolean(Keys.git.enableGitServlet, true)) {
  142. // Git Servlet disabled
  143. return false;
  144. }
  145. if (action.equals(gitReceivePack)) {
  146. // Push request
  147. if (user.canPush(repository)) {
  148. return true;
  149. } else {
  150. // user is unauthorized to push to this repository
  151. logger.warn(MessageFormat.format("user {0} is not authorized to push to {1}",
  152. user.username, repository));
  153. return false;
  154. }
  155. } else if (action.equals(gitUploadPack)) {
  156. // Clone request
  157. if (user.canClone(repository)) {
  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. return true;
  167. }
  168. /**
  169. * An authenticated user with the CREATE role can create a repository on
  170. * push.
  171. *
  172. * @param user
  173. * @param repository
  174. * @param action
  175. * @return the repository model, if it is created, null otherwise
  176. */
  177. @Override
  178. protected RepositoryModel createRepository(UserModel user, String repository, String action) {
  179. boolean isPush = !StringUtils.isEmpty(action) && gitReceivePack.equals(action);
  180. if (isPush) {
  181. if (user.canCreate(repository)) {
  182. // user is pushing to a new repository
  183. // validate name
  184. if (repository.startsWith("../")) {
  185. logger.error(MessageFormat.format("Illegal relative path in repository name! {0}", repository));
  186. return null;
  187. }
  188. if (repository.contains("/../")) {
  189. logger.error(MessageFormat.format("Illegal relative path in repository name! {0}", repository));
  190. return null;
  191. }
  192. // confirm valid characters in repository name
  193. Character c = StringUtils.findInvalidCharacter(repository);
  194. if (c != null) {
  195. logger.error(MessageFormat.format("Invalid character '{0}' in repository name {1}!", c, repository));
  196. return null;
  197. }
  198. // create repository
  199. RepositoryModel model = new RepositoryModel();
  200. model.name = repository;
  201. model.addOwner(user.username);
  202. model.projectPath = StringUtils.getFirstPathElement(repository);
  203. if (model.isUsersPersonalRepository(user.username)) {
  204. // personal repository, default to private for user
  205. model.authorizationControl = AuthorizationControl.NAMED;
  206. model.accessRestriction = AccessRestrictionType.VIEW;
  207. } else {
  208. // common repository, user default server settings
  209. model.authorizationControl = AuthorizationControl.fromName(GitBlit.getString(Keys.git.defaultAuthorizationControl, ""));
  210. model.accessRestriction = AccessRestrictionType.fromName(GitBlit.getString(Keys.git.defaultAccessRestriction, ""));
  211. }
  212. // create the repository
  213. try {
  214. GitBlit.self().updateRepositoryModel(model.name, model, true);
  215. logger.info(MessageFormat.format("{0} created {1} ON-PUSH", user.username, model.name));
  216. return GitBlit.self().getRepositoryModel(model.name);
  217. } catch (GitBlitException e) {
  218. logger.error(MessageFormat.format("{0} failed to create repository {1} ON-PUSH!", user.username, model.name), e);
  219. }
  220. } else {
  221. logger.warn(MessageFormat.format("{0} is not permitted to create repository {1} ON-PUSH!", user.username, repository));
  222. }
  223. }
  224. // repository could not be created or action was not a push
  225. return null;
  226. }
  227. }