What is the semicolon reserved for in URLs?

前端 未结 6 914
梦谈多话
梦谈多话 2020-12-05 04:42

The RFC 3986 URI: Generic Syntax spec lists a semicolon as a reserved (sub-delim) character:

reserved    = gen-delims / sub-delims

gen-delims  = \":\" / \"/         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-05 05:08

    Since 2014 path segments are known to contribute to Reflected File Download attacks. Let's assume we have a vulnerable API that reflects whatever we send to it (the URL was real apparently, now fixed):

    https://google.com/s?q=rfd%22||calc||
    
    {"results":["q", "rfd\"||calc||","I love rfd"]}
    

    Now, this is harmless in a browser as it's JSON so it's not going to be rendered but the browser will rather offer to download the response as a file. Now here's the path segments come to help (for the attacker):

    https://google.com/s;/setup.bat;?q=rfd%22||calc||
    

    Everything between semicolons (;/setup.bat;) will be not sent to the web service, but instead the browser will interpret it as the file name... to save the API response. Now, a file called setup.bat will be downloaded and run without asking about dangers of running files downloaded from Internet (because it contains the word "setup" in its name). The contents will be interpreted as Windows batch file, and the calc.exe command will be run.

    Prevention:

    • sanitize your API's input (in this case they should just allow alphanumerics); escaping is not sufficient
    • add Content-Disposition: attachment; filename="whatever.txt" on APIs that are not going to be rendered; Google was missing the filename part which actually made the attack easier
    • add X-Content-Type-Options: nosniff header to API responses

提交回复
热议问题