gitHTTP.py
Python script, ASCII text executable
1import uuid 2 3from models import * 4from app import app, gitCommand, getPermissionLevel, getVisibility, db, bcrypt 5import os 6import shutil 7import config 8import flask 9import git 10import subprocess 11from flask_httpauth import HTTPBasicAuth 12import zlib 13import re 14import datetime 15 16auth = HTTPBasicAuth(realm=config.AUTH_REALM) 17 18 19authRequired = flask.Response("Unauthorized Access", 401, {"WWW-Authenticate": 'Basic realm="Login Required"'}) 20 21 22@auth.verify_password 23def verifyPassword(username, password): 24user = User.query.filter_by(username=username).first() 25 26if user and bcrypt.check_password_hash(user.passwordHashed, password): 27flask.g.user = username 28return True 29 30return False 31 32 33@app.route("/<username>/<repository>/git-upload-pack", methods=["POST"]) 34@app.route("/git/<username>/<repository>/git-upload-pack", methods=["POST"]) 35@auth.login_required(optional=True) 36def gitUploadPack(username, repository): 37if auth.current_user() is None and not getVisibility(username, repository): 38return authRequired 39if not (getVisibility(username, repository) or getPermissionLevel(flask.g.user, username, repository) is not None): 40flask.abort(403) 41 42serverRepoLocation = os.path.join(config.REPOS_PATH, username, repository, ".git") 43text = gitCommand(serverRepoLocation, flask.request.data, "upload-pack", "--stateless-rpc", ".") 44 45return flask.Response(text, content_type="application/x-git-upload-pack-result") 46 47 48@app.route("/<username>/<repository>/git-receive-pack", methods=["POST"]) 49@app.route("/git/<username>/<repository>/git-receive-pack", methods=["POST"]) 50@auth.login_required 51def gitReceivePack(username, repository): 52if not getPermissionLevel(flask.g.user, username, repository): 53flask.abort(403) 54 55serverRepoLocation = os.path.join(config.REPOS_PATH, username, repository, ".git") 56text = gitCommand(serverRepoLocation, flask.request.data, "receive-pack", "--stateless-rpc", ".") 57 58sha = flask.request.data.split(b" ", 2)[1].decode() 59info = gitCommand(serverRepoLocation, None, "show", "-s", "--format='%H%n%at%n%cn <%ce>%n%B'", sha).decode() 60 61if re.match("^[0-9a-fA-F]{40}$", info[:40]): 62print(info.split("\n", 3)) 63sha, time, identity, body = info.split("\n", 3) 64login = flask.g.user 65 66if not Commit.query.filter_by(identifier=f"/{username}/{repository}/{sha}").first(): 67user = User.query.filter_by(username=login).first() 68repo = Repo.query.filter_by(route=f"/{username}/{repository}").first() 69 70commit = Commit(sha, user, repo, time, body, identity) 71 72db.session.add(commit) 73db.session.commit() 74 75return flask.Response(text, content_type="application/x-git-receive-pack-result") 76 77 78@app.route("/<username>/<repository>/info/refs", methods=["GET", "POST"]) 79@app.route("/git/<username>/<repository>/info/refs", methods=["GET", "POST"]) 80@auth.login_required(optional=True) 81def gitInfoRefs(username, repository): 82serverRepoLocation = os.path.join(config.REPOS_PATH, username, repository, ".git") 83 84repo = git.Repo(serverRepoLocation) 85repoData = Repo.query.filter_by(route=f"/{username}/{repository}").first() 86if not repoData.defaultBranch: 87if repo.heads: 88repoData.defaultBranch = repo.heads[0].name 89repo.git.checkout("-f", repoData.defaultBranch) 90 91if auth.current_user() is None and (not getVisibility(username, repository) or flask.request.args.get("service") == "git-receive-pack"): 92return authRequired 93try: 94if not (getVisibility(username, repository) or getPermissionLevel(flask.g.user, username, repository) is not None): 95flask.abort(403) 96except AttributeError: 97return authRequired 98 99service = flask.request.args.get("service") 100 101if service.startswith("git"): 102service = service[4:] 103else: 104flask.abort(403) 105 106if service == "receive-pack": 107try: 108if not getPermissionLevel(flask.g.user, username, repository): 109flask.abort(403) 110except AttributeError: 111return authRequired 112 113serviceLine = f"# service=git-{service}\n" 114serviceLine = (f"{len(serviceLine) + 4:04x}" + serviceLine).encode() 115 116if service == "upload-pack": 117text = serviceLine + b"0000" + gitCommand(serverRepoLocation, None, "upload-pack", "--stateless-rpc", "--advertise-refs", "--http-backend-info-refs", ".") 118elif service == "receive-pack": 119refs = gitCommand(serverRepoLocation, None, "receive-pack", "--http-backend-info-refs", ".") 120text = serviceLine + b"0000" + refs 121else: 122flask.abort(403) 123 124response = flask.Response(text, content_type=f"application/x-git-{service}-advertisement") 125response.headers["Cache-Control"] = "no-cache" 126 127return response 128 129