CP-14339 - Guard against a missing area page in result attachment generation#77
Merged
Conversation
The issue here is result-attachment generation 500s with `undefined method '<' for nil` whenever a template field area has no `page`. It's a permitted-but-not-required area param, so an imported or API-built area can omit it, and `pdf.pages[nil]` walks straight into HexaPDF's `nil < 0` at page_tree_node.rb:96. The existing `next if page.nil?` guard runs one line too late: the exception fires inside the `pages[...]` call itself. This adds a nil-guard before the index and skips the area, mirroring the surrounding `next if pdf.nil?` style. It logs a warning on the way out so a genuinely malformed template doesn't silently drop a field from a signed document without a trace. Adds a spec for the missing / nil / out-of-range page cases. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
spaulsandhu
commented
Jul 8, 2026
| # HexaPDF's pages[nil] does `nil < 0` and raises. `page` is a | ||
| # permitted-but-not-required area param, so a malformed/imported area | ||
| # can omit it. Skip it, but log so silently-incomplete signed docs stay visible. | ||
| if area['page'].nil? |
Member
Author
There was a problem hiding this comment.
I think this is reasonable for now, if anyone complains about missing pages we can check logs and whatnot, but crashing completely isn't really acceptable lol
rubocop flags the set_ prefix as a writer-method name; assign_field_area reads fine and isn't mistaken for an accessor. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This fixes the Airbrake
undefined method '<' for nilonGET /api/submissions/:id(CP-14339). Result-attachment generation 500s whenever a template field area has nopage.pageis a permitted-but-not-required area param (templates_controller.rblists it alongsidex/y/w/h, not required), so an imported, copied, or API-built area can legitimately omit it. When it does,pdf.pages[area['page']]inSubmissions::GenerateResultAttachments.fill_submitter_fieldspassesnilinto HexaPDF, whosePageTreeNode#pagedoesindex = self[:Count] + index if index < 0and blows up onnil < 0(page_tree_node.rb:96). The existingnext if page.nil?guard is one line too late, the exception fires inside thepages[...]call before it runs. This is the only.pages[<variable>]indexing site in the codebase, so it's the only place with this crash.The guard skips the area (mirroring the surrounding
next if pdf.nil?style) and logs a warning on the way out, since this is a signed-document product and silently dropping a field without a trace is worse than the crash. A malformed template stays visible in the logs instead.Added
spec/lib/submissions/generate_result_attachments_spec.rbcovering a missingpagekey, an explicitpage: nil, and an out-of-range page (confirming the existing nil-page-result handling still applies and the new guard only catchesnil).Not in scope here: why the affected submission's template has a
nilpage in the first place. Could be a one-off import artifact or a data-integrity gap worth a backfill, but that's a separate slice from the crash guard.🤖 Generated with Claude Code