- Credit a Monero invoice in the same transaction that claims it, so two settlements that see different transaction sets cannot both credit it. - Add an optional signed manifest_sequence; a node and its mesh peers refuse an approved manifest older than the installed one, so a replayed approval cannot undo a suspension or key revocation. - Keep revoked device key IDs revoked instead of reactivating them on registration. - Take the rate-limit address from CF-Connecting-IP or the proxy's own X-Forwarded-For entry, never the client-chosen first entry, and key per-account challenge and login limits by account and address so strangers cannot lock an account out. - Keep a signed sync transaction spent once its writes commit. - Stop a paired peer widening its scope by omitting app, collection or space lists; cap mesh pull pages and peer response size. - Bound metric labels by route pattern, cap request headers at 64 KiB, check subprotocol WebSocket tokens against deleted accounts, match app record prefixes case-sensitively, compare the app admin token in constant time, require a node key's public half to match its seed, and stop printing invalid secrets or raw database errors. - Only render http and https peer links on the nodes page.
124 lines
4.4 KiB
Ziran
124 lines
4.4 KiB
Ziran
#import "std/go_types"
|
|
#import "device_keys"
|
|
config :: #import "config";
|
|
auth :: #import "authentication";
|
|
http_auth :: #import "http_auth";
|
|
http_body :: #import "http_body";
|
|
response :: #import "response";
|
|
http :: #import "std/http_go";
|
|
sql :: #import "std/sql_go";
|
|
keys :: #import "device_keys";
|
|
json :: #import "std/json_go";
|
|
errors :: #import "std/errors_go";
|
|
metrics :: #import "metrics";
|
|
|
|
Devices :: struct {
|
|
database: *sql.Database
|
|
configuration: *config.Config
|
|
counters: *metrics.ServerMetrics
|
|
verify: auth.VerifySignature
|
|
replayError: Error
|
|
}
|
|
|
|
#scope_file
|
|
DevicesResponse :: struct {
|
|
devices: []DeviceKey #go_tag "json:\"devices\""
|
|
}
|
|
DeviceRevocationResponse :: struct {
|
|
status: string #go_tag "json:\"status\""
|
|
}
|
|
|
|
#scope_export
|
|
Route :: (devices: Devices, writer: http.ResponseWriter, request: *http.Request) {
|
|
user := http_auth.BearerUser(devices.database, request, devices.configuration.TokenSecret)
|
|
if user.authentication.error != null || user.authentication.status != cast(isize)0 {
|
|
http_auth.Respond(writer, devices.counters, user.authentication)
|
|
return
|
|
}
|
|
method := http.Method(request)
|
|
if method == "GET" {
|
|
listed := keys.List(devices.database, http.Context(request), user.value)
|
|
if listed.error != null {
|
|
response.Error(writer, cast(isize)500, "device list failed")
|
|
return
|
|
}
|
|
value: DevicesResponse
|
|
value.devices = listed.value
|
|
response.JSON(writer, cast(isize)200, value)
|
|
return
|
|
}
|
|
if method == "DELETE" {
|
|
Revoke(devices, writer, request, user.value)
|
|
return
|
|
}
|
|
body := http_body.ReadJSON(writer, request, devices.configuration.MaxBodyBytes)
|
|
if body.error != null {
|
|
response.Error(writer, cast(isize)400, errors.Message(body.error))
|
|
return
|
|
}
|
|
registration: DeviceRegistrationRequest
|
|
if json.Unmarshal(body.value, *registration) != null {
|
|
response.Error(writer, cast(isize)400, "invalid device registration")
|
|
return
|
|
}
|
|
keys.NormalizeRegistration(*registration)
|
|
context := http.Context(request)
|
|
verified := keys.VerifyRegistration(devices.database, context, user.value, registration, devices.verify)
|
|
if verified.error != null || verified.status != cast(isize)0 {
|
|
http_auth.Respond(writer, devices.counters, verified)
|
|
return
|
|
}
|
|
device: DeviceKey
|
|
device.AccountID = user.value
|
|
device.AppID = registration.AppID
|
|
device.KeyID = registration.KeyID
|
|
device.ClientID = registration.ClientID
|
|
device.PublicKey = registration.PublicKey
|
|
error := keys.Register(devices.database, context, device, registration.Nonce, devices.replayError)
|
|
if error != null {
|
|
if errors.Is(error, devices.replayError) {
|
|
response.Error(writer, cast(isize)409, "device registration replay")
|
|
return
|
|
}
|
|
if errors.Is(error, keys.ErrDeviceKeyRevoked) {
|
|
response.Error(writer, cast(isize)409, errors.Message(error))
|
|
return
|
|
}
|
|
response.Error(writer, cast(isize)500, "device registration failed")
|
|
return
|
|
}
|
|
response.JSON(writer, cast(isize)200, device)
|
|
}
|
|
|
|
Revoke :: (devices: Devices, writer: http.ResponseWriter, request: *http.Request, accountID: string) {
|
|
body := http_body.ReadJSON(writer, request, devices.configuration.MaxBodyBytes)
|
|
if body.error != null {
|
|
response.Error(writer, cast(isize)400, errors.Message(body.error))
|
|
return
|
|
}
|
|
revocation: DeviceRevocationRequest
|
|
if json.Unmarshal(body.value, *revocation) != null {
|
|
response.Error(writer, cast(isize)400, "invalid device revocation")
|
|
return
|
|
}
|
|
keys.NormalizeRevocation(*revocation)
|
|
context := http.Context(request)
|
|
verified := keys.VerifyRevocation(devices.database, context, accountID, revocation, devices.verify)
|
|
if verified.error != null || verified.status != cast(isize)0 {
|
|
http_auth.Respond(writer, devices.counters, verified)
|
|
return
|
|
}
|
|
error := keys.Revoke(devices.database, context, accountID, revocation, devices.replayError)
|
|
if error != null {
|
|
if errors.Is(error, devices.replayError) {
|
|
response.Error(writer, cast(isize)409, "device revocation replay")
|
|
return
|
|
}
|
|
response.Error(writer, cast(isize)500, "device revocation failed")
|
|
return
|
|
}
|
|
value: DeviceRevocationResponse
|
|
value.status = "revoked"
|
|
response.JSON(writer, cast(isize)200, value)
|
|
}
|