Web 控制台此前所有 admin 角色用户共享全部设备。新增设备级归属:内置 admin 把设备绑定给一个用户名后,该设备对其他 admin/viewer 不可见不可控,只有 owner 与内置 admin 可见可控。 - 新增 device_acl.json(与 users.json 同目录,YAMA_DEVICE_ACL_FILE 可覆盖), 原子写、启动加载;文件损坏拒绝启动,避免私人设备变成公共。 - 可见性:内置 admin 全部;未绑定设备仅 admin 角色;绑定设备仅 owner。 覆盖 get_devices、/api/devices、屏幕墙,以及 connect/term_open/mouse/key/ clipboard/keyframe/rdp_reset 全部控制通道;device_update 也只推给可见者。 - 只有内置 admin(用户名 admin + role admin)能 bind/unbind;admin 角色保留 建用户、删设备、审计等既有功能。删设备清绑定。 - 审计日志也按归属过滤:非内置 admin 只能看到公共设备和自己绑定设备的记录 (device_id / device / devices 三种写法都识别);auditquery 增 DeviceAllowed 钩子,ACL 提供 Owner 快照,避免扫描时逐条加锁。 - 删用户会解绑其设备;但账号名下有绑定设备时只有内置 admin 能删,其他 admin 删除会把设备释放到公共池,属于越权。解绑在删除前完成,解绑失败中止删除; 删除失败时回滚绑定。 - 绑定/解绑后立即重新校验在线会话:失去权限的屏幕/终端会话被断开。 - 本地用户名禁止 '@'(保留给 SSO 邮箱);本地密码登录拒绝 '@' 用户名, SSO 拒绝名为 admin 的身份,避免绕过超级管理员判定。 - 前端:superadmin 卡片显示 owner、绑定按钮和弹窗(本地用户 datalist + SSO 邮箱自由输入);deviceAcl 能力位由服务端挑战下发,C++ 端无此位不显示; 设备 ID 经 jsAttr 转义后再进 onclick,修掉存储型 XSS。 验证:go build/test/vet 全绿;新增 ACL 持久化、可见性矩阵、控制通道拒绝、 绑定/解绑会话重校验、device_update 不泄露、分组过滤、删用户解绑与越权拒绝、 审计按归属过滤等测试;本地起 Go 服务 + WebSocket 探针验证 challenge、绑定、 非 superadmin 拒绝、解绑。 Model: deepseek-flash Co-Authored-By: Claude Code <noreply@anthropic.com>
182 lines
6.1 KiB
Go
182 lines
6.1 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
"github.com/yuanyuanxiang/SimpleRemoter/server/go/logger"
|
|
"github.com/yuanyuanxiang/SimpleRemoter/server/go/wsauth"
|
|
)
|
|
|
|
const testSSOSecret = "test-shared-secret"
|
|
|
|
func signTestToken(t *testing.T, secret string, claims jwt.MapClaims) string {
|
|
t.Helper()
|
|
tok := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
s, err := tok.SignedString([]byte(secret))
|
|
if err != nil {
|
|
t.Fatalf("sign test token: %v", err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// noneToken builds a base64url header/claims JWT with alg "none" and no
|
|
// signature, to exercise the algorithm-confusion rejection path.
|
|
func noneToken(claims jwt.MapClaims) string {
|
|
h, _ := json.Marshal(map[string]string{"alg": "none", "typ": "JWT"})
|
|
c, _ := json.Marshal(claims)
|
|
enc := base64.RawURLEncoding.EncodeToString
|
|
return strings.TrimRight(enc(h), "=") + "." + strings.TrimRight(enc(c), "=") + "."
|
|
}
|
|
|
|
func cookieReq(token string) *http.Request {
|
|
r := &http.Request{Header: http.Header{}}
|
|
r.AddCookie(&http.Cookie{Name: "auth_token", Value: token})
|
|
return r
|
|
}
|
|
|
|
func bearerReq(token string) *http.Request {
|
|
r := &http.Request{Header: http.Header{}}
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
return r
|
|
}
|
|
|
|
func TestJWTDetector(t *testing.T) {
|
|
d := NewJWTDetector(JWTDetectorConfig{Secret: testSSOSecret})
|
|
|
|
valid := func(rolename string) jwt.MapClaims {
|
|
return jwt.MapClaims{
|
|
"username": "alice",
|
|
"rolename": rolename,
|
|
"exp": time.Now().Add(time.Hour).Unix(),
|
|
}
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
req *http.Request
|
|
wantUser string
|
|
wantRole string
|
|
wantOK bool
|
|
}{
|
|
{"admin maps to admin", cookieReq(signTestToken(t, testSSOSecret, valid("admin"))), "alice", "admin", true},
|
|
{"viewer maps to viewer", cookieReq(signTestToken(t, testSSOSecret, valid("viewer"))), "alice", "viewer", true},
|
|
{"empty rolename maps to viewer", cookieReq(signTestToken(t, testSSOSecret, valid(""))), "alice", "viewer", true},
|
|
{"unknown rolename maps to viewer", cookieReq(signTestToken(t, testSSOSecret, valid("operator"))), "alice", "viewer", true},
|
|
{"bearer header accepted", bearerReq(signTestToken(t, testSSOSecret, valid("admin"))), "alice", "admin", true},
|
|
{"no credential", &http.Request{Header: http.Header{}}, "", "", false},
|
|
{"wrong secret rejected", cookieReq(signTestToken(t, "other-secret", valid("admin"))), "", "", false},
|
|
{"expired token rejected", cookieReq(signTestToken(t, testSSOSecret, jwt.MapClaims{
|
|
"username": "alice", "rolename": "admin", "exp": time.Now().Add(-time.Hour).Unix(),
|
|
})), "", "", false},
|
|
{"missing username rejected", cookieReq(signTestToken(t, testSSOSecret, jwt.MapClaims{
|
|
"rolename": "admin", "exp": time.Now().Add(time.Hour).Unix(),
|
|
})), "", "", false},
|
|
{"alg none rejected", cookieReq(noneToken(valid("admin"))), "", "", false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
user, role, ok := d.Detect(tt.req)
|
|
if ok != tt.wantOK {
|
|
t.Fatalf("ok = %v, want %v (user=%q role=%q)", ok, tt.wantOK, user, role)
|
|
}
|
|
if !tt.wantOK {
|
|
return
|
|
}
|
|
if user != tt.wantUser || role != tt.wantRole {
|
|
t.Fatalf("got (%q, %q), want (%q, %q)", user, role, tt.wantUser, tt.wantRole)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A detector with no shared secret must never accept a token, even one signed
|
|
// with an empty key — otherwise a misconfigured deployment would silently
|
|
// accept forged sessions.
|
|
func TestJWTDetectorEmptySecretFailClosed(t *testing.T) {
|
|
d := NewJWTDetector(JWTDetectorConfig{})
|
|
tok := signTestToken(t, "", jwt.MapClaims{
|
|
"username": "alice", "rolename": "admin", "exp": time.Now().Add(time.Hour).Unix(),
|
|
})
|
|
if _, _, ok := d.Detect(cookieReq(tok)); ok {
|
|
t.Fatal("empty-secret detector accepted a token; expected fail-closed")
|
|
}
|
|
}
|
|
|
|
// Non-default config keys must be honored, so a different portal can reuse the
|
|
// detector with no code changes.
|
|
func TestJWTDetectorCustomConfig(t *testing.T) {
|
|
d := NewJWTDetector(JWTDetectorConfig{
|
|
Secret: testSSOSecret,
|
|
CookieName: "session",
|
|
UsernameClaim: "sub",
|
|
RoleClaim: "role",
|
|
AdminRole: "root",
|
|
})
|
|
tok := signTestToken(t, testSSOSecret, jwt.MapClaims{
|
|
"sub": "bob", "role": "root", "exp": time.Now().Add(time.Hour).Unix(),
|
|
})
|
|
r := &http.Request{Header: http.Header{}}
|
|
r.AddCookie(&http.Cookie{Name: "session", Value: tok})
|
|
user, role, ok := d.Detect(r)
|
|
if !ok || user != "bob" || role != "admin" {
|
|
t.Fatalf("got (%q, %q, %v), want (%q, %q, true)", user, role, ok, "bob", "admin")
|
|
}
|
|
}
|
|
|
|
// fakeSSODetector lets the /sso handler's role policy be exercised without a
|
|
// real portal JWT.
|
|
type fakeSSODetector struct {
|
|
username, role string
|
|
ok bool
|
|
}
|
|
|
|
func (f *fakeSSODetector) Detect(*http.Request) (string, string, bool) {
|
|
return f.username, f.role, f.ok
|
|
}
|
|
|
|
// The /sso handler must only bridge administrators. A valid portal session for
|
|
// any other role — or no session at all — returns 401 so the front-end falls
|
|
// back to the built-in login form.
|
|
func TestHandleSSOAdminOnly(t *testing.T) {
|
|
newSrv := func(d SSODetector) *Server {
|
|
return &Server{
|
|
log: logger.New(logger.Config{}),
|
|
auth: wsauth.New(),
|
|
sso: d,
|
|
}
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
detector SSODetector
|
|
wantStatus int
|
|
}{
|
|
{"admin is bridged", &fakeSSODetector{"alice", "admin", true}, http.StatusOK},
|
|
{"built-in username is rejected", &fakeSSODetector{"admin", "admin", true}, http.StatusUnauthorized},
|
|
{"viewer is rejected", &fakeSSODetector{"bob", "viewer", true}, http.StatusUnauthorized},
|
|
{"unknown role is rejected", &fakeSSODetector{"carol", "operator", true}, http.StatusUnauthorized},
|
|
{"no credential is rejected", &fakeSSODetector{ok: false}, http.StatusUnauthorized},
|
|
{"SSO disabled", nil, http.StatusUnauthorized},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s := newSrv(tt.detector)
|
|
rec := httptest.NewRecorder()
|
|
s.handleSSO(rec, httptest.NewRequest(http.MethodPost, "/sso", nil))
|
|
if rec.Code != tt.wantStatus {
|
|
t.Fatalf("status = %d, want %d (body=%s)", rec.Code, tt.wantStatus, rec.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|