日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

tomcat对URL合法字符的判断(RFC 7230 and RFC 3986 异常排查)

發(fā)布時(shí)間:2025/3/20 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tomcat对URL合法字符的判断(RFC 7230 and RFC 3986 异常排查) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??

  • 起因

有一個(gè)數(shù)據(jù)上報(bào)接口,之前在物理機(jī)上部署,數(shù)據(jù)上報(bào)正常。

最近將項(xiàng)目遷移到 docker 中,結(jié)果出現(xiàn)了異常如下:

Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level. java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986at org.apache.coyote.http11.InternalInputBuffer.parseRequestLine(InternalInputBuffer.java:192)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1028)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:745)
  • 問題原因

問題排查過程:略

原因:請(qǐng)求中出現(xiàn)了字符"{"(示例請(qǐng)求)

https://127.0.0.1:8080/metric?data=[{%22app

在不更改接口數(shù)據(jù)的前提下,解決方案為:在tomcat的cataline.properties追加配置

tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}

官方文檔鏈接:http://tomcat.apache.org/tomcat-8.0-doc/config/systemprops.html#Other

tomcat.util.http.parser.HttpParser. requestTargetAllow

A string comprised of characters the server should allow even when they are not encoded. These characters would normally result in a 400 status.

The acceptable characters for this property are:?|,?{?, and?}

WARNING: Use of this option will expose the server to CVE-2016-6816.

If not specified, the default value of?null?will be used.

  • 問題來源

那么為什么之前接口不會(huì)出現(xiàn)異常呢?是tomcat版本不同導(dǎo)致的。

查看源碼后發(fā)現(xiàn):org.apache.tomcat.util.http.parser.HttpParser

tomcat 8.2.3 版本及 tomcat 7.0.82 ,都有如下代碼,讀取配置

String prop = System.getProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow"); if (prop != null) {for (int i = 0; i < prop.length(); i++) {char c = prop.charAt(i);if (c == '{' || c == '}' || c == '|') {REQUEST_TARGET_ALLOW[c] = true;} else {log.warn(sm.getString("httpparser.invalidRequestTargetCharacter",Character.valueOf(c)));}} }

而tomcat 8.0.14 版本中并沒有讀取配置,對(duì) |?{?}?的處理,而是默認(rèn)為合法字符。

static {// Setup the flag arraysfor (int i = 0; i < 128; i++) {if (i < 32) {isToken[i] = false;} else if (i == '(' || i == ')' || i == '<' || i == '>' || i == '@' ||i == ',' || i == ';' || i == ':' || i == '\\' || i == '\"' ||i == '/' || i == '[' || i == ']' || i == '?' || i == '=' ||i == '{' || i == '}' || i == ' ' || i == '\t') {isToken[i] = false;} else {isToken[i] = true;}if (i >= '0' && i <= '9' || i >= 'A' && i <= 'F' ||i >= 'a' && i <= 'f') {isHex[i] = true;} else {isHex[i] = false;}} }

雖然沒有進(jìn)行全面的比對(duì),但可以看出在 8.0.x 左右的一些版本中,tomcat.util.http.parser.HttpParser. requestTargetAllow?這個(gè)配置是沒有生效的,即? |?{?}?這3個(gè)符號(hào)認(rèn)為是合法的。

  • 結(jié)論:
  • tomcat.util.http.parser.HttpParser. requestTargetAllow?這個(gè)字段可以解決URL中存在 |?{?}?字符的問題。
  • tomcat自tomcat 8.0.35版本之后對(duì)URL參數(shù)做了比較規(guī)范的限制,必須遵循RFC 7230 and RFC 3986規(guī)范,對(duì)于非保留字字符(json格式的請(qǐng)求參數(shù))必須做轉(zhuǎn)義操作。
  • ?

    ?

    ?

    ?

    ?

    轉(zhuǎn)載于:https://my.oschina.net/pding/blog/1794176

    總結(jié)

    以上是生活随笔為你收集整理的tomcat对URL合法字符的判断(RFC 7230 and RFC 3986 异常排查)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。