# Discourse incorrectly detecting uploaded file as an image

**URL:** <https://meta.discourse.org/t/discourse-incorrectly-detecting-uploaded-file-as-an-image/283564>\
**Category:** Support\
**Created:** [October 26, 2023, 5:44pm UTC](https://meta.discourse.org/t/discourse-incorrectly-detecting-uploaded-file-as-an-image/283564 "2023-10-26T17:44:13Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [November 18, 2023, 6:26pm UTC](https://meta.discourse.org/t/discourse-incorrectly-detecting-uploaded-file-as-an-image/283564/5 "2023-11-18T18:26:32Z")

</div>

I did some digging around this issue.

In short, your file is being detected as JPEG because it starts with the same signature as this type of file.  
Fixing this behavior in Discourse is possible, but it requires a modification. (see at the end)

* * *

Here some technical stuff.  
This issue starts here:

> <https://github.com/discourse/discourse/blob/main/lib/upload_creator.rb#L65-L74>

[FastImage](https://github.com/sdsykes/fastimage) library opens the file and determines the type and size.  
As you expect, it returns a type of `JPEG`.

If you look at what is [JPEG signature](https://en.wikipedia.org/wiki/List_of_file_signatures), it looks like this:

 ![image](https://global.discourse-cdn.com/meta/original/4X/a/9/9/a99f200be2e4d2ff64fdec576d78134f40a157c1.png)

> **JPEG markers**
>
> More information: [List of file signatures - Wikipedia](https://en.wikipedia.org/wiki/List_of_file_signatures)  
> [JPEG - Wikipedia](https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure)
> 
> ![image](https://global.discourse-cdn.com/meta/original/4X/f/2/6/f26509baa05bc07f91311fdac4385c46de91921f.png)

It always starts with the following marker bytes: `FF D8`

Looking at your file sample with a Hex editor, you will see it starts the same.

 ![image](https://global.discourse-cdn.com/meta/original/4X/f/a/2/fa2bd0d99b9dcac9c99ce22db826e98591eeb768.png)

Now, if you look at how FastImage detects a JPEG, you can see here:

> <https://github.com/sdsykes/fastimage/blob/master/lib/fastimage.rb#L535-L536>

However, you can’t extract any image information since not all the required bytes are there.

How do we fix this issue in Discourse?  
Looking at FastImage code, there is a valuable option you can pass.

> <https://github.com/sdsykes/fastimage/blob/master/lib/fastimage.rb#L194-L195>

By using this option, any error ( `SizeNotFound`, `ImageFetchFailure`, `CannotParseImage`, `UnknownImageType`, `BadImageURI`) will return no image info ; and your file won’t be detected as an image.

```ruby
@image_info =
begin
   FastImage.new(@file, :raise_on_failure=>true)
rescue StandardError
   nil
end
...
is_image ||= @image_info && FileHelper.is_supported_image?("test.#{@image_info.type}")

```

Then, it can work now:

 ![image](https://global.discourse-cdn.com/meta/original/4X/f/9/4/f949c9ec4ce129883cd7cd68b35d1a413667a471.png)

I can make a PR later. Using this option makes sense here. 👍

---

_[View the full topic](https://meta.discourse.org/t/discourse-incorrectly-detecting-uploaded-file-as-an-image/283564)._
