Skip to content

Guard against nil attachments in processing form - #1539

Open
jazairi wants to merge 1 commit into
mainfrom
etd-710
Open

jazairi wants to merge 1 commit into
mainfrom
etd-710

Conversation

@jazairi

@jazairi jazairi commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Why these changes are being introduced:

ThesisController#deleted_file_list calls .blob on a potentially nil Active Storage attachment.
The processing form can trigger a race condition
when a background process deletes a file that is
still visible on the form. If a user attempts to
delete that file and the attachment no longer
exists, the app throws a 422 error.

Relevant ticket(s):

How this addresses that need:

This adds a guard clause to check for the presence of the attachment before calling .blob.

Side effects of this change:

None.

Developer

Accessibility
  • ANDI or WAVE has been run in accordance to our guide.
  • This PR contains no changes to the view layer.
  • New issues flagged by ANDI or WAVE have been resolved.
  • New issues flagged by ANDI or WAVE have been ticketed (link in the Pull Request details above).
  • No new accessibility issues have been flagged.
New ENV
  • All new ENV is documented in README.
  • All new ENV has been added to Heroku Pipeline, Staging and Prod.
  • ENV has not changed.
Approval beyond code review
  • UXWS/stakeholder approval has been confirmed.
  • UXWS/stakeholder review will be completed retroactively.
  • UXWS/stakeholder review is not needed.
Additional context needed to review

E.g., if the PR includes updated dependencies and/or data
migration, or how to confirm the feature is working.

Code Reviewer

Code
  • I have confirmed that the code works as intended.
  • Any CodeClimate issues have been fixed or confirmed as
    added technical debt.
Documentation
  • The commit message is clear and follows our guidelines
    (not just this pull request message).
  • The documentation has been updated or is unnecessary.
  • New dependencies are appropriate or there were no changes.
Testing
  • There are appropriate tests covering any new functionality.
  • No additional test coverage is required.

Why these changes are being introduced:

`ThesisController#deleted_file_list` calls `.blob`
on a potentially nil Active Storage attachment.
The processing form can trigger a race condition
when a background process deletes a file that is
still visible on the form. If a user attempts to
delete that file and the attachment no longer
exists, the app throws a 422 error.

Relevant ticket(s):

- [USE-710](https://mitlibraries.atlassian.net/browse/ETD-710)

How this addresses that need:

This adds a guard clause to check for the presence
of the attachment before calling `.blob`.

Side effects of this change:

None.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Add coverage verifying stale attachment submissions complete without a 422/error.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Guards thesis processing against deleted Active Storage attachments.

Changes:

  • Checks attachment existence before accessing its blob.
  • Skips stale attachment entries safely.
File summaries
File Description
app/controllers/thesis_controller.rb Adds a nil attachment guard in deleted_file_list.

The stale-attachment regression path lacks controller test coverage and should be tested.

Review details

Suppressed comments (1)

app/controllers/thesis_controller.rb:182

  • This guard only skips building the flash-message entry; the same stale id remains in thesis_params and is passed to thesis.update at line 124. Because Thesis enables accepts_nested_attributes_for :files_attachments, Rails can still raise ActiveRecord::RecordNotFound while applying _destroy for an attachment that disappeared, so the reported 422 race is not fully prevented. Remove or ignore missing attachment entries before the update (and add a regression test for the stale id).
      next unless attachment
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +181 to +182
attachment = ActiveStorage::Attachment.find_by(id: file['id'])
next unless attachment
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 98.287% (+0.001%) from 98.286% — etd-710 into main

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants