mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Fix crashes found by fuzzing malformed input
- Components can't be made into a parent loop, and the parts tree walks stop safely if one exists (logbook entry and squawk resolution pages crashed). - A rejected aircraft photo no longer breaks the edit page; empty photos are refused; images that can't be read aren't resized, and a failed resize sends the original instead of an error. - Unsupported formats (/report.pdf…) answer 406; tampered upload references, out-of-range numbers and constraint violations get a form message instead of a 500; requests with null bytes are refused (400). - Blank values in required columns with defaults take the default; optional links to records that don't exist are validation errors. - Imports (oil report, logbook scans, .sam.zip) accept only uploaded files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5876c07a1a
commit
908c943e21
15 files changed
+201
-15
No files matched your search
@@ -82,16 +82,20 @@ class Aircraft < ApplicationRecord
|
||||
events.create!(category: category, event_name: event_name, user: user, notes: notes)
|
||||
end
|
||||
|
||||
# A saved photo to show (not one just rejected by validation, which has no
|
||||
# address yet).
|
||||
def picture_shown? = picture.attached? && picture.attachment.persisted?
|
||||
|
||||
# The photo at a page's size (:thumb or :card), or the original when it
|
||||
# isn't a kind that can be resized.
|
||||
# can't be resized (see ActiveStorage::Blob#resizable_image?).
|
||||
def picture_for(size)
|
||||
picture.variable? ? picture.variant(size) : picture
|
||||
picture.blob.resizable_image? ? picture.variant(size) : picture
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def picture_is_image
|
||||
if picture.attached? && !picture.content_type.to_s.start_with?("image/")
|
||||
if picture.attached? && (!picture.content_type.to_s.start_with?("image/") || picture.blob.byte_size.zero?)
|
||||
errors.add(:picture, :not_image)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,28 @@
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
primary_abstract_class
|
||||
|
||||
before_validation :default_blank_required_columns
|
||||
validate :optional_references_exist
|
||||
|
||||
private
|
||||
|
||||
# A blank form field arrives as nil; for a column that can't be empty and has
|
||||
# a default (a checkbox, a count, a position), use the default rather than
|
||||
# failing in the database.
|
||||
def default_blank_required_columns
|
||||
self.class.columns.each do |column|
|
||||
next if column.null || column.default.nil? || !self[column.name].nil?
|
||||
self[column.name] = self.class.type_for_attribute(column.name).cast(column.default)
|
||||
end
|
||||
end
|
||||
|
||||
# An optional link (a parent part, a collection…) given an id that doesn't
|
||||
# exist is a mistake in the form, not something to store.
|
||||
def optional_references_exist
|
||||
self.class.reflect_on_all_associations(:belongs_to).each do |assoc|
|
||||
next if assoc.polymorphic? || !assoc.options[:optional]
|
||||
next if self[assoc.foreign_key].blank? || !attribute_changed?(assoc.foreign_key)
|
||||
errors.add(assoc.name, :invalid) unless public_send(assoc.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
+15
-4
@@ -44,13 +44,23 @@ class Component < ApplicationRecord
|
||||
OVERHAUL_ABBREVIATIONS.fetch(overhaul_kind) { engine? || propeller? ? "TSO" : I18n.t("models.component.since_service") }
|
||||
end
|
||||
|
||||
# Its sub-components, and theirs.
|
||||
def descendant_ids
|
||||
child_components.flat_map { [ _1.id, *_1.descendant_ids ] }
|
||||
# Its sub-components, and theirs. (Stops at a loop, should one exist.)
|
||||
def descendant_ids(seen = Set[id])
|
||||
child_components.flat_map do |child|
|
||||
next [] unless seen.add?(child.id)
|
||||
[ child.id, *child.descendant_ids(seen) ]
|
||||
end
|
||||
end
|
||||
|
||||
# Its parent, the parent's parent, and so on up.
|
||||
def ancestor_ids
|
||||
parent_component ? [ parent_component_id, *parent_component.ancestor_ids ] : []
|
||||
ids = []
|
||||
node = self
|
||||
while (parent_id = node.parent_component_id) && parent_id != id && !ids.include?(parent_id)
|
||||
ids << parent_id
|
||||
node = Component.find_by(id: parent_id) or break
|
||||
end
|
||||
ids
|
||||
end
|
||||
def engine? = component_type&.name.to_s.match?(/engine/i)
|
||||
def propeller? = component_type&.name.to_s.match?(/propell?er|\bprop\b/i)
|
||||
@@ -133,6 +143,7 @@ class Component < ApplicationRecord
|
||||
def parent_on_same_aircraft
|
||||
return unless parent_component
|
||||
errors.add(:parent_component, :itself) if parent_component_id == id && id
|
||||
errors.add(:parent_component, :own_part) if id && parent_component.ancestor_ids.include?(id) # would make a loop
|
||||
errors.add(:parent_component, :other_aircraft) if parent_component.aircraft_id != aircraft_id
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user