package api
import (
"context"
"fmt"
"strings"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// The owner's hard stops: what stays with a person signed in at a browser
// even when a token of theirs holds every letter they do. One table says
// them: the gate below refuses by it, and the sentence a login's page
// shows is generated from it, so the two cannot drift.
// browserOnlyCode is the stable code of a refusal a token cannot get past.
const browserOnlyCode = "browser_only"
type hardStop struct {
// Words says what the token does not do, in the sentence's grammar.
Words string
// Page is where, in a browser, the person does it: a path under the
// repository the call named, "" for the forge's front page.
Page string
// Match says whether a call is one. nil is a stop another rule keeps
// (refuseToken's: the person's sign-in and credentials) and only
// appears in the sentence.
Match func(d Deps, desc *ops.Descriptor, in any) bool
}
func opIs(names ...string) func(Deps, *ops.Descriptor, any) bool {
return func(_ Deps, desc *ops.Descriptor, _ any) bool {
for _, n := range names {
if desc.Name == n {
return true
}
}
return false
}
}
var hardStops = []hardStop{
{Words: "change branch protection", Page: "/settings", Match: opIs("branch.protection.set", "ci.checks.set")},
{Words: "override a failing check", Page: "/landings", Match: func(_ Deps, _ *ops.Descriptor, in any) bool {
return inputField(in, "Override") != ""
}},
{Words: "delete or hand on a repository", Page: "/settings", Match: func(_ Deps, desc *ops.Descriptor, _ any) bool { return deedOps[desc.Name] }},
{Words: "act as site admin", Page: "", Match: func(_ Deps, desc *ops.Descriptor, _ any) bool { return desc.Auth == ops.Admin }},
// A login token's sync push to a protected branch is refused at the sync
// front (xfer.Credential.Login), not here: it is a stop with no call.
{Words: "push straight to a protected branch", Page: "/settings"},
{Words: "mint or hand out credentials", Page: "/me", Match: func(_ Deps, desc *ops.Descriptor, _ any) bool {
// Every op that returns a secret once is one, whatever it is called:
// a token, a code, a key, a link. The sign-in link returns a link
// no tag marks.
return desc.HasOnceSecret() || desc.Name == "me.signin-link"
}},
{Words: "grant admin rights to another account", Page: "/settings", Match: func(d Deps, desc *ops.Descriptor, in any) bool {
return adminClass(d.lettersGranted(desc, in))
}},
{Words: "manage your sign-in and credentials"},
}
// hardStopList is the table's words, one entry a stop.
func hardStopList() []string {
words := make([]string, len(hardStops))
for i, h := range hardStops {
words[i] = h.Words
}
return words
}
// hardStopWords is the table's words as one clause: "a, b, or c".
func hardStopWords() string {
words := hardStopList()
return strings.Join(words[:len(words)-1], ", ") + ", or " + words[len(words)-1]
}
// hardStopGate refuses, to a login token, a call the table names. It is
// for login tokens alone: a token that holds its letters on a repository
// keeps using them there (RFC 0021), and a forge agent's site-admin reach
// is by design.
func (d Deps) hardStopGate(ctx context.Context, desc *ops.Descriptor, in any) error {
if tok, ok := ops.TokenOf(ctx); !ok || !tok.Login {
return nil
}
for _, h := range hardStops {
if h.Match == nil || !h.Match(d, desc, in) {
continue
}
next := d.link(h.Page)
if repo := repoNamed(in, desc.Scope == ops.ScopeRepo); repo != "" {
next = d.link("/r/" + repo + h.Page)
}
return &ops.Refusal{Code: browserOnlyCode, Class: ops.ErrForbidden,
Message: fmt.Sprintf("%s is for you signed in at a browser: a token does not %s; do it at %s", desc.Name, h.Words, next),
Fields: map[string]string{"next": next}}
}
return nil
}
// commandGate is every command's gate: the hard stops, then the hold.
func (d Deps) commandGate(ctx context.Context, desc *ops.Descriptor, in any) error {
if err := d.hardStopGate(ctx, desc, in); err != nil {
return err
}
return d.legalHoldGate(ctx, desc, in)
}
// HardStops is the gate that keeps a login token to the table, for the
// forge's doors to put in front of a registry they build themselves.
func (d Deps) HardStops() ops.CommandGate { return d.hardStopGate }
// adminClass says letters hold what the hard-stop acts need: a (branch
// protection, override, the deeds) or s (the forge's own). Reading, check-in
// and the developer's letters are not.
func adminClass(letters string) bool { return strings.ContainsAny(letters, "as") }
// lettersGranted is what a call would give another account: the letters of
// the role or string it names, or of the fossil login it links, read as the
// call's own Run reads them. An op that grants nothing says "". A role or
// login that cannot be read is taken as admin, because the call then fails
// closed rather than open.
func (d Deps) lettersGranted(desc *ops.Descriptor, in any) string {
role := func(name string) string {
if strings.TrimSpace(name) == "" {
return ""
}
r, err := oltp.ParseRole(name)
if err != nil {
return "a"
}
return r.Caps()
}
switch desc.Name {
case "account.grant":
return role(inputField(in, "Role"))
case "agent.grant":
return role(inputField(in, "Role")) + inputField(in, "Caps")
case "account.caps", "repo.category.set":
return inputField(in, "Caps")
case "team.member":
// A team owner manages its members and its repositories, so naming
// one is a grant of the team's reach.
letters := inputField(in, "Caps")
if teamOwnerSet(in) {
letters += "a"
}
return letters
case "team.repo.add", "team.repo.offer.accept":
// Put under a team, a repository takes its members' letters, which
// can be admin-class. Any such move by a login token is refused:
// reading what each member holds would be a second rule to drift.
return "a"
case "repo.fossil.users.sync":
// The sync copies a peer's capabilities onto linked accounts and
// applies its category letters: a grant of whatever the peer says.
return "a"
case "mirror.set":
// Repointing the upstream is how that sync is weaponised: the new
// upstream says what it likes. Only the interval of the upstream
// already set is an ordinary change; no store, no upstream set, or a
// different address is not.
if d.Mirrors == nil {
return "a"
}
m, ok, err := d.Mirrors.MirrorFor(inputField(in, "Repo"))
if err != nil || !ok || strings.TrimSpace(m.URL) != strings.TrimSpace(inputField(in, "URL")) {
return "a"
}
return ""
case "purge.grant":
if inputBool(in, "Revoke") {
return ""
}
return "a"
case "repo.fossil.users.link":
if strings.TrimSpace(inputField(in, "Account")) == "" {
return ""
}
if d.FossilUsers == nil {
return "a"
}
users, err := d.FossilUsers.FossilUsers(inputField(in, "Repo"))
if err != nil {
return "a"
}
for _, u := range users {
if u.Login == inputField(in, "Login") {
return u.Caps
}
}
return "a"
}
return ""
}
// teamOwnerSet says a team.member call names the member an owner.
func teamOwnerSet(in any) bool {
switch v := in.(type) {
case teamMemberIn:
return v.Owner != nil && *v.Owner
case *teamMemberIn:
return v != nil && v.Owner != nil && *v.Owner
}
return false
}