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

[#7875] Fix attachment processing #7932

Merged
merged 6 commits into from
Oct 3, 2023
Merged

Conversation

gbp
Copy link
Member

@gbp gbp commented Oct 2, 2023

Relevant issue(s)

Fixes: #7875

What does this do?

  1. 614025a update attachment masking to call IncomingMessage#parse_raw_email!(true) when the original attachment body can't be retrieved. This should rebuild the attachment in the database and correct the mis-matched hexdigest which allows the original attachment to be found again in the future.
  2. 3a56b19 allow mask jobs to be called inline and rescue when the attachment has been rebuilt
  3. 90d64a3 updates the mask controller to account for attachments being replaced in the database.
  4. ebc6392 protect against exceptions on the request#show and other actions where the attachment can be masked for the first time.

Why was this needed?

To ensure all attachments are viewable and accessible by users.

@gbp gbp force-pushed the attachment-processing-reparse branch from bc931f5 to 00f734b Compare October 2, 2023 13:53
We're going to make a change which might mean attachments might be
rebuilt within the `FoiAttachmentMaskJob` this means the signed global
ID might not return the attachment in this controller. This change
ensures their is a referer parameter and if the attachment can't be
loaded then we redirect back to the referer.

Note, we don't need to worry about people modifying the referer to get
users redirected to a third party domain as this is protected against by
Rails.
This check isn't doing anything. It was meant to protect when parsing
incoming messages to stop hidden attachments from being made public
again.

We don't actually yet have this parsing in place instead we've focused
on finding the original attachment [1].

For the remainder of cases we will need to parse the raw email again but
we have added separate check [2] so this one here is redundant.

[1] #7875
[2] #7920
Catch and rescue from `MissingAttachment` exceptions. Re-parse the
incoming message to rebuild the attachments instead.

Fixes: #7875
When attachment is unmasked we attempt to run the mask job inline. This
commit makes a couple of changes.

1. It unlocks the job so we can run it, so we don't have to worry about
   if the job doesn't run
2. Now the mask job can call `IncomingMessage#parse_raw_email!(true)` we
   need to handle to case where the attachment disappears after being
   rebuilt. This results in a `ActiveRecord::RecordNotFound` when
   reloading the instance.
3. Add RebuiltAttachment exception which we can then catch and rescue
   later. See the next commit.
When calling `FoiAttachment#body` this can now raise an exception due to
the current attachment instance getting replaced and no longer existing
in the database.

We can catch and rescue from this by reloading the new attachment and
calling the same code again.

This is what `FoiAttachment.protect_against_rebuilt_attachments` does
and should prevent exceptions when viewing requests which haven't be
masked and cached yet. These will normally be very old attachments as
this also need the `hexdigest` to be mismatched.
Exception notifications don't include the name of the exception so
improve the message to make it clearer what the issue is.
@gbp gbp force-pushed the attachment-processing-reparse branch from 00f734b to 625c685 Compare October 2, 2023 15:45
@gbp gbp merged commit 3f9a400 into develop Oct 3, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken attachment masking
1 participant