Compare commits

...
2 Commits
Author SHA1 Message Date
Sakurasan 3e7efb3c88 fix passkey
server/internal/passkey/passkey.go
 的 takeSession,过期校验只在显式设置了 Expires 时才生效(与 go-webauthn 库内部 !IsZero() 的检查一致)
2026-08-19 00:00:51 +08:00
SakurasanandClaude 8495694671 构建: 入口脚本 chown 数据目录后降权运行
绑定挂载/命名卷被 docker 以 root 自动创建时,容器非 root 用户
写不进 db(SQLITE_CANTOPEN)。改为 entrypoint 以 root 启动,
chown 数据目录到 OT_UID:OT_GID(默认 1000:1000)后 su-exec 降权,
compose 不再需要 user: 覆盖。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-18 22:25:43 +08:00
5 changed files with 80 additions and 7 deletions
+5 -4
View File
@@ -35,18 +35,19 @@ RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
# ---------- 阶段 3:运行镜像 ----------
FROM alpine:3.21
RUN apk add --no-cache ca-certificates tzdata \
&& addgroup -S openteam && adduser -S -G openteam openteam
RUN apk add --no-cache ca-certificates tzdata su-exec
WORKDIR /app
COPY --from=go-builder /out/openteam ./openteam
# 与二进制同目录放置,符合服务 CWD 约定(router 按相对路径 web/dist 找静态资源)
COPY --from=web-builder /src/dist ./web/dist
RUN mkdir -p /app/data && chown -R openteam:openteam /app
USER openteam
# 入口脚本以 root 启动,chown 数据目录后 su-exec 降权为 OT_UID:OT_GID(默认 1000:1000)
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh && mkdir -p /app/data
ENV OT_ENV=production \
OT_PORT=8080 \
OT_DB_DRIVER=sqlite \
OT_DB_DSN=data/openteam.db
EXPOSE 8080
VOLUME ["/app/data"]
ENTRYPOINT ["/entrypoint.sh"]
CMD ["./openteam"]
+3 -2
View File
@@ -17,8 +17,6 @@ services:
restart: unless-stopped
ports:
- "${HOST_PORT:-8080}:8080"
# 以宿主用户运行,保证能写当前目录的 ./data 绑盘(默认 1000:1000,可用 OT_UID/OT_GID 覆盖)
user: "${OT_UID:-1000}:${OT_GID:-1000}"
environment:
OT_ENV: production
OT_PORT: 8080
@@ -29,6 +27,9 @@ services:
OT_MASTER_KEY: ${OT_MASTER_KEY:?请在 .env 中设置 OT_MASTER_KEY}
OT_ADMIN_PASSWORD: ${OT_ADMIN_PASSWORD:?请在 .env 中设置 OT_ADMIN_PASSWORD}
OT_JWT_SECRET: ${OT_JWT_SECRET:?请在 .env 中设置 OT_JWT_SECRET}
# 入口脚本据此 chown 数据目录并降权运行(默认 1000:1000)
OT_UID: "${OT_UID:-1000}"
OT_GID: "${OT_GID:-1000}"
volumes:
- ./data:/app/data
healthcheck:
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
# openteam 容器入口:以 root 短暂启动,修正数据目录属主后降权执行服务。
# 解决绑定挂载/命名卷被 docker 以 root 自动创建、应用非 root 用户写不进 db 的问题。
set -e
PUID="${OT_UID:-1000}"
PGID="${OT_GID:-1000}"
mkdir -p /app/data
chown -R "${PUID}:${PGID}" /app/data
exec su-exec "${PUID}:${PGID}" "$@"
+4 -1
View File
@@ -235,7 +235,10 @@ func (s *Service) takeSession(challenge string) (webauthn.SessionData, bool) {
delete(s.sessions, challenge)
}
s.mu.Unlock()
if ok && time.Now().After(sess.Expires) {
// Expires 可能为零值:go-webauthn 默认 Enforce=false 不设过期时间。
// 零值时间恒早于 now,直接 After 会把每个 challenge 都判为过期,
// 与库内部一致,仅当显式设置了过期时间才做校验。
if ok && !sess.Expires.IsZero() && time.Now().After(sess.Expires) {
return webauthn.SessionData{}, false
}
return sess, ok
+56
View File
@@ -0,0 +1,56 @@
package passkey
import (
"testing"
"github.com/glebarez/sqlite"
"github.com/openteam/server/internal/store"
"gorm.io/gorm"
)
func newTestService(t *testing.T) *Service {
t.Helper()
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&store.User{}, &store.Passkey{}); err != nil {
t.Fatal(err)
}
s, err := New(db, Config{RPID: "localhost", Origin: "http://localhost:8080", Name: "openteam test"})
if err != nil {
t.Fatal(err)
}
return s
}
// TestChallengeSessionRoundTrip 验证 begin 产生的 challenge 能被 complete 取回。
// go-webauthn 默认 Enforce=false,SessionData.Expires 为零值;takeSession 的
// 过期判断必须跳过零值时间,否则每个 challenge 都被当成已过期(线上必现
// "challenge 已过期或不存在")。
func TestChallengeSessionRoundTrip(t *testing.T) {
s := newTestService(t)
u := &store.User{Username: "alice", Email: "alice@example.com", Role: "user", Status: "active"}
if err := s.db.Create(u).Error; err != nil {
t.Fatal(err)
}
creation, err := s.BeginRegistration(u)
if err != nil {
t.Fatal(err)
}
challenge := creation.Response.Challenge.String() // 与前端回传一致的 base64url
sess, ok := s.takeSession(challenge)
if !ok {
t.Fatal("takeSession 返回 false:challenge 被误判为已过期(Expires 零值 bug)")
}
if sess.Challenge != challenge {
t.Fatalf("session challenge 不匹配: %q != %q", sess.Challenge, challenge)
}
// challenge 一次性消费
if _, ok := s.takeSession(challenge); ok {
t.Fatal("takeSession 应一次性消费 challenge,二次取应失败")
}
}