webp-sh / webp_server_go

Go version of WebP Server. A tool that will serve your JPG/PNG/BMP/SVGs as WebP/AVIF format with compression, on-the-fly.
https://docs.webp.sh
GNU General Public License v3.0
1.79k stars 174 forks source link

Empty file causes runtime error when it is expected to be a directory #272

Closed michael-pxr closed 1 year ago

michael-pxr commented 1 year ago

Describe the bug The webp-server returns status code 500 when there is a file whose name is equal to a section in the path of the request. E.g. the request http://localhost:3333/empty/image-01.jpg will fail if there exist a file named empty.

To Reproduce Create the directory with files:

# Create a directory
mkdir img
# Add a real image
cp ~/Picture/image-01.jpg ./img/image-01.jpg
# Create an empty file
touch ./img/empty

Start the webp-server:

docker run -p 3333:3333 -v ./img:/opt/pics --name webp-server webpsh/webp-server-go:0.9.10

In Chrome, Firefox and Safari, go to http://localhost:3333/image-01.jpg. The image is shown as expected. In Chrome and Firefox the Content-Type is image/webp. In Safari, the Content-Type is image/jpeg.

In Chrome and Firefox, go to http://localhost:3333/empty/image-01.jpg. The response status code is 500, with the following message:

runtime error: invalid memory address or nil pointer dereference

In Safari the status code is 404, with message

sendfile: file exhaust/local/8d0988c39e0f70b7 not found

Expected behavior I expect the status code to be 404 for all browsers.

Screenshots and logs Chrome curl corresponding to the Chrome request:

$ curl -v 'http://localhost:3333/empty/image-01.jpg' \
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
  -H 'Accept-Language: en-GB,en;q=0.9' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Sec-Fetch-Dest: document' \
  -H 'Sec-Fetch-Mode: navigate' \
  -H 'Sec-Fetch-Site: none' \
  -H 'Sec-Fetch-User: ?1' \
  -H 'Upgrade-Insecure-Requests: 1' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' \
  -H 'sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  --compressed
*   Trying 127.0.0.1:3333...
* Connected to localhost (127.0.0.1) port 3333 (#0)
> GET /empty/image-01.jpg HTTP/1.1
> Host: localhost:3333
> Accept-Encoding: deflate, gzip
> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
> Accept-Language: en-GB,en;q=0.9
> Cache-Control: no-cache
> Connection: keep-alive
> Pragma: no-cache
> Sec-Fetch-Dest: document
> Sec-Fetch-Mode: navigate
> Sec-Fetch-Site: none
> Sec-Fetch-User: ?1
> Upgrade-Insecure-Requests: 1
> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
> sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"
> sec-ch-ua-mobile: ?0
> sec-ch-ua-platform: "macOS"
>
< HTTP/1.1 500 Internal Server Error
< Server: WebP Server Go
< Date: Thu, 14 Sep 2023 13:26:40 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 64
<
* Connection #0 to host localhost left intact
runtime error: invalid memory address or nil pointer dereference%

corresponding logs of the webp-server:

 - [2023-09-14 13:33:07]  GET      /empty/image-01.jpg  500   Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Firefox curl corresponding to the Firefox request:

$ curl -v 'http://localhost:3333/empty/image-01.jpg' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' \
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' \
  -H 'Accept-Language: en-US,en;q=0.5' \
  -H 'Accept-Encoding: gzip, deflate, br' \
  -H 'Connection: keep-alive' \
  -H 'Upgrade-Insecure-Requests: 1' \
  -H 'Sec-Fetch-Dest: document' \
  -H 'Sec-Fetch-Mode: navigate' \
  -H 'Sec-Fetch-Site: none' \
  -H 'Sec-Fetch-User: ?1' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache'
*   Trying 127.0.0.1:3333...
* Connected to localhost (127.0.0.1) port 3333 (#0)
> GET /empty/image-01.jpg HTTP/1.1
> Host: localhost:3333
> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0
> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
> Accept-Language: en-US,en;q=0.5
> Accept-Encoding: gzip, deflate, br
> Connection: keep-alive
> Upgrade-Insecure-Requests: 1
> Sec-Fetch-Dest: document
> Sec-Fetch-Mode: navigate
> Sec-Fetch-Site: none
> Sec-Fetch-User: ?1
> Pragma: no-cache
> Cache-Control: no-cache
>
< HTTP/1.1 500 Internal Server Error
< Server: WebP Server Go
< Date: Thu, 14 Sep 2023 13:27:18 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 64
<
* Connection #0 to host localhost left intact
runtime error: invalid memory address or nil pointer dereference%

corresponding logs of the webp-server:

 - [2023-09-14 13:34:38]  GET      /empty/image-01.jpg  500   Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0

Safari curl corresponding to the Safari request:

$ curl -v 'http://localhost:3333/empty/image-01.jpg' \
-X 'GET' \
-H 'Pragma: no-cache' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
-H 'Sec-Fetch-Site: none' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Accept-Language: en-GB,en;q=0.9' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Cache-Control: no-cache' \
-H 'Host: localhost:3333' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.15' \
-H 'Connection: keep-alive' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Sec-Fetch-Dest: document'
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying 127.0.0.1:3333...
* Connected to localhost (127.0.0.1) port 3333 (#0)
> GET /empty/image-01.jpg HTTP/1.1
> Host: localhost:3333
> Pragma: no-cache
> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
> Sec-Fetch-Site: none
> Accept-Encoding: gzip, deflate
> Accept-Language: en-GB,en;q=0.9
> Sec-Fetch-Mode: navigate
> Cache-Control: no-cache
> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.15
> Connection: keep-alive
> Upgrade-Insecure-Requests: 1
> Sec-Fetch-Dest: document
>
< HTTP/1.1 404 Not Found
< Server: WebP Server Go
< Date: Thu, 14 Sep 2023 13:27:40 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 55
<
* Connection #0 to host localhost left intact
sendfile: file exhaust/local/8d0988c39e0f70b7 not found%

corresponding logs of the webp-server:

time="2023-09-14 13:35:28" level=info msg="Resize /opt/pics/empty/image-01.jpg itself to exhaust/local/8d0988c39e0f70b7" func="[88:webp_server_go/encoder.ResizeItself]"
time="2023-09-14 13:35:28" level=warning msg="Could not load /opt/pics/empty/image-01.jpg: open /opt/pics/empty/image-01.jpg: not a directory" func="[100:webp_server_go/encoder.ResizeItself]"
 - [2023-09-14 13:35:28]  GET      /empty/image-01.jpg  404   Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.15

Environment (please complete the following information):

Additional context N/A

n0vad3v commented 1 year ago

This problem can be fixed by https://github.com/webp-sh/webp_server_go/pull/274, you can try using webpsh/webp-server-go:latest to see if it works for you before our 0.9.11 release. 😉

michael-pxr commented 1 year ago

Thanks for solving the problem so quickly!