From a74035e2fc3d8d0148a84d4937d51cbb480acb43 Mon Sep 17 00:00:00 2001 From: ex_tianzy Date: Fri, 6 Nov 2026 10:40:33 +0800 Subject: [PATCH] fix(oauth): separate implicit token scopes from explicit permission requests --- deployable/EXISTING_ECS_BROKER_DEPLOY.md | 18 +++++++++++++++++ .../work-product-profiles.expanded.json | 1 - deployable/scripts/broker_deploy.py | 16 +++++++++++---- deployable/tests/test_broker_deploy.py | 20 +++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/deployable/EXISTING_ECS_BROKER_DEPLOY.md b/deployable/EXISTING_ECS_BROKER_DEPLOY.md index b5d9821..72135e3 100644 --- a/deployable/EXISTING_ECS_BROKER_DEPLOY.md +++ b/deployable/EXISTING_ECS_BROKER_DEPLOY.md @@ -164,3 +164,21 @@ sudo python3 deployable/scripts/broker_deploy.py update-permissions \ 核对预览只列出已开通的scope后,用相同命令加`--apply`。脚本校验活动容器、计划完整性、已有scope不被移除,备份overlay和plan并同步校验记录,只recreate connector;相同配置重跑不重启。镜像、router、长连接、数据库卷、密钥和绑定保持,connector重建期间请求可能短暂中断;失败恢复原配置并尝试重建旧connector,失败状态明确报告。不输出解析后的环境或密钥。 完成后必须发起**新的授权事务**:旧授权页面/已创建事务的scope不会随环境变更自动扩展。确认授权URL的scope已包含新增项、实际token返回granted_scopes包含目标范围,再以真实业务code和数据验收。不要手改授权URL绕过ECS产品权限Profile。新权限要求可能使旧凭据暂时需要重新授权,使用测试用户完成验证。 + +### 私有化平台授权目录核对修订(2026-11-06) + +扩展profile原先误将token返回的auth:user.id:read作为显式可申请scope。登录实际开发者后台后发现该值不在524项应用权限目录中;不能把token的granted_scopes直接复制成authorize请求清单。当前示例已移除此项。 + +已有运行配置含该项时,先核对开发者后台其余23项均已开通且发布,再执行: + +```bash +git pull --ff-only +sudo python3 deployable/scripts/broker_deploy.py update-permissions \ + --plan /home/cnooc/hainuo-broker-update-20261105-recovery \ + --profiles deployable/examples/work-product-profiles.expanded.json \ + --allow-remove-scope auth:user.id:read +``` + +默认仍只预览。核对移除项仅为auth:user.id:read后,同命令加--apply。仅显式列出的scope可以移除;不删除已有凭据、绑定或应用权限。此为授权请求清单修正,不是撤销用户在平台上的权限。 + +实际后台还发现contact:department.base:readonly、contact:department.organize:readonly、task:tasklist:write三项未开通(后台status=0,其余对应已开通项status=5)。开通/发布这三项,或由产品决定移出首版申请清单后,必须创建新的授权事务,旧事务仍绑定旧scopes_json。本次没有自动替用户开通后台权限。 diff --git a/deployable/examples/work-product-profiles.expanded.json b/deployable/examples/work-product-profiles.expanded.json index e9eb484..847339a 100644 --- a/deployable/examples/work-product-profiles.expanded.json +++ b/deployable/examples/work-product-profiles.expanded.json @@ -1,6 +1,5 @@ { "current-config-v1": [ - "auth:user.id:read", "bitable:app", "calendar:calendar", "calendar:calendar:readonly", diff --git a/deployable/scripts/broker_deploy.py b/deployable/scripts/broker_deploy.py index f516217..2abef79 100644 --- a/deployable/scripts/broker_deploy.py +++ b/deployable/scripts/broker_deploy.py @@ -623,7 +623,7 @@ def container_permission_profiles(container): raise DeployError('cannot verify active connector product scopes') from None -def update_permissions(root,plan,profiles_path,apply=False): +def update_permissions(root,plan,profiles_path,apply=False,allow_remove_scopes=None): """Change approved scopes on the active broker, without changing images/keys.""" profiles=profiles_from(Path(profiles_path)) check(root,plan,baseline=False) @@ -636,11 +636,18 @@ def update_permissions(root,plan,profiles_path,apply=False): old=json.loads(overlay['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES']) if container_permission_profiles(current)!=old: raise DeployError('active connector scopes differ from selected plan; no changes made') - if set(old)!=set(profiles) or any(not set(scopes)<=set(profiles[name]) for name,scopes in old.items()): + if set(old)!=set(profiles): raise DeployError('permission update is additive only; preserve all existing profiles and scopes') + removals={name:sorted(set(scopes)-set(profiles[name])) for name,scopes in old.items()} + removed={scope for values in removals.values() for scope in values} + allowed=set(allow_remove_scopes or []) + desired={scope for scopes in profiles.values() for scope in scopes} + if not removed<=allowed or allowed & desired: + raise DeployError('permission update is additive only; each reviewed removal requires exact --allow-remove-scope') additions={name:sorted(set(scopes)-set(old[name])) for name,scopes in profiles.items()} print('Product scope additions: '+json.dumps(additions,sort_keys=True)) - if not any(additions.values()): + if removed:print('Product scope removals (explicitly reviewed): '+json.dumps(removals,sort_keys=True)) + if not any(additions.values()) and not removed: print('PERMISSIONS UNCHANGED: no files or containers changed') return if not apply: @@ -705,6 +712,7 @@ def main(): permissions.add_argument('--plan',required=True) permissions.add_argument('--profiles',required=True) permissions.add_argument('--apply',action='store_true') + permissions.add_argument('--allow-remove-scope',action='append',default=[]) for name in ("repair-router","state","diagnose","check","build","activate","rollback"): cmd = sub.add_parser(name) cmd.add_argument("--plan",required=True) @@ -726,7 +734,7 @@ def main(): root = Path(args.plan).resolve() plan = load_plan(root) if args.action == 'update-permissions': - update_permissions(root,plan,args.profiles,args.apply) + update_permissions(root,plan,args.profiles,args.apply,args.allow_remove_scope) elif args.action == "repair-router": repair_router(root,plan) elif args.action == "state": diff --git a/deployable/tests/test_broker_deploy.py b/deployable/tests/test_broker_deploy.py index abe4a68..9ac348f 100644 --- a/deployable/tests/test_broker_deploy.py +++ b/deployable/tests/test_broker_deploy.py @@ -616,3 +616,23 @@ def test_permission_failed_restart_restores_overlay_and_plan(scenario,monkeypatc with pytest.raises(deploy.DeployError,match='restored'):deploy.update_permissions(scenario['output'],plan,scenario['profiles'],True) assert calls[0]==2 for name,value in before.items():assert json.loads((scenario['output']/name).read_text())==json.loads(value) + + +def test_permission_removal_requires_exact_explicit_scope_and_is_reviewed(scenario,monkeypatch,capsys): + plan=permission_scenario(scenario,monkeypatch) + # Existing synthetic profile has only read; replacing it must be explicit. + scenario['profiles'].write_text(json.dumps({'product-v1':['wiki:wiki']})) + with pytest.raises(deploy.DeployError,match='exact'): + deploy.update_permissions(scenario['output'],plan,scenario['profiles'],False,['other:scope']) + deploy.update_permissions(scenario['output'],plan,scenario['profiles'],False,['read']) + assert 'Product scope removals' in capsys.readouterr().out + assert not any('up' in c for c in scenario['calls']) + deploy.update_permissions(scenario['output'],plan,scenario['profiles'],True,['read']) + scopes=json.loads(json.loads((scenario['output']/'compose.broker.json').read_text())['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES']) + assert scopes=={'product-v1':['wiki:wiki']} + + +def test_product_explicit_request_does_not_copy_implicit_token_scope(): + scopes=json.loads((ROOT/'examples/work-product-profiles.expanded.json').read_text())['current-config-v1'] + assert 'auth:user.id:read' not in scopes + assert {'drive:drive','wiki:wiki','calendar:calendar'}<=set(scopes)