Skip to content

Conversation

@HecarimV
Copy link
Contributor

@HecarimV HecarimV commented Jun 12, 2025

Ⅰ. Describe what this PR did

fix bedrock Sigv4 mismatch
ref: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html

URL Encoding Rules for Canonical String in AWS SigV4
Path Encoding (Canonical URI)
  • Encode all characters except unreserved characters (A-Z, a-z, 0-9, -, _, ., ~) and reserved forward slashes (/).
  • Example: /my path/my%20path (space encoded as %20, not +).
  • Preserve forward slashes (/) without encoding
  • Always use uppercase for encoded characters (e.g., %2F instead of %2f).

Ⅱ. Does this pull request fix one issue?

fixes: #2396

@HecarimV HecarimV requested review from CH3CHO, cr7258 and rinfx as code owners June 12, 2025 01:46
@lingma-agents
Copy link

lingma-agents bot commented Jun 12, 2025

修复 AWS Bedrock Sigv4 签名路径编码问题并添加配置文档支持

变更概述

新功能

  • AWS Bedrock 配置文档新增
    README.mdREADME_EN.md 中添加了 AWS Bedrock 的配置字段说明和使用示例:
    • 新增 awsAccessKeyawsSecretKeyawsRegion 配置项
    • 提供 YAML 配置示例和 JSON 请求/响应示例
    • 明确说明 Bedrock 服务接入的完整流程

问题修复

  • Sigv4 签名路径编码修复
    provider/bedrock.go 中:
    • 新增 encodeSigV4Path 函数强制对路径分段进行 URL 编码(url.PathEscape
    • 修正路径编码规则:空格转为 %20、保留 / 不编码、使用大写十六进制表示
    • 修复因路径编码不规范导致的签名验证失败问题

安全增强

  • 模型 ID 解码增强
    buildChatCompletionResponse 方法中:
    • ctxKeyFinalRequestModel 的值进行 URL 解码(url.QueryUnescape
    • 确保模型 ID 中的特殊字符正确解析,避免因未解码导致的路径匹配错误

文档更新

  • 中文/英文文档同步
    双语 README 文件均新增 Bedrock 配置章节,包含:
    • 配置参数说明表格
    • 完整配置示例
    • 请求/响应格式示例
变更统计
  • 📁 修改文件: 3 个文件

  • ✅ 新增代码: +136 行

  • ❌ 删除代码: -1 行

变更文件
文件路径 变更说明
plugins/​wasm-go/​extensions/​ai-proxy/​README.​md 新增 AWS Bedrock 配置说明、使用示例和响应格式文档
plugins/​wasm-go/​extensions/​ai-proxy/​README_​EN.​md 新增英文版 AWS Bedrock 配置说明和使用示例
plugins/​wasm-go/​extensions/​ai-proxy/​provider/​bedrock.​go 修复 Sigv4 签名路径编码逻辑,新增 URL 编码处理函数

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

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

🔎 代码评审报告

🎯 评审意见概览
严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 0 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 1 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 0 次要问题,酬情优化。例如:代码格式不规范或注释缺失。

总计: 1 个问题

📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
🔹 plugins/wasm-go/extensions/ai-proxy/provider/bedrock.go (1 💬)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. AWS凭证配置缺少安全存储建议

在README中文和英文文档新增的Bedrock配置示例中,直接展示了明文AWS访问密钥和秘密密钥。项目未提供敏感凭证的存储安全建议(如环境变量、密钥管理服务等),可能导致用户将敏感信息以明文形式存储在配置文件中,存在安全风险。建议补充安全存储凭证的最佳实践说明。

📌 关键代码

| `awsAccessKey` | string | 必填   | -   | AWS Access Key,用于身份认证        |
| `awsSecretKey` | string | 必填   | -   | AWS Secret Access Key,用于身份认证 |
| `awsAccessKey` | string    | Required    | -       | AWS Access Key used for authentication        |
| `awsSecretKey` | string    | Required    | -       | AWS Secret Access Key used for authentication |

⚠️ 潜在风险

用户可能将敏感凭证以明文形式存储在配置文件或版本库中,导致AWS账户被非法访问。

🔍2. 路径编码逻辑存在架构不一致性

在bedrock.go中新增的encodeSigV4Path函数对路径分段进行URL编码,但其他云服务提供商(如Azure/OpenAI)的签名实现未采用统一的路径处理规范。需确认该编码逻辑是否符合AWS Sigv4规范要求,并评估是否需要在所有需要签名的服务中建立统一的路径编码标准。

📌 关键代码

func encodeSigV4Path(path string) string {
...
segments[i] = url.PathEscape(seg)
...}

⚠️ 潜在风险

若编码方式与其他服务不一致,可能导致签名验证失败或出现安全漏洞。

🔍3. 模型ID解码未处理异常情况

在bedrock.go的buildChatCompletionResponse函数中,新增的modelId解码操作直接忽略错误返回值(_ := url.QueryUnescape),未处理解码失败场景。若模型ID包含无法解码的字符,可能导致空值或无效模型标识,引发后续接口调用异常。

📌 关键代码

modelId, _ := url.QueryUnescape(ctx.GetStringContext(ctxKeyFinalRequestModel, ""))

⚠️ 潜在风险

模型ID解码失败时可能导致空指针异常或调用错误服务端点。

🔍4. 配置参数未实施校验机制

新增的AWS配置参数(awsRegion等)在配置加载时未添加有效性校验。例如未验证区域是否属于AWS有效区域列表,可能导致配置错误后服务无法正常工作。建议在配置加载阶段增加参数合法性校验。

📌 关键代码

| `awsRegion`    | string | 必填   | -      | AWS 区域,例如:us-east-1          |

⚠️ 潜在风险

无效区域配置可能导致服务调用失败且错误提示不明确,增加运维排查难度。

审查详情
📒 文件清单 (3 个文件)
📝 变更: 3 个文件

📝 变更文件:

  • plugins/wasm-go/extensions/ai-proxy/README.md
  • plugins/wasm-go/extensions/ai-proxy/README_EN.md
  • plugins/wasm-go/extensions/ai-proxy/provider/bedrock.go

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

},
}
requestId := ctx.GetStringContext(requestIdHeader, "")
modelId, _ := url.QueryUnescape(ctx.GetStringContext(ctxKeyFinalRequestModel, ""))
Copy link

