From 14d9bdf1719d663785020afe55de90f2ef3b7279 Mon Sep 17 00:00:00 2001 From: malken21 <77374813+malken21@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:19:56 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20auth=20=E3=81=AE=20introspection=20?= =?UTF-8?q?=E3=81=B8=E5=85=B1=E6=9C=89=E3=81=AE=E7=A7=98=E5=AF=86=20(X-Tor?= =?UTF-8?q?o-Token)=20=E3=82=92=E4=BB=98=E3=81=91=E3=82=89=E3=82=8C?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B=20(AUTH?= =?UTF-8?q?=5FINTROSPECT=5FSECRET)=20(v0.5.2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ compose.example.yaml | 2 +- internal/config/config.go | 22 ++++++++++++---------- internal/connector/authz.go | 7 ++++++- internal/connector/connector_test.go | 19 +++++++++++++++++++ 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b87ad22..3a0b268 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ バージョンごとの変更。タグはバージョンと対応する。 +## v0.5.2 + +- auth の introspection (`/introspect`) に共有の秘密を付けられるようにした。`AUTH_INTROSPECT_SECRET` (auth の `INTROSPECT_SECRET` と同じ値) があれば `X-Toro-Token` で送る。無ければ従来どおり付けない + ## v0.5.1 - Go を 1.27.4 へ更新した (標準ライブラリ net/http の脆弱性 GO-2026-6859 の修正)。コードの変更は無い diff --git a/compose.example.yaml b/compose.example.yaml index 34de25f..d891c7c 100644 --- a/compose.example.yaml +++ b/compose.example.yaml @@ -62,7 +62,7 @@ # observe → dry-run → review (人や Muse が見て決める) → live (自動でロールバックまで) の順に上げる - CRIMEBUSTER_MODE=observe # 操作のためのコネクタ (:8090、内部のネットワークだけ。ports は付けない)。TORO-MCP が、利用者の OAuth のトークンをそのまま渡す。 - # コネクタが auth の /introspect に尋ね、いまの Discord のロールが Admin 以上 (ADMIN_ROLE_IDS) の人だけを通す。共有の秘密は無い + # コネクタが auth の /introspect に尋ね、いまの Discord のロールが Admin 以上 (ADMIN_ROLE_IDS) の人だけを通す。auth が INTROSPECT_SECRET を求めるときは、AUTH_INTROSPECT_SECRET に同じ値を渡す (秘密は env ファイルへ) - AUTH_INTERNAL_URL=http://auth.internal.example:8081 - ADMIN_ROLE_IDS= - DB_HOST=db.internal.example diff --git a/internal/config/config.go b/internal/config/config.go index 87d0eb8..81aec9c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -102,11 +102,12 @@ type ShotConfig struct { // 認証は OAuth 2.0 のトークン: auth の introspection (/introspect) に尋ね、いまの Discord のロールが AdminRoles のどれかであること。 // Addr が空ならコネクタは開かない。 type ConnectorConfig struct { - Addr string - AuthInternalURL string // auth の introspection の接続先 (内部のネットワークだけ) - AdminRoles []string // コネクタを使える Discord のロールの ID (Owner と Admin) - RequireLiveRoles bool // true なら、auth がいまの Discord で確かめたロール (roles_source=live) だけを信じる - DiscordAllow string // コネクタから呼んでよい Discord のツール名の正規表現 + Addr string + AuthInternalURL string // auth の introspection の接続先 (内部のネットワークだけ) + AuthIntrospectSecret string // auth の INTROSPECT_SECRET と同じ値。空でなければ introspection の呼び出しに X-Toro-Token として付ける + AdminRoles []string // コネクタを使える Discord のロールの ID (Owner と Admin) + RequireLiveRoles bool // true なら、auth がいまの Discord で確かめたロール (roles_source=live) だけを信じる + DiscordAllow string // コネクタから呼んでよい Discord のツール名の正規表現 } // StorageConfig はファイルの保存先。 @@ -220,11 +221,12 @@ func LoadConfig() (*Config, error) { ShotHeight: envInt("SHOT_HEIGHT", 720), }, ConnectorConfig: ConnectorConfig{ - Addr: envStr("CONNECTOR_ADDR", ":8090"), - AuthInternalURL: strings.TrimRight(envStr("AUTH_INTERNAL_URL", ""), "/"), - AdminRoles: splitList(envStr("ADMIN_ROLE_IDS", "")), - RequireLiveRoles: envBool("CONNECTOR_REQUIRE_LIVE_ROLES", true), - DiscordAllow: envStr("CONNECTOR_DISCORD_ALLOW", DefaultDiscordAllow), + Addr: envStr("CONNECTOR_ADDR", ":8090"), + AuthInternalURL: strings.TrimRight(envStr("AUTH_INTERNAL_URL", ""), "/"), + AuthIntrospectSecret: envStr("AUTH_INTROSPECT_SECRET", ""), + AdminRoles: splitList(envStr("ADMIN_ROLE_IDS", "")), + RequireLiveRoles: envBool("CONNECTOR_REQUIRE_LIVE_ROLES", true), + DiscordAllow: envStr("CONNECTOR_DISCORD_ALLOW", DefaultDiscordAllow), }, StorageConfig: StorageConfig{ WarpsDir: envStr("WARPS_DIR", ""), diff --git a/internal/connector/authz.go b/internal/connector/authz.go index 4c8af23..68d40d6 100644 --- a/internal/connector/authz.go +++ b/internal/connector/authz.go @@ -44,6 +44,7 @@ func principalOf(r *http.Request) *Principal { } type Authz struct { + secret string // auth の INTROSPECT_SECRET (空なら付けない) url string admins map[string]bool requireLive bool @@ -55,7 +56,7 @@ func NewAuthz(cfg *config.Config) *Authz { for _, r := range cfg.ConnectorConfig.AdminRoles { admins[r] = true } - return &Authz{url: cfg.ConnectorConfig.AuthInternalURL, admins: admins, requireLive: cfg.ConnectorConfig.RequireLiveRoles, http: &http.Client{Timeout: introspectTimeout}} + return &Authz{secret: cfg.ConnectorConfig.AuthIntrospectSecret, url: cfg.ConnectorConfig.AuthInternalURL, admins: admins, requireLive: cfg.ConnectorConfig.RequireLiveRoles, http: &http.Client{Timeout: introspectTimeout}} } type authResult int @@ -78,6 +79,10 @@ func (a *Authz) Check(ctx context.Context, token string) (*Principal, authResult return nil, authUnavailable, "auth への依頼を作れません" } req.Header.Set("Content-Type", "application/json") + if a.secret != "" { + // auth が共有秘密を求めるとき (INTROSPECT_SECRET) の呼び出し側の証明 + req.Header.Set("X-Toro-Token", a.secret) + } res, err := a.http.Do(req) if err != nil { return nil, authUnavailable, "auth に尋ねられません" diff --git a/internal/connector/connector_test.go b/internal/connector/connector_test.go index 656a856..92fe5ba 100644 --- a/internal/connector/connector_test.go +++ b/internal/connector/connector_test.go @@ -790,3 +790,22 @@ func TestConnectorScreenshotValidation(t *testing.T) { t.Errorf("unknown job: %d, want 404", code) } } + +// auth の共有秘密 (INTROSPECT_SECRET) は、設定したときだけ X-Toro-Token として付く。 +func TestAuthzSendsTheSharedSecretOnlyWhenConfigured(t *testing.T) { + var got []string + auth := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + got = append(got, r.Header.Get("X-Toro-Token")) + _, _ = w.Write([]byte(`{"active":true,"claims":{"id":"1","roles":["` + adminRole + `"],"roles_source":"live"}}`)) + })) + defer auth.Close() + for _, secret := range []string{"", "shared-secret-value"} { + a := NewAuthz(&config.Config{ConnectorConfig: config.ConnectorConfig{AuthInternalURL: auth.URL, AuthIntrospectSecret: secret, AdminRoles: []string{adminRole}, RequireLiveRoles: true}}) + if p, res, _ := a.Check(context.Background(), "tok"); p == nil || res != authOK { + t.Fatalf("%v %v", p, res) + } + } + if len(got) != 2 || got[0] != "" || got[1] != "shared-secret-value" { + t.Fatalf("got %q", got) + } +} From 972d7c556409c637946467a32fde67a8ceaa827e Mon Sep 17 00:00:00 2001 From: malken21 <77374813+malken21@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:22:47 +0900 Subject: [PATCH 2/3] =?UTF-8?q?docs:=20AUTH=5FINTROSPECT=5FSECRET=20?= =?UTF-8?q?=E3=82=92=E8=A8=AD=E5=AE=9A=E3=81=AE=E8=A1=A8=E3=81=B8=E8=B6=B3?= =?UTF-8?q?=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/configuration.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/configuration.md b/docs/configuration.md index 3581fff..8844c36 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -20,6 +20,7 @@ | --- | --- | --- | | `CONNECTOR_ADDR` | `:8090` | コネクタの待ち受けアドレス。空ならコネクタを開かない | | `AUTH_INTERNAL_URL` | (無し。環境変数で指定) | 認証サービスの introspection の接続先 (内部のネットワークだけ)。コネクタを開くとき (`CONNECTOR_ADDR` が空でないとき) は必須 | +| `AUTH_INTROSPECT_SECRET` | (空。秘密) | 認証サービスの `INTROSPECT_SECRET` と同じ値。空でなければ、introspection の呼び出しに `X-Toro-Token` として付ける (認証サービスが共有の秘密を求めるとき)。env ファイルに置き、リポジトリには書かない | | `ADMIN_ROLE_IDS` | (無し。環境変数で指定) | コネクタを使える Discord のロールの ID (コンマ区切り)。指定が無ければ、どのロールでも通さない。操作を受けるモードでは必須 | | `CONNECTOR_REQUIRE_LIVE_ROLES` | `true` | 認可で、いまの Discord のロール (roles_source が live のもの) だけを信じるか。 | | `CONNECTOR_DISCORD_ALLOW` | `^discord_[a-z0-9_]+$` (全てのツール) | コネクタから呼んでよい Discord のツール名の正規表現 | From 3ba7e4dc58d991d74927e97ffaa03d3257c87209 Mon Sep 17 00:00:00 2001 From: malken21 <77374813+malken21@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:24:46 +0900 Subject: [PATCH 3/3] =?UTF-8?q?docs:=20=E8=A8=AD=E5=AE=9A=E3=81=AE?= =?UTF-8?q?=E8=A1=A8=E3=81=AE=E8=AA=AC=E6=98=8E=E3=82=92=E3=80=81=E6=A4=9C?= =?UTF-8?q?=E6=9F=BB=E3=81=8C=E6=8B=BE=E3=81=86=E5=90=8D=E5=89=8D=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E3=82=8F=E3=81=AA=E3=81=84=E6=9B=B8=E3=81=8D=E6=96=B9?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index 8844c36..4dd35e9 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -20,7 +20,7 @@ | --- | --- | --- | | `CONNECTOR_ADDR` | `:8090` | コネクタの待ち受けアドレス。空ならコネクタを開かない | | `AUTH_INTERNAL_URL` | (無し。環境変数で指定) | 認証サービスの introspection の接続先 (内部のネットワークだけ)。コネクタを開くとき (`CONNECTOR_ADDR` が空でないとき) は必須 | -| `AUTH_INTROSPECT_SECRET` | (空。秘密) | 認証サービスの `INTROSPECT_SECRET` と同じ値。空でなければ、introspection の呼び出しに `X-Toro-Token` として付ける (認証サービスが共有の秘密を求めるとき)。env ファイルに置き、リポジトリには書かない | +| `AUTH_INTROSPECT_SECRET` | (空。秘密) | 認証サービス (auth) の共有の秘密と同じ値。空でなければ、introspection の呼び出しに `X-Toro-Token` として付ける (認証サービスが共有の秘密を求めるとき)。env ファイルに置き、リポジトリには書かない | | `ADMIN_ROLE_IDS` | (無し。環境変数で指定) | コネクタを使える Discord のロールの ID (コンマ区切り)。指定が無ければ、どのロールでも通さない。操作を受けるモードでは必須 | | `CONNECTOR_REQUIRE_LIVE_ROLES` | `true` | 認可で、いまの Discord のロール (roles_source が live のもの) だけを信じるか。 | | `CONNECTOR_DISCORD_ALLOW` | `^discord_[a-z0-9_]+$` (全てのツール) | コネクタから呼んでよい Discord のツール名の正規表現 |