简体   繁体   English

如何在 WSO2 EI 上的 HTTP 请求中转义 URL 不安全字符

[英]How to escape URL unsafe characters in HTTP request on WSO2 EI

Is it possible to escape unsafe URL characters in WSO2?是否可以在 WSO2 中转义不安全的 URL 字符? For example if WSO2 is receiving a request with a request URL https://example.com/something?token=...&imageFormat=image/png .例如,如果 WSO2 正在接收请求 URL https://example.com/something?token=...&imageFormat=image/png The slash "/" symbol in the query parameters is not safe, but I don't care, I just want to proxy this request to another server, but WSO2 returns a Error 404--Not Found , probably because the slash in the query parameters confused it.查询参数中的斜杠“/”符号是不安全的,不过我不在乎,我只是想把这个请求代理到另一台服务器,但是 WSO2 返回一个Error 404--Not Found ,可能是因为查询中的斜杠参数把它弄糊涂了。

However the request is rejected far before any mediator processing takes place, so I can't do anything about it.但是,在任何调解器处理发生之前,该请求就被拒绝了,所以我对此无能为力。 Is there any way to escape incoming request URL's?有什么方法可以逃避传入的请求 URL?

I don't think it's the query param that's causing the issue.我不认为是导致问题的查询参数。 Generally whatever is after the ?通常在 ? 之后是? is ignored for context matching.在上下文匹配中被忽略。 I tried this with a simple API and it worked without any issues.我用一个简单的 API 尝试了这个,它没有任何问题。 See the sample below.请参阅下面的示例。

API API

<?xml version="1.0" encoding="UTF-8"?>
<api context="/sample" name="Sample" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET">
        <inSequence>
            <property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>
            <payloadFactory media-type="xml">
                <format>
                    <msg xmlns="">Hello</msg>
                </format>
                <args/>
            </payloadFactory>
            <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

WireLog有线日志

HTTP-Listener I/O dispatcher-5 >> "GET /sample?token=12345&imageFormat=image/png HTTP/1.1[\r][\n]"
HTTP-Listener I/O dispatcher-5 >> "User-Agent: PostmanRuntime/7.29.0[\r][\n]"
HTTP-Listener I/O dispatcher-5 >> "Accept: */*[\r][\n]"
HTTP-Listener I/O dispatcher-5 >> "Postman-Token: f5e87376-f8b1-4722-b068-6513314de9ab[\r][\n]"
HTTP-Listener I/O dispatcher-5 >> "Host: localhost:8280[\r][\n]"
HTTP-Listener I/O dispatcher-5 >> "Accept-Encoding: gzip, deflate, br[\r][\n]"
HTTP-Listener I/O dispatcher-5 >> "Connection: keep-alive[\r][\n]"
HTTP-Listener I/O dispatcher-5 >> "[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "HTTP/1.1 200 OK[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "Content-Type: application/json; charset=UTF-8[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "Date: Tue, 30 Aug 2022 13:00:01 GMT[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "Transfer-Encoding: chunked[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "Connection: keep-alive[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "f[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "{"msg":"Hello"}[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "0[\r][\n]"
HTTP-Listener I/O dispatcher-5 << "[\r][\n]"

As you can see above I'm doing the following call and it works as expected.正如您在上面看到的,我正在执行以下调用,它按预期工作。

curl 'http://localhost:8280/sample?token=12345&imageFormat=image/png'

Workaround解决方法

If you are sure that the error is caused by the slash in the query param, here is a workaround.如果您确定错误是由查询参数中的斜杠引起的,这里有一个解决方法。 All the service calls that are not matched will be dispatched to the default main sequence.所有不匹配的服务调用将被分派到默认的main序列。 You can find this sequence by navigating to <EI_HOME>/repository/deployment/server/synapse-configs/default/sequences or going to Sequences section from the carbon console.您可以通过导航到<EI_HOME>/repository/deployment/server/synapse-configs/default/sequences或从 carbon 控制台转到Sequences部分来找到此序列。 In your main sequence, you can add some logic like the below, to check if this is your API that's failing and route everything to a different sequence or you can even invoke the original API after doing URL encoding from the main sequence.在您的主序列中,您可以添加一些如下所示的逻辑,以检查这是否是您的 API 失败并将所有内容路由到不同的序列,或者您甚至可以在从主序列进行 ZE6B391A8D2C4D45902A23A8B65 编码后调用原始 API。 In the below example the root context of my API is Sample .在下面的示例中,我的 API 的根上下文是Sample

<filter regex="true" source="starts-with(get-property('To'), '/sample')" xmlns:ns="http://org.apache.synapse/xsd">
    <then>
        <log>
            <property name="msg" value="Weird API Matched ~~~~~~Do your stuff here or invoke another sequence~~~~~~~"/>
        </log>
        <sequence key="Seq"/>
    </then>
    <else>
        <log>
            <property name="msg" value="No Match ~~~~~~~~~~~~~~~~~"/>
        </log>
    </else>
</filter>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM