Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a documented way to turn off EPS handling via Ghostscript #7391

Closed
raphaelm opened this issue Sep 12, 2023 · 6 comments · Fixed by #7392
Closed

Provide a documented way to turn off EPS handling via Ghostscript #7391

raphaelm opened this issue Sep 12, 2023 · 6 comments · Fixed by #7392

Comments

@raphaelm
Copy link

raphaelm commented Sep 12, 2023

Pillow parses EPS files through ghostscript.

Ghostscript is troubled with regular security issues of critical impact, such as remote code execution. There has been this one, two CVEs this year already, and there is another one just coming up with the patch not yet included in any release.

We do not need EPS handling and we can turn it off in our code using Image.open(…, formats=["JPG", "PNG", …]). However, we use third-party components that also use Pillow (such as reportlab) and we'd like to make sure they also don't accidentally open EPS files and we can't modify the code.

From what I can tell, we can accomplish this with:

from PIL import Image

if "EPS" in Image.ID:
    Image.ID.remove("EPS")

However, that seems to be undocumented and feels like unclean monkeypatching. I think it would be worth it providing a documented API to turn off third-party backends like ghostscript globally.

@radarhere
Copy link
Member

Hi. See what you think of #7392. It adds EpsImagePlugin.gs_binary, so that setting EpsImagePlugin.gs_binary = False would result in OSError: Unable to locate Ghostscript on paths when trying to load an EPS image.

@radarhere radarhere changed the title Provide a documented way to turn off EPS handling via ghostscript Provide a documented way to turn off EPS handling via Ghostscript Sep 12, 2023
@raphaelm
Copy link
Author

Wow, that was fast! Not sure if I read the code exactly, but wouldn't this trigger OSError from line 72 whenever I set it to False and import the module?

@radarhere
Copy link
Member

radarhere commented Sep 12, 2023

No, the Ghostscript function is only called when an image is loaded.

def load(self, scale=1, transparency=False):
# Load EPS via Ghostscript
if self.tile:
self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency)

With the workaround that you posted above, an EPS file won't be identified. In my suggestion, it will be identified. Image.open will complete successfully. It will just raise an OSError once load() is called on it (which many Pillow operations call internally).

@raphaelm
Copy link
Author

Ah sorry, I've mistaken Ghostscript to be a class. My bad! Yes, sounds good then. (A note in the docs about it would be great to make it "official" so I can somehow expect it to continue to work in future versions.)

@radarhere
Copy link
Member

Sure. I've added a commit to the PR. You can see the preview at https://pillow--7392.org.readthedocs.build/en/7392/handbook/image-file-formats.html#eps

To use Ghostscript, Pillow searches for the “gs” executable. On Windows, it also searches for “gswin32c” and “gswin64c”. If you would like to customise this behaviour, EpsImagePlugin.gs_binary = "gswin64" will set the name of the executable to use. EpsImagePlugin.gs_binary = False will prevent Ghostscript from being used altogether.

@raphaelm
Copy link
Author

Thank you <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants