Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions internal/broker/logins.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,10 @@ func (rq *request) sameLogin(page loginmatch.Page, item string, sel loginSel, l
// of now, and hands out its username and password. On a failure the
// request is answered.
func (rq *request) handOutLogin(ctx context.Context, rc receipt, page loginmatch.Page, item string, sel loginSel, l store.Login, from string) {
if !rq.sameLogin(page, item, sel, l, from) || !rq.stillAllowedLogin(sel, item, func() {}) {
if ok, answered := rq.loginStillSame(page, item, sel, l, from, func() {}); !ok {
if answered {
return // the reload check has written the one denied entry
}
rq.s.log.Warn("broker: the login item changed while the request waited; the request is denied",
slog.String("request_id", rq.sub.RequestID), slog.String("client", rq.client))
rq.ended = true
Expand Down Expand Up @@ -660,26 +663,39 @@ func (rq *request) fetchApproved(ctx context.Context, item string) (store.Login,
return store.Login{}, "", false
}

// loginStillSame runs sameLogin and then stillAllowedLogin. ok is false
// when either fails. answered is true when the reload check denied the
// request: it has written the one denied entry and answered, so the
// caller must not write a second entry (#220). When ok is false and
// answered is false, the caller writes the entry and answers.
func (rq *request) loginStillSame(page loginmatch.Page, item string, sel loginSel, l store.Login, from string, beforeDeny func()) (ok, answered bool) {
if !rq.sameLogin(page, item, sel, l, from) {
return false, false
}
return rq.stillAllowedLogin(sel, item, beforeDeny)
}

// stillAllowedLogin checks, after the tap, that a reload did not take away
// what the request uses: the client and its pin, and the rule of the item.
// When a reload took the client or its pin away, it runs beforeDeny, then
// denies the request and answers.
func (rq *request) stillAllowedLogin(sel loginSel, item string, beforeDeny func()) bool {
// denies the request and answers, and answered is true. When only the
// rule changed, ok and answered are both false: the caller denies.
func (rq *request) stillAllowedLogin(sel loginSel, item string, beforeDeny func()) (ok, answered bool) {
if !rq.allowedNow(rq.decided, nil, "", nil) {
beforeDeny()
rq.denyReloaded()
return false // it has answered
return false, true
}
l, ok := rq.s.view().cfg.Logins[sel.section]
if !ok {
return false
l, found := rq.s.view().cfg.Logins[sel.section]
if !found {
return false, false
}
clients, rule, ok := l.RuleFor(item)
clients, rule, found := l.RuleFor(item)
if sel.meta.Reprompt {
rule.Ask = config.AskAlways // as in checkLoginItem
}
return ok && slices.Equal(clients, sel.clients) && slices.Contains(clients, rq.client) &&
reflect.DeepEqual(rule, sel.rule)
return found && slices.Equal(clients, sel.clients) && slices.Contains(clients, rq.client) &&
reflect.DeepEqual(rule, sel.rule), false
}

// listLogins answers a request for the list of logins of a page. It needs
Expand Down Expand Up @@ -965,8 +981,11 @@ func (rq *request) preapproveLogin(ctx context.Context, req *api.PreapproveReque
// time since the tap includes the fetch.
end := rq.s.nowTime().Add(approval.LoginPreapproveTime - res.SinceTap())
// Clear the copy of the request before any answer, as below.
if !rq.sameLogin(page, item, sel, l, from) || !rq.stillAllowedLogin(sel, item, l.Clear) {
if ok, answered := rq.loginStillSame(page, item, sel, l, from, l.Clear); !ok {
l.Clear()
if answered {
return // the reload check has written the one denied entry
}
rq.s.log.Warn("broker: the login item changed while the request waited; the pre-approval is denied",
slog.String("request_id", rq.sub.RequestID), slog.String("client", rq.client))
rq.ended = true
Expand Down
104 changes: 104 additions & 0 deletions internal/broker/reloaddeny_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package broker

import (
"maps"
"net/http"
"slices"
"strings"
"testing"
"time"

"github.com/code6x/nodkey/internal/api"
"github.com/code6x/nodkey/internal/audit"
"github.com/code6x/nodkey/internal/config"
)

// TestLoginReloadDenyOneEntry: a reload during the fetch after the tap
// denies a login and a login pre-approval with exactly one denied entry
// (#220). Before, a reload that took the pin away wrote two: one by the
// reload check, and one by the caller. A reload that changes only the
// rule or the clients of the item is still denied by the caller, with one
// entry.
func TestLoginReloadDenyOneEntry(t *testing.T) {
// takePin gives the client laptop another pin.
takePin := func(t *testing.T, l *loginTest) {
next := *l.svc.view().cfg
next.Clients = maps.Clone(next.Clients)
c := next.Clients["laptop"]
c.Pin[0] ^= 0xff // another key
next.Clients["laptop"] = c
if _, err := l.svc.Reload(&next, l.svc.view().keys, strings.Repeat("a", 64)); err != nil {
t.Error(err)
}
}
// changeRule makes the rule of the section web stricter.
changeRule := func(t *testing.T, l *loginTest) {
next := *l.svc.view().cfg
next.Logins = maps.Clone(next.Logins)
w := next.Logins["web"]
w.Rule.Window = 4 * time.Hour
w.Rule.Ask = config.AskWindow
next.Logins["web"] = w
if _, err := l.svc.Reload(&next, l.svc.view().keys, strings.Repeat("a", 64)); err != nil {
t.Error(err)
}
}
// addClient lets one more client ask for the logins of web. The rule
// stays the same, so only the check of the clients in
// stillAllowedLogin sees the change.
addClient := func(t *testing.T, l *loginTest) {
next := *l.svc.view().cfg
next.Logins = maps.Clone(next.Logins)
w := next.Logins["web"]
w.Clients = append(slices.Clone(w.Clients), "ci")
next.Logins["web"] = w
if _, err := l.svc.Reload(&next, l.svc.view().keys, strings.Repeat("a", 64)); err != nil {
t.Error(err)
}
}
login := func(l *loginTest) *result {
return l.approveLogin(itemShop, pageShop, audit.AnswerApproveOnce)
}
pre := func(l *loginTest) *result {
p := l.start(l.laptp, api.PathPreapprove, api.PreapproveRequest{Login: itemShop, URL: pageShop, Uses: 1})
pr := l.waitAsked(1)[0].Prompt
l.tap(pr.RequestID, audit.AnswerApproveOnce)
return p.wait(t)
}
cases := []struct {
name string
reload func(*testing.T, *loginTest)
ask func(*loginTest) *result
}{
{"login: the pin is taken away", takePin, login},
{"pre-approval: the pin is taken away", takePin, pre},
{"login: the rule changed", changeRule, login},
{"pre-approval: the rule changed", changeRule, pre},
{"login: the clients changed", addClient, login},
{"pre-approval: the clients changed", addClient, pre},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
l := newLoginEnv(t, nil)
l.vs.lmu.Lock()
l.vs.onFetch = func(n int) {
if n == 2 { // the fetch after the tap
c.reload(t, l)
}
}
l.vs.lmu.Unlock()
mustStatus(t, c.ask(l), http.StatusForbidden)
// The caller wrote its entry after the answer: wait until
// the broker is done with the request.
l.svc.waitIdle(t, "laptop")
events := l.audit.all()
got := requestEvents(events, lastRequestID(t, events))
mustEvents(t, got, "request decision:approve_once denied:not_allowed")
if n := l.svc.rememberedCount(); n != 0 {
t.Errorf("the broker keeps %d logins", n)
}
l.noGrant()
l.vs.checkCleared(t)
})
}
}