3

示例端点:

https://localhost:4200/test/index.html?param1=username%2passw0rd&baseApi=https://someOtherService/APIBase.jsp?INT/

当我单击上面的端点时。我的网址被转换成这个......

https://localhost:4200/test/index.html?param1=username%2passw0rd&baseApi=https:%2F%2FsomeOtherService%2FAPIBase.jsp

正如您在 baseApi 参数中看到的那样,“/”被转换为“%2F”,并且值“?INT/”也被删除了。

如何防止 URL 转换“/”并为第二个参数“baseApi”中的值保留“?INT/”值。

参考

4

2 回答 2

11

你没有。如果允许,它将破坏互联网。

更多细节:

  1. 您可以在后端 API 上使用 URL 解码将 URL 转换回标准文本。
  2. 永远不要在 URL 中传递密码。这实际上是您可以做的最不安全的事情之一。

关于第 1 点:情况并非总是如此,许多 API 框架会自动解码查询字符串。

如果您需要传递完整的 URL,请确保它已正确编码:

console.log("https://localhost:4200/test/index.html?" + encodeURIComponent("param1=username%2passw0rd&baseApi=https://someOtherService/APIBase.jsp?INT/"));

https://localhost:4200/test/index.html?param1%3Dusername%252passw0rd%26baseApi%3Dhttps%3A%2F%2FsomeOtherService%2FAPIBase.jsp%3FINT%2F

于 2020-08-25T14:10:19.880 回答
4

您不能也不应该试图阻止特殊字符被转换。正确的方法是在发送用户之前故意对它们进行编码,然后在读取数据之前对其进行解码。如果您使用的是 javascript,那么您可以通过encodeURIComponent(string)和执行此操作decodeURIComponent(string)

encodeURIComponent()将接受任何字符串,并将其转换为对 URL 友好的格式。

decodeURIComponent()将采用转换后的字符串,并将其更改回常规格式。

至于你不应该试图阻止它们被转换的原因......我将在这里引用 Mozilla:

For example, if a user writes "Jack & Jill", using encodeURIComponent the text will get encoded as "Jack %26 Jill". Without encoding, the ampersand could be interpreted on the server as the start of a new field and jeopardize the integrity of the data.

换句话说,假设您在 URL 中允许使用符号,并且有人创建了一个名为“ Jack&baseApi=evilURL ”的用户名。您的网址最终会是

http://localhost:4200?username=Jack& baseApi=evilURL & baseApi =https://someOtherService

而且我不想站在你的服务器上,试图猜测正确的 baseApi 是“evilURL”还是“someOtherService”。

于 2020-09-07T19:45:56.887 回答