Skip to content

fix: bedrock canonical URI sig#3590

Open
wydream wants to merge 2 commits intoalibaba:mainfrom
wydream:fix/bedrock-canonical-sig
Open

fix: bedrock canonical URI sig#3590
wydream wants to merge 2 commits intoalibaba:mainfrom
wydream:fix/bedrock-canonical-sig

Conversation

@wydream
Copy link
Collaborator

@wydream wydream commented Mar 13, 2026

Ⅰ. Describe what this PR did

本 PR 修复了 Bedrock Provider 在 c12183ca 之后引入的 SigV4 canonical URI 编码回归问题。

背景

c12183ca 主要修复了 Bedrock 在 AK/SK 模式下“只对部分 API 签名”的问题,这部分改动是正确且必要的。
但同一提交中,encodeSigV4Path 从“按段直接 PathEscape”改为“PathUnescape 后再按单编码重建”,导致已编码路径段(例如 %3A%2F)在 canonical URI 中的表达变化,进而在部分场景触发 AWS 签名校验失败。

已复现的典型模型名:

  • global.amazon.nova-2-lite-v1:0
  • arn:aws:bedrock:us-east-1:123456789012:inference-profile/global.anthropic.claude-sonnet-4-20250514-v1:0

主要变更

  1. 仅回退 canonical URI 编码策略provider/bedrock.go):

    • 保留 query 剥离逻辑(? 后内容不参与 canonical URI)
    • 移除 PathUnescape + 自定义重编码 的“单编码归一化”逻辑
    • 恢复为按路径段 url.PathEscape(seg)(历史可用行为)
  2. 删除不再需要的辅助函数provider/bedrock.go):

    • sigV4EscapePathSegment
    • isSigV4Unreserved
  3. 新增并强化 SigV4 单元测试provider/bedrock_sigv4_path_test.go):

    • 覆盖路径构造阶段、canonical URI 计算阶段、签名阶段
    • 覆盖 raw path、pre-encoded path、ARN inference-profile、query、非法 % 等边界场景
  4. 保留 c12183ca 的有效修复能力

    • 统一签名入口、original 路径签名、embeddings 等非 chat/image API 的签名覆盖能力不变

修复前后对比

场景 修复前(本次修复前) 修复后
pre-encoded model path(%3A)canonical URI 被归一化为 %3A 保留为 %253A 口径
pre-encoded ARN(含 %2F)canonical URI 被归一化为 %2F 保留为 %252F 口径
query 参与 canonical URI 已不参与(正确) 继续不参与(保持)
embeddings/original 路径 SigV4 覆盖 ✅ 已修复 ✅ 保持
global.amazon.nova-2-lite-v1:0 相关签名回归 ❌ 可能失败 ✅ 修复

Ⅱ. Does this pull request fix one issue?

修复了一个 SigV4 回归问题:
c12183ca 中 canonical URI 编码策略调整导致部分 Bedrock model path 与 AWS 服务端计算口径不一致,出现 The request signature we calculated does not match the signature you provided

Ⅲ. Why don't you add test cases (unit test/integration test)?

