强制 Barbican REST API 的内容类型¶
包含您的 Launchpad 蓝图的 URL
https://blueprints.launchpad.net/barbican/+spec/barbican-enforce-content-type
在 Barbican 从 Falcon 迁移到 Pecan 之前,内容类型会被强制转换为 application/json。这意味着用户可以使用 curl 等工具,省略内容类型,调用仍然会成功。迁移到 Pecan 后,默认 content-type 更改为 application/x-www-form-urlencoded。这意味着没有指定 content-type 的 curl 请求最终会以 urlencoded 字符串的形式进入 Barbican,并在 JSON 化过程中失败。
问题描述¶
由于从 Falcon 迁移到 Pecan,请求的默认内容类型已从 application/json 更改为 application/xxx-www-form-urlencoded。这意味着以前在使用 Falcon 实现时有效的、没有 content-type 的调用,现在在使用 Pecan 实现时将失败。
如果调用者将其 content-type 标头设置为 application/json,则一切正常。
但是,如果调用者省略了 content-type 标头,则现在默认设置为 application/xxx-www-form-urlencoded。这意味着 Pecan 将 URL 编码请求体,这会导致 Barbican 在尝试将其 JSON 化时失败。
提议的变更¶
建议的更改是检查以下类型的 Barbican 请求中的 content-type 标头,并拒绝任何未指定正确值的请求(通过 Pecan abort,HTTP 415)。
Resource |
HTTP 方法 |
强制内容类型 |
|---|---|---|
Secret |
PUT |
application/octet-stream text/plain |
密钥 |
POST |
application/json |
Orders |
POST |
application/json |
容器 |
POST |
application/json |
TransportKeys |
POST |
application/json |
备选方案¶
存在替代建议更改的方案
将 content-type 标头更改为正确的值。这将解决上述问题,但可能会产生意想不到的后果,因为用户输入的数据将与他们提供/期望的不同。
检测 content-type 采用默认值的情况,并通过
URL 解码字符串
清除可能添加的任何填充字符
数据模型影响¶
无。
REST API 影响¶
以下资源的用戶需要在使用 REST API 调用时在 content-type 标头中提供有效值,如下所示
Resource |
HTTP 方法 |
所需内容类型 |
|---|---|---|
Secret |
PUT |
application/octet-stream 或 text/plain |
密钥 |
POST |
application/json |
Orders |
POST |
application/json |
容器 |
POST |
application/json |
TransportKeys |
POST |
application/json |
安全影响¶
无。
通知与审计影响¶
无。
其他最终用户影响¶
无。
性能影响¶
现在,每个 REST 调用都会检查 content-type。没有正确 content-type 的调用将使用 pecan.abort 失败,并返回 HTTP 415。
其他部署者影响¶
无。
开发人员影响¶
使用 curl 等工具的开发人员需要确保在 HTTP 标头中传递 content-type:application/json,否则将以 HTTP 415 失败。
实现¶
负责人¶
- 主要负责人
sheyman
- 其他贡献者
john-wood-w arunkant-uws
工作项¶
此更改需要 3 个工作项
更新 Barbican 代码以检测不正确的内容类型并使用 HTTP 415 进行 Pecan abort
更新 Barbican 文档以描述此更改并提供恢复操作
更新 Barbican 测试以验证新代码的行为。这包括单元测试和功能测试。
依赖项¶
无。
测试¶
对于建议的更改,需要以下正向测试场景
Resource |
动词 |
Content-Type |
预期结果 |
|---|---|---|---|
Secret |
PUT |
application/octet-stream |
成功 |
Secret |
PUT |
text/plain |
成功 |
密钥 |
POST |
application/json |
成功 |
Orders |
POST |
application/json |
成功 |
容器 |
POST |
application/json |
成功 |
Transport Keys |
POST |
application/json |
成功 |
此外,将使用以下负向测试来验证行为
Resource |
动词 |
Content-Type |
预期结果 |
|---|---|---|---|
Secret |
PUT |
无(省略) |
HTTP 415 |
Secret |
PUT |
application/octet-streamx |
HTTP 415 |
Secret |
PUT |
text/plainx |
HTTP 415 |
Secret |
PUT |
applicationx/octet-stream |
HTTP 415 |
Secret |
PUT |
textx/plain |
HTTP 415 |
Secret |
PUT |
application/json |
HTTP 415 |
Resource |
动词 |
Content-Type |
预期结果 |
|---|---|---|---|
密钥 |
POST |
无(省略) |
HTTP 415 |
密钥 |
POST |
text/plain |
HTTP 415 |
密钥 |
POST |
application/octet-stream |
HTTP 415 |
密钥 |
POST |
application/jsonx |
HTTP 415 |
密钥 |
POST |
applicationx/json |
HTTP 415 |
密钥 |
POST |
application/jsonx |
HTTP 415 |
Resource |
动词 |
Content-Type |
预期结果 |
|---|---|---|---|
Orders |
POST |
无(省略) |
HTTP 415 |
Orders |
POST |
text/plain |
HTTP 415 |
Orders |
POST |
application/octet-stream |
HTTP 415 |
Orders |
POST |
application/jsonx |
HTTP 415 |
Orders |
POST |
applicationx/json |
HTTP 415 |
Orders |
POST |
application/jsonx |
HTTP 415 |
Resource |
动词 |
Content-Type |
预期结果 |
|---|---|---|---|
容器 |
POST |
无(省略) |
HTTP 415 |
容器 |
POST |
text/plain |
HTTP 415 |
容器 |
POST |
application/octet-stream |
HTTP 415 |
容器 |
POST |
application/jsonx |
HTTP 415 |
容器 |
POST |
applicationx/json |
HTTP 415 |
容器 |
POST |
application/jsonx |
HTTP 415 |
Resource |
动词 |
Content-Type |
预期结果 |
|---|---|---|---|
Transport Keys |
POST |
无(省略) |
HTTP 415 |
Transport Keys |
POST |
text/plain |
HTTP 415 |
Transport Keys |
POST |
application/octet-stream |
HTTP 415 |
Transport Keys |
POST |
application/jsonx |
HTTP 415 |
Transport Keys |
POST |
applicationx/json |
HTTP 415 |
Transport Keys |
POST |
application/jsonx |
HTTP 415 |
文档影响¶
文档需要说明 content-type 标头现在是上述“建议更改”下的 API/方法所必需的。省略 content-type 或 content-type 与允许的值不同将导致 HTTP 415。
注意:当前文档指出 secret PUT “应该”包含适当的 content-type。 这将需要更新为“必须”。 请参阅 https://github.com/cloudkeep/barbican/wiki/Application-Programming-Interface#put
参考资料¶
以下 bug 和建议的修复导致了此蓝图的创建