Spend a hashcash stamp only when the request succeeds (closes #158)
check / check (push) Waiting to run
check / check (push) Waiting to run
A session request refused with 400 or 409, or a +H channel message refused for membership, a ban or +m, used to spend its stamp, so a retry with the same stamp was refused as already used. hashcash.Validator.Validate is split into Check, which spends nothing, and Claim, which records the stamp; Release undoes a claim. Session creation checks the stamp first, claims it just before CreateSession and releases it if CreateSession fails. SendChannelMessage checks the stamp after the membership, ban and +m checks. Model: opus-5-5
This commit is contained in:
@@ -199,7 +199,7 @@ func (hdlr *Handlers) handleCreateSession(
|
||||
return
|
||||
}
|
||||
|
||||
if !hdlr.validateHashcash(
|
||||
if !hdlr.checkHashcash(
|
||||
writer, request, payload.Hashcash,
|
||||
) {
|
||||
return
|
||||
@@ -232,14 +232,15 @@ func (hdlr *Handlers) handleCreateSession(
|
||||
}
|
||||
|
||||
hdlr.executeCreateSession(
|
||||
writer, request, payload.Nick, username,
|
||||
writer, request,
|
||||
payload.Nick, username, payload.Hashcash,
|
||||
)
|
||||
}
|
||||
|
||||
func (hdlr *Handlers) executeCreateSession(
|
||||
writer http.ResponseWriter,
|
||||
request *http.Request,
|
||||
nick, username string,
|
||||
nick, username, stamp string,
|
||||
) {
|
||||
remoteIP := clientIP(request)
|
||||
|
||||
@@ -247,12 +248,17 @@ func (hdlr *Handlers) executeCreateSession(
|
||||
request.Context(), remoteIP,
|
||||
)
|
||||
|
||||
if !hdlr.claimHashcash(writer, request, stamp) {
|
||||
return
|
||||
}
|
||||
|
||||
sessionID, clientID, token, err :=
|
||||
hdlr.params.Database.CreateSession(
|
||||
request.Context(),
|
||||
nick, username, hostname, remoteIP,
|
||||
)
|
||||
if err != nil {
|
||||
hdlr.hashcashVal.Release(stamp)
|
||||
hdlr.handleCreateSessionError(
|
||||
writer, request, err,
|
||||
)
|
||||
@@ -273,10 +279,10 @@ func (hdlr *Handlers) executeCreateSession(
|
||||
}, http.StatusCreated)
|
||||
}
|
||||
|
||||
// validateHashcash validates a hashcash stamp if required.
|
||||
// Returns false if validation failed and a response was
|
||||
// already sent.
|
||||
func (hdlr *Handlers) validateHashcash(
|
||||
// checkHashcash checks a hashcash stamp if required,
|
||||
// without spending it. Returns false if the check failed
|
||||
// and a response was already sent.
|
||||
func (hdlr *Handlers) checkHashcash(
|
||||
writer http.ResponseWriter,
|
||||
request *http.Request,
|
||||
stamp string,
|
||||
@@ -295,7 +301,31 @@ func (hdlr *Handlers) validateHashcash(
|
||||
return false
|
||||
}
|
||||
|
||||
err := hdlr.hashcashVal.Validate(
|
||||
err := hdlr.hashcashVal.Check(
|
||||
stamp, hdlr.params.Config.HashcashBits,
|
||||
)
|
||||
if err != nil {
|
||||
hdlr.respondError(
|
||||
writer, request,
|
||||
"invalid hashcash stamp: "+err.Error(),
|
||||
http.StatusPaymentRequired,
|
||||
)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// claimHashcash spends a stamp that checkHashcash accepted.
|
||||
// Returns false if Claim refused the stamp, normally because
|
||||
// it was already spent, and a response was already sent.
|
||||
func (hdlr *Handlers) claimHashcash(
|
||||
writer http.ResponseWriter,
|
||||
request *http.Request,
|
||||
stamp string,
|
||||
) bool {
|
||||
err := hdlr.hashcashVal.Claim(
|
||||
stamp, hdlr.params.Config.HashcashBits,
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user