已新增并执行单元测试(provider/bedrock_sigv4_path_test.go):

  • TestEncodeSigV4Path
    • raw model id(含 :
    • pre-encoded model id(%3A
    • raw/pre-encoded inference-profile ARN(含 /
    • 带 query 的路径
    • 非法 % 转义路径
  • TestOverwriteRequestPathHeaderPreservesSingleEncodedRequestPath
    • 验证 overwriteRequestPathHeader 在 plain model 和 pre-encoded model 场景都不会把转发路径双重编码
  • TestGenerateSignatureIgnoresQueryStringInCanonicalURI
    • 验证 query 不影响签名结果
  • TestGenerateSignatureDiffersForRawAndPreEncodedModelPath
    • 验证 raw 与 pre-encoded 输入路径确实对应不同 canonical URI/签名

同时执行了回归:

  • go test -gcflags="all=-N -l" ./provider
  • go test -gcflags="all=-N -l" -run TestBedrock ./...

Ⅳ. Describe how to verify it

方式一:运行单元测试

cd plugins/wasm-go/extensions/ai-proxy
go test -gcflags="all=-N -l" -v ./provider
go test -gcflags="all=-N -l" -v -run TestBedrock ./...

方式二:手动验证

  1. 使用 Bedrock AK/SK 配置,发送 Chat Completion 请求,model 分别使用:
    • global.amazon.nova-2-lite-v1:0
    • arn:aws:bedrock:us-east-1:123456789012:inference-profile/global.anthropic.claude-sonnet-4-20250514-v1:0
  2. 验证请求头包含:
    • Authorization: AWS4-HMAC-SHA256 ...
    • X-Amz-Date: ...
  3. 验证不再出现 canonical URI 相关签名报错(尤其是 %3A/%253A%2F/%252F 口径场景)。

Ⅴ. Special notes for reviews

  1. 这是“定点修复”:只调整 canonical URI 编码策略,不回退 c12183ca 的签名覆盖修复。
  2. query 处理保持正确:canonical URI 仍只使用 path,不包含 query。
  3. 兼容性取向:编码行为回到历史可用口径,优先保证已验证通过的生产请求路径。
  4. 测试数据脱敏:ARN 用例中的账号 ID 使用占位值 123456789012

Ⅵ. AI Coding Tool Usage Checklist (if applicable)

Please check all applicable items:

  • For regular updates/changes (not new plugins):
    • I have included the AI Coding summary below

AI Coding Summary

问题根因:

  1. c12183caencodeSigV4Path 改为“单编码归一化”(先解码再重编码)
  2. 对已编码路径段(%3A%2F)产生 canonical URI 口径变化
  3. 在部分 Bedrock model path 下与 AWS 服务端校验不一致,导致签名失败

修复方案:

  1. 保留 query 剥离
  2. 回退 canonical URI 路径段编码到 url.PathEscape(seg)
  3. 移除单编码归一化辅助函数
  4. 增补分层测试覆盖路径构造、canonical URI、签名及边界输入

影响范围:

  1. provider/bedrock.goencodeSigV4Path 逻辑回退并清理相关辅助函数
  2. provider/bedrock_sigv4_path_test.go:新增/强化 SigV4 canonical URI 与签名测试

Ⅰ. Describe what this PR did

This PR fixes a SigV4 canonical URI encoding regression introduced by Bedrock Provider after c12183ca.

Background

c12183ca mainly fixes the problem of "only signing some APIs" in Bedrock in AK/SK mode. This part of the change is correct and necessary.
However, in the same submission, encodeSigV4Path was changed from "directly PathEscape by segment" to "PathUnescape and then reconstructed by single encoding", resulting in changes in the expression of encoded path segments (such as %3A, %2F) in the canonical URI, which in turn triggered AWS signature verification failure in some scenarios.

Typical model names that have been reproduced:

  • global.amazon.nova-2-lite-v1:0
  • arn:aws:bedrock:us-east-1:123456789012:inference-profile/global.anthropic.claude-sonnet-4-20250514-v1:0

Major changes

  1. Fallback only canonical URI encoding strategy (provider/bedrock.go):

    • Retain query stripping logic (content after ? does not participate in canonical URI)
    • Removed "single encoding normalization" logic of PathUnescape + Custom Recoding
    • Revert to path segment url.PathEscape(seg) (historically available behavior)
  2. Remove helper functions that are no longer needed (provider/bedrock.go):

    • sigV4EscapePathSegment
    • isSigV4Unreserved
  3. Added and enhanced SigV4 unit test (provider/bedrock_sigv4_path_test.go):

    • Covers the path construction phase, canonical URI calculation phase, and signature phase
    • Covers boundary scenarios such as raw path, pre-encoded path, ARN inference-profile, query, illegal %, etc.
  4. Retain the effective repair capability of c12183ca:

    • The signature coverage capabilities of non-chat/image APIs such as unified signature entry, original path signature, and embeddings remain unchanged.

Comparison before and after repair

Scene Before restoration (before this restoration) After restoration
pre-encoded model path (%3A) canonical URI normalized to %3A retained as %253A caliber
pre-encoded ARN (containing %2F) canonical URI normalized to %2F retained as %252F caliber
query participate canonical URI no longer participate (correct) continue not to participate (keep)
embeddings/original path SigV4 override ✅ Fixed ✅ Maintained
global.amazon.nova-2-lite-v1:0 related signature regression ❌ May fail ✅ Fix

Ⅱ. Does this pull request fix one issue?

Fixed a SigV4 regression:
The adjustment of the canonical URI encoding strategy in c12183ca caused some Bedrock model paths to be inconsistent with the AWS server calculation caliber, and The request signature we calculated does not match the signature you provided appeared.

Ⅲ. Why don't you add test cases (unit test/integration test)?

Unit test has been added and executed (provider/bedrock_sigv4_path_test.go):

  • TestEncodeSigV4Path
    • raw model id (including :)
    • pre-encoded model id (%3A)
    • raw/pre-encoded inference-profile ARN (including /)
    • path with query
    • Illegal % escape path
  • TestOverwriteRequestPathHeaderPreservesSingleEncodedRequestPath
    • Verify that overwriteRequestPathHeader will not double-encode the forwarding path in plain model and pre-encoded model scenarios.
  • TestGenerateSignatureIgnoresQueryStringInCanonicalURI
    • Verify query does not affect signature results
  • TestGenerateSignatureDiffersForRawAndPreEncodedModelPath
    • Verify that the raw and pre-encoded input paths indeed correspond to different canonical URIs/signatures

Regression was also performed:

  • go test -gcflags="all=-N -l" ./provider
  • go test -gcflags="all=-N -l" -run TestBedrock ./...

Ⅳ. Describe how to verify it

Method 1: Run unit tests

cd plugins/wasm-go/extensions/ai-proxy
go test -gcflags="all=-N -l" -v ./provider
go test -gcflags="all=-N -l" -v -run TestBedrock ./...

Method 2: Manual verification

  1. Use Bedrock AK/SK configuration, send Chat Completion request, use model respectively:
    • global.amazon.nova-2-lite-v1:0
    • arn:aws:bedrock:us-east-1:123456789012:inference-profile/global.anthropic.claude-sonnet-4-20250514-v1:0
  2. Verify that the request header contains:
    • Authorization: AWS4-HMAC-SHA256 ...
    • X-Amz-Date: ...
  3. Verification no longer causes errors related to canonical URI signatures (especially %3A/%253A and %2F/%252F caliber scenarios).

Ⅴ. Special notes for reviews

  1. This is a "fixed point fix": only adjust the canonical URI encoding strategy and do not roll back the signature coverage fix of c12183ca.
  2. query processing remains correct: canonical URI still only uses path and does not contain query.
  3. Compatibility Orientation: The encoding behavior returns to the historical available caliber, giving priority to the verified production request path.
  4. Test data desensitization: The account ID in the ARN use case uses the placeholder value 123456789012.

Ⅵ. AI Coding Tool Usage Checklist (if applicable)

Please check all applicable items:

  • For regular updates/changes (not new plugins):
    • I have included the AI Coding summary below

AI Coding Summary

Root cause of the problem:

  1. c12183ca changes encodeSigV4Path to "single encoding normalization" (decode first and then re-encode)
  2. Generate canonical URI caliber changes for encoded path segments (%3A, %2F)
  3. Some Bedrock model paths are inconsistent with AWS server verification, resulting in signature failure.

Fix:

  1. Keep query stripping
  2. Fallback canonical URI path segment encoding to url.PathEscape(seg)
  3. Remove single-code normalization auxiliary function
  4. Add layered testing to cover path construction, canonical URI, signature and boundary input

Scope of influence:

  1. provider/bedrock.go: encodeSigV4Path logic rolls back and cleans up related auxiliary functions
  2. provider/bedrock_sigv4_path_test.go: Add/enhance SigV4 canonical URI and signature test

Change-Id: I22df4c38bc6150c54ecf2f75d49108073e964623
@wydream wydream requested review from johnlanni and rinfx as code owners March 13, 2026 05:27
@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Change-Id: If25ed4556b47e92d748521a80f126ce4a1a8a2b3
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