isomerpages / isomercms-backend

A static website builder and host for the Singapore Government
5 stars 1 forks source link

fix(5xx): should return 4xx when file not found #1291

Closed kishore03109 closed 6 months ago

kishore03109 commented 6 months ago

Problem

Described in linear: missing media files in pages leads to a 5xx when the resource name itself cannot be processed

as an example, consider a link (some_link)[/%2fa/b] -> this resolves to //a/b, which is not processable by our backend as it results in an error like so: Error when getting Git blob hash: Error: HEAD:images/technews//tvws-part2.JPG and in turn causes a 5xx.

this 5xx is caused because GitFileService (iirc) will try to determine the blob hash (see if can find the file on disk) and in doing so, it will get an error (as the file doesn't exist), leading to the 500.

see here for an example where this occurs

in order to reproduce this:

Additionally, we also see a fatal: path '.git' does not exist in 'HEAD' in the logs every time the workspace is refreshed. This is because we rely on fileStats to get a list of directories, but forget to remove special directories such as .git.

Solution

Reason for this is that git under the hood does a file path normalision for checking if the file exists in local disk, but does not when checking when it exists in head.

consider these three examples:

Screenshot 2024-04-11 at 12 58 41 PM

We use fileStats to see if a file exists before passing it to GetGitBlobHash. issue with this is that while the file could exist in disk and still return a correct folder stats, this should still return a file not found error as the path given by the user is still incorrect. (am opting for this rather than normalising the path in the function GetGitBlobHash to keep the implementation of GetGitBlobHash lean and not think about potential path traversal attacks.)

for the special files, we simply do a filter to ignore files like .git. We know that the class of errors with the strings exists on disk and Error when getting Git blob hash mostly comes from the .git folder by looking at dd logs here.

ie all instances of exists on disk and Error when getting Git blob hash errors in prod for the past two weeks came from trying to access the .git folders.

Breaking Changes

Before & After Screenshots

BEFORE: Screenshot 2024-04-11 at 12 51 22 PM

AFTER: Screenshot 2024-04-11 at 12 52 17 PM

Tests

in order to reproduce this:

kishore03109 commented 6 months ago

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @kishore03109 and the rest of your teammates on Graphite Graphite