def submit_POST(): valid = Validation(request) _manifest = valid.require("manifest", friendly_name="Manifest") max_len = Job.manifest.prop.columns[0].type.length note = valid.optional("note", default="Submitted on the web") valid.expect(not _manifest or len(_manifest) < max_len, "Manifest must be less than {} bytes".format(max_len), field="manifest") if not valid.ok: return render_template("submit.html", **valid.kwargs) try: manifest = Manifest(yaml.safe_load(_manifest)) except Exception as ex: valid.error(str(ex), field="manifest") return render_template("submit.html", **valid.kwargs) job = Job(current_user, _manifest) job.note = note db.session.add(job) db.session.flush() for task in manifest.tasks: t = Task(job, task.name) db.session.add(t) db.session.flush() # assigns IDs for ordering purposes queue_build(job, manifest) # commits the session return redirect("/~" + current_user.username + "/job/" + str(job.id))
def forgot_POST(): valid = Validation(request) email = valid.require("email", friendly_name="Email") if not valid.ok: return render_template("forgot.html", **valid.kwargs) user = User.query.filter(User.email == email).first() valid.expect(user, "No account found with this email address.") if not valid.ok: return render_template("forgot.html", **valid.kwargs) factors = (UserAuthFactor.query.filter( UserAuthFactor.user_id == user.id)).all() valid.expect( not any(f for f in factors if f.factor_type in [FactorType.totp, FactorType.u2f]), "This account has two-factor authentication enabled, contact support.") if not valid.ok: return render_template("forgot.html", **valid.kwargs) rh = user.gen_reset_hash() db.session.commit() send_email( 'reset_pw', user.email, 'Reset your password on {}'.format(site_name), headers={ "From": f"{cfg('sr.ht', 'owner-name')} <*****@*****.**>", "To": "{} <{}>".format(user.username, user.email), "Reply-To": f"{cfg('sr.ht', 'owner-name')} <{cfg('sr.ht', 'owner-email')}>", }, user=user) audit_log("password reset requested", user=user) return render_template("forgot.html", done=True)
def ticket_edit_POST(owner, name, ticket_id): tracker, _ = get_tracker(owner, name) if not tracker: abort(404) ticket, access = get_ticket(tracker, ticket_id) if not ticket: abort(404) if not TicketAccess.edit in access: abort(401) valid = Validation(request) title = valid.require("title", friendly_name="Title") desc = valid.optional("description") valid.expect(not title or 3 <= len(title) <= 2048, "Title must be between 3 and 2048 characters.", field="title") valid.expect(not desc or len(desc) < 16384, "Description must be no more than 16384 characters.", field="description") if not valid.ok: return render_template("edit_ticket.html", tracker=tracker, ticket=ticket, **valid.kwargs) ticket.title = title ticket.description = desc db.session.commit() return redirect(ticket_url(ticket))
def ssh_keys_POST(): user = User.query.get(current_user.id) valid = Validation(request) ssh_key = valid.require("ssh-key") if valid.ok: try: parsed_key = ssh.SSHKey(ssh_key) valid.expect(parsed_key.bits, "This is not a valid SSH key", "ssh-key") except: valid.error("This is not a valid SSH key", "ssh-key") if valid.ok: fingerprint = parsed_key.hash_md5()[4:] valid.expect(SSHKey.query\ .filter(SSHKey.fingerprint == fingerprint) \ .count() == 0, "We already have this SSH key on file.", "ssh-key") if not valid.ok: return render_template("keys.html", current_user=user, ssh_key=ssh_key, valid=valid) key = SSHKey(user, ssh_key, fingerprint, parsed_key.comment) db.session.add(key) audit_log("ssh key added", 'Added SSH key {}'.format(fingerprint)) db.session.commit() return redirect("/keys")
def oauth_register_POST(): valid = Validation(request) client_name = valid.require("client-name") redirect_uri = valid.require("redirect-uri") valid.expect(not redirect_uri or valid_url(redirect_uri), "Must be a valid HTTP or HTTPS URI", field="redirect-uri") if not valid.ok: return render_template("oauth-register.html", client_name=client_name, redirect_uri=redirect_uri, valid=valid) client = OAuthClient(current_user, client_name, redirect_uri) secret = client.gen_client_secret() session["client_id"] = client.client_id session["client_secret"] = secret session["client_event"] = "registered" db.session.add(client) audit_log("register oauth client", "Registered OAuth client {}".format(client.client_id)) db.session.commit() return redirect("/oauth/registered")
def jobs_POST(): valid = Validation(request) _manifest = valid.require("manifest", cls=str) max_len = Job.manifest.prop.columns[0].type.length valid.expect(not _manifest or len(_manifest) < max_len, "Manifest must be less than {} bytes".format(max_len), field="manifest") note = valid.optional("note", cls=str) read = valid.optional("access:read", ["*"], list) write = valid.optional("access:write", [current_token.user.username], list) secrets = valid.optional("secrets", cls=bool, default=True) tags = valid.optional("tags", [], list) valid.expect( all(re.match(r"^[A-Za-z0-9_.-]+$", tag) for tag in tags), "Invalid tag name, tags must use lowercase alphanumeric characters, underscores, dashes, or dots", field="tags") triggers = valid.optional("triggers", list(), list) execute = valid.optional("execute", True, bool) if not valid.ok: return valid.response try: manifest = Manifest(yaml.safe_load(_manifest)) except Exception as ex: valid.error(str(ex)) return valid.response # TODO: access controls job = Job(current_token.user, _manifest) job.note = note if tags: job.tags = "/".join(tags) job.secrets = secrets db.session.add(job) db.session.flush() for task in manifest.tasks: t = Task(job, task.name) db.session.add(t) db.session.flush() # assigns IDs for ordering purposes for index, trigger in enumerate(triggers): _valid = Validation(trigger) action = _valid.require("action", TriggerType) condition = _valid.require("condition", TriggerCondition) if not _valid.ok: _valid.copy(valid, "triggers[{}]".format(index)) return valid.response # TODO: Validate details based on trigger type t = Trigger(job) t.trigger_type = action t.condition = condition t.details = json.dumps(trigger) db.session.add(t) if execute: queue_build(job, manifest) # commits the session else: db.session.commit() return {"id": job.id}
def billing_initial_POST(): valid = Validation(request) amount = valid.require("amount") amount = int(amount) plan = valid.require("plan") valid.expect(not amount or amount > 0, "Expected amount >0") if not valid.ok: return "Invalid form submission", 400 current_user.payment_cents = amount db.session.commit() if current_user.stripe_customer: return redirect(url_for("billing.billing_GET")) return redirect(url_for("billing.new_payment_GET"))
def totp_challenge_POST(): user_id = session.get('authorized_user') factors = session.get('extra_factors') return_to = session.get('return_to') or '/' if not user_id or not factors: return redirect("/login") valid = Validation(request) code = valid.require('code') if not valid.ok: return render_template("totp-challenge.html", return_to=return_to, valid=valid) code = code.replace(" ", "") try: code = int(code) except: valid.error("This TOTP code is invalid (expected a number)", field="code") if not valid.ok: return render_template("totp-challenge.html", return_to=return_to, valid=valid) factor = UserAuthFactor.query.get(factors[0]) secret = factor.secret.decode('utf-8') valid.expect(totp(secret, code), 'The code you entered is incorrect.', field='code') if not valid.ok: return render_template("totp-challenge.html", valid=valid, return_to=return_to) factors = factors[1:] if len(factors) != 0: return get_challenge(factors[0]) del session['authorized_user'] del session['extra_factors'] del session['return_to'] user = User.query.get(user_id) login_user(user, remember=True) audit_log("logged in") db.session.commit() metrics.meta_logins_success.inc() return redirect(return_to)
def create_POST(): valid = Validation(request) name = valid.require("tracker_name", friendly_name="Name") desc = valid.optional("tracker_desc") if not valid.ok: return render_template("tracker-create.html", **valid.kwargs), 400 valid.expect(2 < len(name) < 256, "Must be between 2 and 256 characters", field="tracker_name") valid.expect(not valid.ok or name[0] in string.ascii_lowercase, "Must begin with a lowercase letter", field="tracker_name") valid.expect(not valid.ok or name_re.match(name), "Only lowercase alphanumeric characters or -.", field="tracker_name") valid.expect(not desc or len(desc) < 4096, "Must be less than 4096 characters", field="tracker_desc") if not valid.ok: return render_template("tracker-create.html", **valid.kwargs), 400 tracker = (Tracker.query.filter( Tracker.owner_id == current_user.id).filter( Tracker.name == name)).first() valid.expect(not tracker, "A tracker by this name already exists", field="tracker_name") if not valid.ok: return render_template("tracker-create.html", **valid.kwargs), 400 tracker = Tracker() tracker.owner_id = current_user.id tracker.name = name tracker.description = desc db.session.add(tracker) db.session.flush() sub = TicketSubscription() sub.tracker_id = tracker.id sub.user_id = current_user.id db.session.add(sub) db.session.commit() if "create-configure" in valid: return redirect( url_for(".settings_details_GET", owner=current_user.username, name=name)) return redirect(tracker_url(tracker))
def authorize(): valid = Validation(request) client_id = request.headers.get("X-OAuth-ID") client_secret = request.headers.get("X-OAuth-Secret") valid.expect(client_id and client_secret, "Required X-OAuth-ID and X-OAuth-Secret headers missing") if not valid.ok: return valid.response client = (OAuthClient.query.filter( OAuthClient.client_id == client_id).one_or_none()) valid.expect(client is not None, "Unknown X-OAuth-ID") if not valid.ok: return valid.response client_secret_hash = hashlib.sha512(client_secret.encode()).hexdigest() valid.expect(client_secret_hash == client.client_secret_hash, "Incorrect X-OAuth-Secret") if not valid.ok: return valid.response valid.expect( client.preauthorized, "This feature is only available to first-party OAuth clients") if not valid.ok: return valid.response g.oauth_client = client
def ticket_add_label(owner, name, ticket_id): tracker, _ = get_tracker(owner, name) if not tracker: abort(404) ticket, access = get_ticket(tracker, ticket_id) if not ticket: abort(404) if not TicketAccess.edit in access: abort(401) valid = Validation(request) label_id = valid.require("label_id", friendly_name="A label") if not valid.ok: ctx = get_ticket_context(ticket, tracker, access) return render_template("ticket.html", **ctx, **valid.kwargs) valid.expect(re.match(r"^\d+$", label_id), "Label ID must be numeric", field="label_id") if not valid.ok: ctx = get_ticket_context(ticket, tracker, access) return render_template("ticket.html", **ctx, **valid.kwargs) label_id = int(request.form.get('label_id')) label = Label.query.filter(Label.id == label_id).first() if not label: abort(404) ticket_label = (TicketLabel.query.filter( TicketLabel.label_id == label.id).filter( TicketLabel.ticket_id == ticket.id)).first() if not ticket_label: ticket_label = TicketLabel() ticket_label.ticket_id = ticket.id ticket_label.label_id = label.id ticket_label.user_id = current_user.id event = Event() event.event_type = EventType.label_added event.user_id = current_user.id event.ticket_id = ticket.id event.label_id = label.id db.session.add(ticket_label) db.session.add(event) db.session.commit() return redirect(ticket_url(ticket))
def oauth_exchange_POST(): valid = Validation(request) client_id = valid.require('client_id') client_secret = valid.require('client_secret') exchange = valid.require('exchange') if not valid.ok: return valid.response client = (OAuthClient.query.filter( OAuthClient.client_id == client_id)).one_or_none() valid.expect(client, 'Unknown client ID') if not valid.ok: return valid.response client_secret_hash = hashlib.sha512(client_secret.encode()).hexdigest() valid.expect(client_secret_hash == client.client_secret_hash, 'Invalid client secret') if not valid.ok: return valid.response stash = redis.get(exchange) valid.expect(stash, 'Exchange token expired') if not valid.ok: return valid.response stash = json.loads(stash.decode()) redis.delete(exchange) user = stash.get('user_id') scopes = stash.get('scopes') user = User.query.filter(User.id == user).first() valid.expect( user, "Unknown user ID stored for " "this exchange token (this isn't supposed to happen") if not valid.ok: return valid.response previous = (OAuthToken.query.filter(OAuthToken.user_id == user.id).filter( OAuthToken.client_id == client.id)).one_or_none() if not previous: oauth_token = OAuthToken(user, client) else: oauth_token = previous previous.expires = datetime.utcnow() + timedelta(days=365) oauth_token.scopes = [OAuthScope(s) for s in scopes.split(",")] token = oauth_token.gen_token() if not client.preauthorized: audit_log("oauth token issued", "issued oauth token {} to client {}".format( oauth_token.token_partial, client.client_id), user=user) if not previous: db.session.add(oauth_token) db.session.commit() return {"token": token, "expires": oauth_token.expires}
def ticket_comment_POST(owner, name, ticket_id): tracker, access = get_tracker(owner, name) if not tracker: abort(404) ticket, access = get_ticket(tracker, ticket_id) if not ticket: abort(404) valid = Validation(request) text = valid.optional("comment") resolve = valid.optional("resolve") resolution = valid.optional("resolution") reopen = valid.optional("reopen") valid.expect(not text or 3 <= len(text) <= 16384, "Comment must be between 3 and 16384 characters.", field="comment") valid.expect(text or resolve or reopen, "Comment is required", field="comment") if (resolve or reopen) and TicketAccess.edit not in access: abort(403) if resolve: try: resolution = TicketResolution(int(resolution)) except Exception as ex: abort(400, "Invalid resolution") else: resolution = None if not valid.ok: ctx = get_ticket_context(ticket, tracker, access) return render_template("ticket.html", **ctx, **valid.kwargs) comment = add_comment(current_user, ticket, text=text, resolve=resolve, resolution=resolution, reopen=reopen) return redirect(ticket_url(ticket, comment))
def settings_POST(owner_name, list_name): owner, ml, access = get_list(owner_name, list_name) if not ml: abort(404) if ml.owner_id != current_user.id: abort(401) valid = Validation(request) list_desc = valid.optional("list_desc") if list_desc == "": list_desc = None valid.expect(not list_desc or 16 < len(list_desc) < 2048, "Description must be between 16 and 2048 characters.", field="list_desc") if not valid.ok: return render_template("list-settings.html", list=ml, owner=owner, access_type_list=ListAccess, access_help_map=access_help_map, **valid.kwargs) def process(perm): bitfield = ListAccess.none for access in ListAccess: if access in [ListAccess.none]: continue if valid.optional("perm_{}_{}".format(perm, access.name)) != None: bitfield |= access return bitfield ml.description = list_desc ml.nonsubscriber_permissions = process("nonsub") ml.subscriber_permissions = process("sub") ml.account_permissions = process("account") db.session.commit() return redirect( url_for("archives.archive", owner_name=owner_name, list_name=list_name))
def settings_access_POST(owner_name, repo_name): owner, repo = check_access(owner_name, repo_name, UserAccess.manage) if isinstance(repo, BaseRedirectMixin): repo = repo.new_repo valid = Validation(request) user = valid.require("user", friendly_name="User") mode = valid.optional("access", cls=AccessMode, default=AccessMode.ro) if not valid.ok: return render_template("settings_access.html", owner=owner, repo=repo, **valid.kwargs) # TODO: Group access if user[0] == "~": user = user[1:] User = current_app.User user = User.query.filter(User.username == user).first() valid.expect( user, "I don't know this user. Have they logged into this service before?", field="user") valid.expect( not user or user.id != current_user.id, "You can't adjust your own access controls. You always have full read/write access.", field="user") if not valid.ok: return render_template("settings_access.html", owner=owner, repo=repo, **valid.kwargs) Access = current_app.Access grant = (Access.query.filter(Access.repo_id == repo.id, Access.user_id == user.id)).first() if not grant: grant = Access() grant.repo_id = repo.id grant.user_id = user.id db.session.add(grant) grant.mode = mode db.session.commit() return redirect("/{}/{}/settings/access".format(owner.canonical_name, repo.name))
def settings_details_POST(owner, name): tracker, access = get_tracker(owner, name) if not tracker: abort(404) if current_user.id != tracker.owner_id: abort(403) valid = Validation(request) desc = valid.optional("tracker_desc", default=tracker.description) valid.expect(not desc or len(desc) < 4096, "Must be less than 4096 characters", field="tracker_desc") if not valid.ok: return render_template("tracker-details.html", tracker=tracker, **valid.kwargs), 400 tracker.description = desc db.session.commit() return redirect(tracker_url(tracker))
def reset_POST(token): user = User.query.filter(User.reset_hash == token).first() if not user: abort(404) if user.reset_expiry < datetime.utcnow(): abort(404) valid = Validation(request) password = valid.require("password", friendly_name="Password") if not valid.ok: return render_template("reset.html", valid=valid) valid.expect(8 <= len(password) <= 512, "Password must be between 8 and 512 characters.", "password") if not valid.ok: return render_template("reset.html", valid=valid) user.password = bcrypt.hashpw(password.encode('utf-8'), salt=bcrypt.gensalt()).decode('utf-8') audit_log("password reset", user=user) db.session.commit() login_user(user, remember=True) metrics.meta_pw_resets.inc() return redirect("/")
def privacy_POST(): valid = Validation(request) key_id = valid.require("pgp-key") key_id = key_id if key_id != "null" else None key = None if key_id: key = PGPKey.query.get(int(key_id)) valid.expect(key.user_id == current_user.id, "Invalid PGP key") if not valid.ok: return redirect("/privacy") user = User.query.get(current_user.id) user.pgp_key = key audit_log("changed pgp key", "Set default PGP key to {}".format(key.key_id if key else None)) db.session.commit() return redirect("/privacy")
def login_POST(): if current_user: return redirect("/") valid = Validation(request) username = valid.require("username", friendly_name="Username") password = valid.require("password", friendly_name="Password") return_to = valid.optional("return_to", "/") if not valid.ok: return render_template("login.html", valid=valid), 400 user = User.query.filter(User.username == username.lower()).one_or_none() valid.expect(user is not None, "Username or password incorrect") if valid.ok: valid.expect( bcrypt.checkpw(password.encode('utf-8'), user.password.encode('utf-8')), "Username or password incorrect") if not valid.ok: metrics.meta_logins_failed.inc() return render_template("login.html", username=username, valid=valid) factors = UserAuthFactor.query \ .filter(UserAuthFactor.user_id == user.id).all() if any(factors): session['extra_factors'] = [f.id for f in factors] session['authorized_user'] = user.id session['return_to'] = return_to return get_challenge(factors[0]) login_user(user, remember=True) audit_log("logged in") db.session.commit() metrics.meta_logins_success.inc() return redirect(return_to)
def security_totp_enable_POST(): valid = Validation(request) secret = valid.require("secret") code = valid.require("code") if not valid.ok: return render_template("totp-enable.html", qrcode=totp_get_qrcode(secret), otpauth_uri=otpauth_uri(secret), secret=secret, valid=valid), 400 code = code.replace(" ", "") try: code = int(code) except: valid.error( "This TOTP code is invalid (expected a number)", field="code") if not valid.ok: return render_template("totp-enable.html", qrcode=totp_get_qrcode(secret), otpauth_uri=otpauth_uri(secret), secret=secret, valid=valid), 400 valid.expect(totp(secret, code), "The code you entered is incorrect.", field="code") if not valid.ok: return render_template("totp-enable.html", qrcode=totp_get_qrcode(secret), otpauth_uri=otpauth_uri(secret), secret=secret, valid=valid), 400 factor = UserAuthFactor(current_user, FactorType.totp) factor.secret = secret.encode('utf-8') db.session.add(factor) audit_log("enabled two factor auth", 'Enabled TOTP') db.session.commit() metrics.meta_totp_enabled.inc() return redirect("/security")
def tracker_labels_POST(owner, name): tracker, access = get_tracker(owner, name) is_owner = current_user.id == tracker.owner_id if not tracker: abort(404) if not is_owner: abort(403) valid = Validation(request) label_name = valid.require("name") label_color = valid.require("color") if not valid.ok: return render_template("tracker-labels.html", tracker=tracker, access=access, is_owner=is_owner, **valid.kwargs), 400 valid.expect(2 < len(label_name) < 50, "Must be between 2 and 50 characters", field="name") valid.expect(color.valid_hex_color_code(label_color), "Invalid hex color code", field="color") if not valid.ok: return render_template("tracker-labels.html", tracker=tracker, access=access, is_owner=is_owner, **valid.kwargs), 400 existing_label = (Label.query.filter( Label.tracker_id == tracker.id).filter( Label.name == label_name)).first() valid.expect(not existing_label, "A label with this name already exists", field="name") if not valid.ok: return render_template("tracker-labels.html", tracker=tracker, access=access, is_owner=is_owner, **valid.kwargs), 400 # Determine a foreground color to use label_color_rgb = color.color_from_hex(label_color) text_color_rgb = color.get_text_color(label_color_rgb) text_color = color.color_to_hex(text_color_rgb) label = Label() label.tracker_id = tracker.id label.name = label_name label.color = label_color label.text_color = text_color db.session.add(label) db.session.commit() return redirect(url_for(".tracker_labels_GET", owner=owner, name=name))
def delegate_scopes_POST(): valid = Validation(request) desc = valid.require("description", cls=str) name = valid.require("name", cls=str) writable = valid.require("writable", cls=bool) valid.expect(not name or re.match(r"^[a-z_]+$", name), "Lowercase characters and underscores only", field="name") if not valid.ok: return valid.response scope = (DelegatedScope.query\ .filter(DelegatedScope.client_id == g.oauth_client.id) .filter(DelegatedScope.name == name)).one_or_none() valid.expect(scope is None, "A scope with this name already exists.", field="name") if not valid.ok: return valid.response scope = DelegatedScope(g.oauth_client, name, desc) scope.write = writable db.session.add(scope) db.session.commit() return scope.to_dict()
def oauth_token_POST(token): valid = Validation(request) client_id = valid.require("client_id") client_secret = valid.require("client_secret") revocation_url = valid.require("revocation_url") if not valid.ok: return valid.response client = (OAuthClient.query.filter( OAuthClient.client_id == client_id)).one_or_none() if not client: return {"errors": [{"reason": "404 not found"}]}, 404 client_secret_hash = hashlib.sha512(client_secret.encode()).hexdigest() valid.expect(client_secret_hash == client.client_secret_hash, "Invalid client secret") if not valid.ok: return valid.response h = hashlib.sha512(token.encode()).hexdigest() oauth_token = (OAuthToken.query.filter( OAuthToken.token_hash == h)).one_or_none() valid.expect( oauth_token is not None and oauth_token.expires > datetime.utcnow(), "Invalid or expired OAuth token") if not valid.ok: return valid.response rev = RevocationUrl.query\ .filter(RevocationUrl.token_id == oauth_token.id)\ .filter(RevocationUrl.client_id == client.id).first() if not rev: rev = RevocationUrl(client, oauth_token, revocation_url) db.session.add(rev) else: rev.url = revocation_url db.session.commit() if oauth_token._scopes == "*": return {"expires": oauth_token.expires, "scopes": "*"} scopes = [ str(s) for s in oauth_token.scopes if (s.client_id and s.client_id == client.client_id) or s == OAuthScope("profile:read") ] valid.expect(any(scopes), "Invalid or expired OAuth token") if not valid.ok: return valid.response scopes = ",".join(scopes) # TODO: Celery task to notify of revocation return { "expires": oauth_token.expires, "scopes": ",".join(str(s) for s in oauth_token.scopes) }
def pgp_keys_POST(): user = User.query.get(current_user.id) valid = Validation(request) pgp_key = valid.require("pgp-key") valid.expect( not pgp_key or len(pgp_key) < 32768, Markup("Maximum encoded key length is 32768 bytes. " "Try <br /><code>gpg --armor --export-options export-minimal " "--export <fingerprint></code><br /> to export a " "smaller key."), field="pgp-key") if valid.ok: try: key = pgpy.PGPKey() key.parse(pgp_key.replace('\r', '').encode('utf-8')) except: valid.error("This is not a valid PGP key", field="pgp-key") valid.expect(any(key.userids), "This key has no user IDs", field="pgp-key") try: prepare_email("test", user.email, "test", encrypt_key=pgp_key) except: valid.error( "We were unable to encrypt a test message with this key", field="pgp-key") if valid.ok: valid.expect(PGPKey.query\ .filter(PGPKey.user_id == user.id) \ .filter(PGPKey.key_id == key.fingerprint)\ .count() == 0, "This is a duplicate key", field="pgp-key") if not valid.ok: return render_template("keys.html", current_user=user, pgp_key=pgp_key, valid=valid) pgp = PGPKey(user, pgp_key, key.fingerprint, key.userids[0].email) db.session.add(pgp) audit_log("pgp key added", 'Added PGP key {}'.format(key.fingerprint)) db.session.commit() return redirect("/keys")
def create_list_POST(): valid = Validation(request) list_name = valid.require("list_name", friendly_name="Name") list_desc = valid.optional("list_desc") if not valid.ok: return render_template("create.html", **valid.kwargs) valid.expect(re.match(r'^[a-z._-][a-z0-9._-]*$', list_name), "Name must match [a-z._-][a-z0-9._-]*", field="list_name") existing = (List.query.filter(List.owner_id == current_user.id).filter( List.name.ilike(list_name)).first()) valid.expect(not existing, "This name is already in use.", field="list_name") valid.expect(not list_desc or 16 < len(list_desc) < 2048, "Description must be between 16 and 2048 characters.", field="list_desc") if not valid.ok: return render_template("create.html", **valid.kwargs) ml = List() ml.owner_id = current_user.id ml.name = list_name ml.description = list_desc db.session.add(ml) db.session.commit() # Auto-subscribe the owner sub = Subscription() sub.user_id = current_user.id sub.list_id = ml.id sub.confirmed = True db.session.add(sub) db.session.commit() return redirect( url_for("archives.archive", owner_name=current_user.canonical_name, list_name=ml.name))
def register_POST(): valid = Validation(request) is_open = cfg("meta.sr.ht::settings", "registration") == "yes" username = valid.require("username", friendly_name="Username") email = valid.require("email", friendly_name="Email address") password = valid.require("password", friendly_name="Password") invite_hash = valid.optional("invite_hash") invite = None if not valid.ok: return render_template("register.html", is_open=(is_open or invite_hash is not None), **valid.kwargs), 400 if not is_open: if not invite_hash: abort(401) else: invite = (Invite.query.filter( Invite.invite_hash == invite_hash).filter( Invite.recipient_id == None)).one_or_none() if not invite: abort(401) email = email.strip() user = User.query.filter(User.username == username).first() valid.expect(user is None, "This username is already in use.", "username") user = User.query.filter(User.email == email).first() valid.expect(user is None, "This email address is already in use.", "email") valid.expect(2 <= len(username) <= 30, "Username must contain between 2 and 30 characters.", "username") valid.expect(re.match("^[a-z_]", username), "Username must start with a lowercase letter or underscore.", "username") valid.expect( re.match("^[a-z0-9_]+$", username), "Username may contain only lowercase letters, numbers and " "underscores", "username") valid.expect(username not in username_blacklist, "This username is not available", "username") valid.expect( len(email) <= 256, "Email must be no more than 256 characters.", "email") valid.expect(8 <= len(password) <= 512, "Password must be between 8 and 512 characters.", "password") if not valid.ok: return render_template("register.html", is_open=(is_open or invite_hash is not None), **valid.kwargs), 400 user = User(username) user.email = email user.password = bcrypt.hashpw(password.encode('utf-8'), salt=bcrypt.gensalt()).decode('utf-8') user.invites = cfg("meta.sr.ht::settings", "user-invites", default=0) send_email( 'confirm', user.email, 'Confirm your {} account'.format(site_name), headers={ "From": f"{cfg('sr.ht', 'owner-name')} <*****@*****.**>", "To": "{} <{}>".format(user.username, user.email), "Reply-To": f"{cfg('sr.ht', 'owner-name')} <{cfg('sr.ht', 'owner-email')}>", }, user=user) db.session.add(user) if invite: db.session.flush() invite.recipient_id = user.id metrics.meta_registrations.inc() db.session.commit() return redirect("/registered")
def profile_POST(): valid = Validation(request) user = User.query.filter(User.id == current_user.id).one() email = valid.optional("email", user.email) email = email.strip() url = valid.optional("url", user.url) location = valid.optional("location", user.location) bio = valid.optional("bio", user.bio) valid.expect(not url or 0 <= len(url) <= 256, "URL must fewer than 256 characters.", "url") valid.expect(not url or valid_url(url), "URL must be a valid http or https URL", "url") valid.expect(not location or 0 <= len(location) <= 256, "Location must fewer than 256 characters.", "location") valid.expect(not bio or 0 <= len(bio) <= 4096, "Bio must fewer than 4096 characters.", "bio") if not valid.ok: return render_template("profile.html", email=email, url=url, location=location, bio=bio, valid=valid), 400 user.url = url user.location = location user.bio = bio new_email = user.email != email if new_email: valid.expect( len(email) <= 256, "Email must be no more than 256 characters.", "email") prev = User.query.filter(User.email == email).first() valid.expect(not prev, "This email address is already in use.", "email") if not valid.ok: return render_template("profile.html", email=email, url=url, location=location, bio=bio, valid=valid), 400 user.new_email = email user.gen_confirmation_hash() send_email('update_email_old', user.email, 'Your {} email address is changing'.format(site_name), new_email=email) send_email('update_email_new', user.new_email, 'Confirm your {} email address change'.format(site_name), new_email=email) audit_log("updated profile") db.session.commit() UserWebhook.deliver(UserWebhook.Events.profile_update, user.to_dict(), UserWebhook.Subscription.user_id == user.id) return render_template("profile.html", new_email=new_email)
def secrets_POST(): valid = Validation(request) name = valid.optional("name") secret_type = valid.require("secret_type", friendly_name="Secret Type") valid.expect(not name or 3 < len(name) < 512, "Name must be between 3 and 512 characters", field="name") if secret_type is not None: try: secret_type = SecretType(secret_type) except: valid.error("{} is not a valid secret type".format(secret_type), field="secret_type") if secret_type == SecretType.plaintext_file: _secret = valid.optional("secret") secret_file = valid.optional("file-file", max_file_size=16384) for f in ["secret", "file-file"]: valid.expect(bool(_secret) ^ bool(secret_file), "Either secret text or file have to be provided", field=f) if _secret: _secret = _secret.replace('\r\n', '\n') if not _secret.endswith('\n'): _secret += '\n' else: _secret = secret_file else: _secret = valid.require("secret", friendly_name="Secret") if isinstance(_secret, str): _secret = _secret.encode() if not valid.ok: return render_template("secrets.html", **valid.kwargs) secret = Secret(current_user, secret_type) if secret_type == SecretType.pgp_key: try: key, _ = pgpy.PGPKey.from_blob(_secret) if key.is_protected: valid.error("PGP key cannot be passphrase protected.", field="secret") except Exception as ex: valid.error("Invalid PGP key.", field="secret") elif secret_type == SecretType.plaintext_file: file_path = valid.require("file-path", friendly_name="Path") file_mode = valid.require("file-mode", friendly_name="Mode") if not valid.ok: return render_template("secrets.html", **valid.kwargs) try: file_mode = int(file_mode, 8) except: valid.error("Must be specified in octal", field="file-mode") if not valid.ok: return render_template("secrets.html", **valid.kwargs) secret.path = file_path secret.mode = file_mode if not valid.ok: return render_template("secrets.html", **valid.kwargs) secret.name = name secret.secret = _secret db.session.add(secret) db.session.commit() session["message"] = "Successfully added secret {}.".format(secret.uuid) return redirect("/secrets")
def tracker_submit_POST(owner, name): tracker, access = get_tracker(owner, name, True) if not tracker: abort(404) if not TicketAccess.submit in access: abort(403) valid = Validation(request) title = valid.require("title", friendly_name="Title") desc = valid.optional("description") another = valid.optional("another") valid.expect(not title or 3 <= len(title) <= 2048, "Title must be between 3 and 2048 characters.", field="title") valid.expect(not desc or len(desc) < 16384, "Description must be no more than 16384 characters.", field="description") if not valid.ok: db.session.commit() # Unlock tracker row return return_tracker(tracker, access, **valid.kwargs), 400 ticket = Ticket() ticket.submitter_id = current_user.id ticket.tracker_id = tracker.id ticket.scoped_id = tracker.next_ticket_id tracker.next_ticket_id += 1 ticket.title = title ticket.description = desc db.session.add(ticket) tracker.updated = datetime.utcnow() # TODO: Handle unique constraint failure (contention) and retry? db.session.commit() event = Event() event.event_type = EventType.created event.user_id = current_user.id event.ticket_id = ticket.id db.session.add(event) db.session.flush() ticket_url = url_for("ticket.ticket_GET", owner=tracker.owner.canonical_name, name=name, ticket_id=ticket.scoped_id) subscribed = False for sub in tracker.subscriptions: notification = EventNotification() notification.user_id = sub.user_id notification.event_id = event.id db.session.add(notification) if sub.user_id == ticket.submitter_id: subscribed = True continue notify(sub, "new_ticket", "{}/{}/#{}: {}".format(tracker.owner.canonical_name, tracker.name, ticket.scoped_id, ticket.title), headers={ "From": "~{} <{}>".format(current_user.username, notify_from), "Sender": smtp_user, }, ticket=ticket, ticket_url=ticket_url) if not subscribed: sub = TicketSubscription() sub.ticket_id = ticket.id sub.user_id = current_user.id db.session.add(sub) db.session.commit() if another: session["another"] = True return redirect( url_for(".tracker_GET", owner=tracker.owner.canonical_name, name=name)) else: return redirect(ticket_url)