trikko / serverino

Small and ready-to-go http server, in D
https://trikko.github.io/serverino/
MIT License
47 stars 2 forks source link

Blank space on the end of Content-disposition #10

Closed m1dok closed 12 months ago

m1dok commented 12 months ago

If I set a blank space on the end of content-Disposition header in multipart/form-data, serverino throws a core.exception.ArrayIndexError.

In line 521 of interfaces, the .strip method is used without checking that the array is empty.

auto k = f[0].strip; auto v = f[1].strip;

I modified a previous test to reproduce the error, just add the following test:


   //Blank space on Content-Disposition in multipart/form-data

      {
      string content;
      string postBody =
"-----------------------------blahblahblah\r
Content-Disposition: form-data; name=\"field1\"\r
\r
first value\r
-----------------------------blahblahblah\r
Content-Disposition: form-data; name=\"field2\"\r
\r
second value\r
-----------------------------blahblahblah\r
Content-Disposition: form-data; name=\"myfile\"; filename=\"file1.txt\";\"   \"\r
Content-Type: application/json     \r
\r
{}
\r
-----------------------------blahblahblah--\r
";

      auto http = HTTP("http://myuser@127.0.0.1:8080/json/dump/test?");
      http.setPostData(postBody,"multipart/form-data; boundary=---------------------------blahblahblah");
      http.method = HTTP.Method.post;
      http.onReceive = (ubyte[] data) { content ~= data; return data.length; };
      http.perform();

      auto j = parseJSON(content);

      assert(j["method"].str == "POST");
      assert(j["uri"].str == "/json/dump/test");
      assert(j["host"].str == "127.0.0.1:8080");

      assert(j["get"].array.map!(x=>x.str).array.sort.array == []);
      assert(j["post"].array.map!(x=>x.str).array.sort.array == []);
      assert(j["cookie"].array.map!(x=>x.str).array.sort.array == []);

      assert(j["username"].str == "myuser");
      assert(j["password"].str == string.init);

      assert(j["form-data"].array.map!(x=>x.str).array.sort.array == ["field1,text/plain,first value", "field2,text/plain,second value"]);
      assert(j["form-file"].array.map!(x=>x.str).array.sort.array == ["myfile,file1.txt,application/json,{}\n"]);

      assert(j["content-type"].str == "multipart/form-data");
   }