AsIsFileService.java

  1. /*
  2.  * Copyright (C) 2009-2010, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.http.server.resolver;

  11. import javax.servlet.http.HttpServletRequest;

  12. import org.eclipse.jgit.lib.Config;
  13. import org.eclipse.jgit.lib.Repository;
  14. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  15. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;

  16. /**
  17.  * Controls access to bare files in a repository.
  18.  * <p>
  19.  * Older HTTP clients which do not speak the smart HTTP variant of the Git
  20.  * protocol fetch from a repository by directly getting its objects and pack
  21.  * files. This class, along with the {@code http.getanyfile} per-repository
  22.  * configuration setting, can be used by
  23.  * {@link org.eclipse.jgit.http.server.GitServlet} to control whether or not
  24.  * these older clients are permitted to read these direct files.
  25.  */
  26. public class AsIsFileService {
  27.     /** Always throws {@link ServiceNotEnabledException}. */
  28.     public static final AsIsFileService DISABLED = new AsIsFileService() {
  29.         @Override
  30.         public void access(HttpServletRequest req, Repository db)
  31.                 throws ServiceNotEnabledException {
  32.             throw new ServiceNotEnabledException();
  33.         }
  34.     };

  35.     private static class ServiceConfig {
  36.         final boolean enabled;

  37.         ServiceConfig(Config cfg) {
  38.             enabled = cfg.getBoolean("http", "getanyfile", true);
  39.         }
  40.     }

  41.     /**
  42.      * Determine if {@code http.getanyfile} is enabled in the configuration.
  43.      *
  44.      * @param db
  45.      *            the repository to check.
  46.      * @return {@code false} if {@code http.getanyfile} was explicitly set to
  47.      *         {@code false} in the repository's configuration file; otherwise
  48.      *         {@code true}.
  49.      */
  50.     protected static boolean isEnabled(Repository db) {
  51.         return db.getConfig().get(ServiceConfig::new).enabled;
  52.     }

  53.     /**
  54.      * Determine if access to any bare file of the repository is allowed.
  55.      * <p>
  56.      * This method silently succeeds if the request is allowed, or fails by
  57.      * throwing a checked exception if access should be denied.
  58.      * <p>
  59.      * The default implementation of this method checks {@code http.getanyfile},
  60.      * throwing
  61.      * {@link org.eclipse.jgit.transport.resolver.ServiceNotEnabledException} if
  62.      * it was explicitly set to {@code false}, and otherwise succeeding
  63.      * silently.
  64.      *
  65.      * @param req
  66.      *            current HTTP request, in case information from the request may
  67.      *            help determine the access request.
  68.      * @param db
  69.      *            the repository the request would obtain a bare file from.
  70.      * @throws ServiceNotEnabledException
  71.      *             bare file access is not allowed on the target repository, by
  72.      *             any user, for any reason.
  73.      * @throws ServiceNotAuthorizedException
  74.      *             bare file access is not allowed for this HTTP request and
  75.      *             repository, such as due to a permission error.
  76.      */
  77.     public void access(HttpServletRequest req, Repository db)
  78.             throws ServiceNotEnabledException, ServiceNotAuthorizedException {
  79.         if (!isEnabled(db))
  80.             throw new ServiceNotEnabledException();
  81.     }
  82. }