Choose a reason for hiding this comment

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

修复modelId解码逻辑的潜在数据污染风险

🟡 Major | 🐞 Bugs

📋 问题详情

在buildChatCompletionResponse函数中,modelId使用url.QueryUnescape解码时忽略了错误返回值。如果原始值无法解码,可能导致不可预测的模型ID值,引发后续API调用失败。

💡 解决方案

需要处理解码失败情况,建议:

-modelId, _ := url.QueryUnescape(ctx.GetStringContext(ctxKeyFinalRequestModel, ""))
+modelId, err := url.QueryUnescape(ctx.GetStringContext(ctxKeyFinalRequestModel, ""))
+if err != nil {
+    modelId = ctx.GetStringContext(ctxKeyFinalRequestModel, "") // 使用原始值作为回退
+}

确保在解码失败时使用原始字符串,避免空值或错误值导致模型调用失败。


您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 46.05%. Comparing base (ef31e09) to head (126b9d3).
Report is 556 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2402       +/-   ##
===========================================
+ Coverage   35.91%   46.05%   +10.14%     
===========================================
  Files          69       81       +12     
  Lines       11576    13018     +1442     
===========================================
+ Hits         4157     5995     +1838     
+ Misses       7104     6678      -426     
- Partials      315      345       +30     

see 78 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

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

LGTM

@johnlanni johnlanni merged commit 38068ee into alibaba:main Jun 12, 2025
15 checks passed
@lingma-agents lingma-agents bot mentioned this pull request Jul 18, 2025
ink-hz pushed a commit to ink-hz/higress-ai-capability-auth that referenced this pull request Nov 5, 2025
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.

[bug] AWS Bedrock signature mismatch when using modelId with colon (:) — path is over-encoded

3 participants