依赖 URL 查询参数的顺序是否合法或安全?具体来说,我是否能够编写代码,并相信该代码将始终有效,并且基于这两个查询字符串具有不同的行为:
?a=10&add=5&multiply=3 # might mean (10 + 5) * 3
?a=10&multiply=3&add=5 # might mean (10 * 3) + 5
(我的例子当然是人为的,我知道构建这样的计算器是荒谬的。:))
最佳答案
这些查询字符串既完全合法又截然不同。根据RFC 3986 ,查询字符串无非就是
...non-hierarchical data that... serves to identify a resource within the scope of the URI's scheme and naming authority
关于 HTML 表单必须如何以 application/x-www-form-urlencoded
格式生成 key=value
对的规则详述 in the W3C HTML spec .特别感兴趣的是如何解析 application/x-www-form-urlencoded
内容的规则:
To decode
application/x-www-form-urlencoded
payloads, the following algorithm should be used....The output of this algorithm is a sorted list of name-value pairs.
因此,application/x-www-form-urlencoded
查询字符串内容可以被正确地视为键/值对的排序列表。
但是,请记住并非所有 Web 框架都捕获此信息。他们可能会为您提供从查询字符串中解析的属性名称和值的无序字典。例如,url.parse
Node.js 中的方法将解析后的查询字符串作为对象返回(其属性是键/值对),而 JavaScript 对象始终是无序的。
关于http - 依赖 URL 查询参数的顺序是否合法或安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25687101/