Skip to content

Conversation

@guohelu
Copy link
Collaborator

@guohelu guohelu commented Nov 29, 2025

Reviewed, transaction id: 66281

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码审查总结

本次 PR 新增了 SCOPE 作用域权限管理功能,整体逻辑清晰。发现以下需要注意的问题:

关键问题

  1. 🚨 异常处理不完整scope_exists 方法捕获异常后返回 False,但 check_parent_task_id 中未处理 Template.DoesNotExist
  2. ⚠️ 逻辑遗漏:新增的 ScopePermission.has_permission 方法缺少返回值
  3. 性能隐患check_parent_task_id 在 SCOPE 类型判断中直接使用 .get() 可能抛出异常并中断认证流程

建议

  • 补充异常处理和缺失的返回值
  • 考虑对 SCOPE 验证逻辑添加单元测试

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码审查

审查发现以下需要注意的问题,请查看具体行内评论。

return Template.objects.filter(
space_id=self.space_id, scope_type=scope_type, scope_value=scope_value
).exists()
except (ValueError, IndexError):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 捕获过于宽泛的异常可能隐藏真实错误。建议只捕获预期的异常类型(如 AttributeError)。

if resource_type != ResourceType.TASK.value:
if resource_type == ResourceType.SCOPE.value:
scope_parts = db_token.resource_id.split("_")
template_obj = Template.objects.get(id=resource_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Template.objects.get() 可能抛出 DoesNotExist 异常,导致认证失败。建议使用 filter().first() 或添加 try-except。


def has_permission(self, request, view):
if view.action in view.MOCK_ABOVE_ACTIONS:
return False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 方法缺少返回值。建议返回 True 或调用 super().has_permission()。

EDIT_ABOVE_ACTIONS = ["update"]
MOCK_ABOVE_ACTIONS = ["preview_task_tree", "create_mock_task"]
permission_classes = [AdminPermission | SpaceSuperuserPermission | TemplatePermission | TemplateMockPermission]
MOCK_ABOVE_ACTIONS = ["create_mock_task"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ preview_task_tree 从 MOCK_ABOVE_ACTIONS 移除,但未见相关权限检查调整,请确认是否故意为之。

def template_exists(self, template_id):
return Template.exists(template_id)

def scope_exists(self, scope_data):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果使用了下划线来分给,那我们在scope_type的所有校验逻辑里,就要禁止用户传入“_”作为scope_type的值

if resource_type != ResourceType.TASK.value:
if resource_type == ResourceType.SCOPE.value:
scope_parts = db_token.resource_id.split("_")
template_obj = Template.objects.get(id=resource_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scope类型的权限,不仅仅能用于template,任务也是可以的

# Reviewed, transaction id: 66534
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查摘要

本次新增了 SCOPE 类型的 Token 资源验证功能。发现以下需要关注的问题:

🚨 严重问题 (2个)

  • scope_exists 方法中数组越界风险未充分防护
  • Token.verify 中 SCOPE 验证逻辑缺少空值校验

⚠️ 逻辑问题 (3个)

  • split("_") 未限制分割次数,可能产生意外结果
  • ScopePermission.has_permission 缺少返回值
  • 缺少针对 SCOPE 资源类型的单元测试

建议: 优先修复数组访问安全问题和补充缺失的返回值,然后增加边界情况测试覆盖。


已标注 5 条行内评论,请查看具体建议。

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查摘要

本次新增了 SCOPE 类型的 Token 资源验证功能。发现以下需要关注的问题:

🚨 严重问题 (3个)

  • scope_exists 方法中数组越界风险
  • Token.verify 中 SCOPE 验证逻辑缺少空值校验
  • ScopePermission.has_permission 缺少返回值

⚠️ 逻辑问题 (2个)

  • split('_') 未限制分割次数
  • TASK 类型验证逻辑可能被绕过

建议: 优先修复数组访问安全问题和补充缺失的返回值,然后增加边界情况测试覆盖。

if len(scope_parts) < 2:
return False

scope_type, scope_value = scope_parts[0], scope_parts[1]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 此行存在数组越界风险:当 scope_data='single' 时,len 检查通过但 scope_parts[1] 会抛出 IndexError。应在第 60 行改为 != 2。

return False

scope_parts = scope_data.split("_")
if len(scope_parts) < 2:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡ split('') 未限制分割次数,当 scope_data='a_b_c' 时会产生 3 个元素。建议使用 split('', 1) 限制最多分割成 2 部分。

if db_token.resource_id != str(resource_id):
if resource_type != ResourceType.TASK.value:
if resource_type == ResourceType.SCOPE.value:
scope_parts = db_token.resource_id.split("_")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 缺少对 db_token.resource_id 的空值和格式校验,建议在 split 前检查是否包含下划线且格式正确。

return "SCOPE"

def has_permission(self, request, view):
if view.action in view.MOCK_ABOVE_ACTIONS:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 此方法缺少 return 语句,会隐式返回 None 导致权限校验失败。应返回明确的布尔值或调用父类方法。

resource_data = result["data"]
return resource_data["scope_type"] == scope_type and resource_data["scope_value"] == scope_value
elif resource_type == ResourceType.TEMPLATE.value:
return False
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 原 TASK 类型验证逻辑被移至 elif,当 resource_type 为 TASK 且 resource_id 不匹配时会跳过 check_parent_task_id 检查,可能导致权限绕过。

@dengyh dengyh merged commit 76e4a4a into TencentBlueKing:master Dec 2, 2025
8 checks passed
Mianhuatang8 pushed a commit to Mianhuatang8/BKFlow that referenced this pull request Jan 7, 2026
* feat: 子流程功能token校验新增作用域资源 --story=127611598
# Reviewed, transaction id: 66281

* fix: 修复逻辑校验问题 --story=127611598
# Reviewed, transaction id: 66534
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants