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 8.3KB

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