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

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