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

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