237 lines
8.3 KiB
Ziran
237 lines
8.3 KiB
Ziran
wire :: #import "wire_headers";
|
|
#import "std/go_types"
|
|
#import "metrics"
|
|
ctx :: #import "std/context_go";
|
|
auth :: #import "authentication";
|
|
auth_errors :: #import "authentication_error";
|
|
accounts :: #import "account_keys";
|
|
state :: #import "account_state";
|
|
token :: #import "token";
|
|
sessions :: #import "auth_sessions";
|
|
http :: #import "std/http_go";
|
|
sql :: #import "std/sql_go";
|
|
clock :: #import "std/time_go";
|
|
url :: #import "std/url_go";
|
|
response :: #import "response";
|
|
metrics :: #import "metrics";
|
|
net :: #import "std/net_go";
|
|
constant_time :: #import "std/constant_time";
|
|
go_strings :: #system_library "go:strings";
|
|
go_http :: #system_library "go:net/http";
|
|
|
|
UserHeaderResult :: struct {
|
|
value: string
|
|
name: string
|
|
}
|
|
UserAuthenticationResult :: struct {
|
|
value: string
|
|
authentication: auth.AuthenticationResult
|
|
}
|
|
|
|
#scope_file
|
|
PrefixResult :: struct {
|
|
value: string
|
|
found: bool
|
|
}
|
|
TrimSpace :: (value: string) -> string #foreign go_strings;
|
|
ToLower :: (value: string) -> string #foreign go_strings;
|
|
CutPrefix :: (value: string, prefix: string) -> PrefixResult #go_results #foreign go_strings "CutPrefix";
|
|
TrimPrefix :: (value: string, prefix: string) -> string #foreign go_strings "TrimPrefix";
|
|
TrimSuffix :: (value: string, suffix: string) -> string #foreign go_strings "TrimSuffix";
|
|
RequestHost :: (request: *http.Request) -> string #go_field #foreign go_http "(*Request).Host";
|
|
|
|
LoopbackHost :: (value: string) -> bool {
|
|
split := net.SplitHostPort(value)
|
|
host := split.host
|
|
if split.error != null {
|
|
host = TrimSuffix(TrimPrefix(value, "["), "]")
|
|
}
|
|
if ToLower(host) == "localhost" {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(host)
|
|
return net.ValidIP(ip) && net.IsLoopback(ip)
|
|
}
|
|
|
|
LoopbackOrigin :: (origin: string) -> bool {
|
|
plain := CutPrefix(origin, "http://")
|
|
if plain.found {
|
|
return LoopbackHost(plain.value)
|
|
}
|
|
secure := CutPrefix(origin, "https://")
|
|
if secure.found {
|
|
return LoopbackHost(secure.value)
|
|
}
|
|
return false
|
|
}
|
|
|
|
#scope_export
|
|
// A loopback connection is the operator only when it came straight to this
|
|
// process: no proxy hop, a local Host (which defeats DNS rebinding), and no
|
|
// cross-site browser Origin.
|
|
DirectLocalRequest :: (request: *http.Request) -> bool {
|
|
address := http.RemoteAddress(request)
|
|
split := net.SplitHostPort(address)
|
|
host := split.host
|
|
if split.error != null {
|
|
host = address
|
|
}
|
|
ip := net.ParseIP(host)
|
|
if !net.ValidIP(ip) || !net.IsLoopback(ip) {
|
|
return false
|
|
}
|
|
// A reverse proxy on the same host makes every internet request arrive
|
|
// from loopback, so a forwarding header means this is not the operator.
|
|
proxyHeaders: [8]string = .["X-Forwarded-For", "X-Forwarded-Host", "X-Forwarded-Proto",
|
|
"X-Real-IP", "Forwarded", "CF-Connecting-IP", "True-Client-IP", "Via"]
|
|
headers := http.Headers(request)
|
|
for name: proxyHeaders {
|
|
if wire.Value(headers, name) != "" {
|
|
return false
|
|
}
|
|
}
|
|
if !LoopbackHost(RequestHost(request)) {
|
|
return false
|
|
}
|
|
origin := TrimSpace(wire.Value(headers, "Origin"))
|
|
return origin == "" || LoopbackOrigin(origin)
|
|
}
|
|
|
|
HeaderAlias :: (request: *http.Request, names: []string) -> string {
|
|
headers := http.Headers(request)
|
|
for name: names {
|
|
value := TrimSpace(wire.Value(headers, name))
|
|
if value != "" {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
UserHeader :: (request: *http.Request) -> UserHeaderResult {
|
|
result: UserHeaderResult
|
|
headers := http.Headers(request)
|
|
names: [3]string = .["X-Mesh-User", "X-Ksync-User", "X-Inbe-User"]
|
|
for name: names {
|
|
value := ToLower(TrimSpace(wire.Value(headers, name)))
|
|
if value != "" {
|
|
result.value = value
|
|
result.name = name
|
|
return result
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
RequireAdmin :: (writer: http.ResponseWriter, request: *http.Request, expected: string) -> bool {
|
|
if expected == "" {
|
|
response.Error(writer, cast(isize)403, "admin disabled")
|
|
return false
|
|
}
|
|
names: [2]string = .["X-Mesh-Admin", "X-Ksync-Admin"]
|
|
provided := HeaderAlias(request, names[:])
|
|
if !constant_time.ConstantTimeTextEqual(provided, expected) {
|
|
response.Error(writer, cast(isize)401, "admin token required")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
RequireLocalOperator :: (writer: http.ResponseWriter, request: *http.Request, adminToken: string) -> bool {
|
|
if adminToken != "" {
|
|
return RequireAdmin(writer, request, adminToken)
|
|
}
|
|
if !DirectLocalRequest(request) {
|
|
response.Error(writer, cast(isize)403,
|
|
"administration requires loopback access or MESH_ADMIN_TOKEN")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
AuthenticateToken :: (database: *sql.Database, request: *http.Request, secret: []u8) -> UserAuthenticationResult {
|
|
result: UserAuthenticationResult
|
|
header := TrimSpace(wire.Value(http.Headers(request), "Authorization"))
|
|
bearer := CutPrefix(header, "Bearer ")
|
|
if !bearer.found || TrimSpace(bearer.value) == "" {
|
|
result.authentication = auth.Failure(cast(isize)401, "bearer token required")
|
|
return result
|
|
}
|
|
return AuthenticateBearer(database, request, TrimSpace(bearer.value), secret)
|
|
}
|
|
|
|
// Shared by the Authorization header and the WebSocket subprotocol, so a
|
|
// token of a deleted account is refused on both.
|
|
AuthenticateBearer :: (database: *sql.Database, request: *http.Request, bearer: string, secret: []u8) -> UserAuthenticationResult {
|
|
result: UserAuthenticationResult
|
|
verified := token.VerifyAuthToken(secret, bearer, clock.Unix(clock.Now()))
|
|
if verified.error != "" {
|
|
result.authentication = auth.Failure(cast(isize)401, "invalid bearer token")
|
|
return result
|
|
}
|
|
context := http.Context(request)
|
|
account := accounts.PublicKey(database, context, verified.value)
|
|
if account.error != null {
|
|
result.authentication = auth.NativeFailure(account.error)
|
|
return result
|
|
}
|
|
if !account.found {
|
|
if url.Path(http.RequestURL(request)) == "/api/v1/sync" {
|
|
deleted := state.Tombstoned(database, context, verified.value)
|
|
if deleted.error != null {
|
|
result.authentication = auth.NativeFailure(deleted.error)
|
|
return result
|
|
}
|
|
if !deleted.value {
|
|
authorized := sessions.AuthorizeBearer(database, context, secret, bearer, verified.value,
|
|
clock.Unix(clock.Now()))
|
|
if authorized.error != null {
|
|
result.authentication = auth.NativeFailure(authorized.error)
|
|
return result
|
|
}
|
|
if !authorized.value {
|
|
result.authentication = auth.Failure(cast(isize)401, "invalid bearer token")
|
|
return result
|
|
}
|
|
result.value = verified.value
|
|
return result
|
|
}
|
|
}
|
|
result.authentication = auth.Failure(cast(isize)401, "sync account not found")
|
|
return result
|
|
}
|
|
authorized := sessions.AuthorizeBearer(database, context, secret, bearer, verified.value,
|
|
clock.Unix(clock.Now()))
|
|
if authorized.error != null {
|
|
result.authentication = auth.NativeFailure(authorized.error)
|
|
return result
|
|
}
|
|
if !authorized.value {
|
|
result.authentication = auth.Failure(cast(isize)401, "invalid bearer token")
|
|
return result
|
|
}
|
|
result.value = verified.value
|
|
return result
|
|
}
|
|
|
|
BearerSessionActive :: (database: *sql.Database, context: ctx.Context, bearer: string, userID: string, now: s64) -> bool {
|
|
return sessions.BearerSessionActive(database, context, bearer, userID, now)
|
|
}
|
|
|
|
BearerUser :: (database: *sql.Database, request: *http.Request, secret: []u8) -> UserAuthenticationResult {
|
|
result := AuthenticateToken(database, request, secret)
|
|
if result.authentication.error != null || result.authentication.status != cast(isize)0 {
|
|
return result
|
|
}
|
|
header := UserHeader(request)
|
|
if header.value != "" && header.value != result.value {
|
|
result.value = ""
|
|
result.authentication = auth.Failure(cast(isize)401, "token user mismatch")
|
|
}
|
|
return result
|
|
}
|
|
|
|
Respond :: (writer: http.ResponseWriter, counters: *ServerMetrics, result: auth.AuthenticationResult) {
|
|
auth_errors.Respond(writer, *counters, auth_errors.Convert(result))
|
|
}
|