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:
rickyandClaude Opus 5.5 committed 2026-11-14 13:17:43 -05:00
1 parent 5876c07a1a
commit 908c943e21
15 files changed
+201 -15

No files matched your search

@@ -8,7 +8,7 @@ class AircraftImportsController < ApplicationController
def create
file = params[:archive]
return redirect_to(new_aircraft_import_path, alert: t("flash.aircraft_imports.choose_a_sam_zip")) if file.blank?
return redirect_to(new_aircraft_import_path, alert: t("flash.aircraft_imports.choose_a_sam_zip")) unless file.respond_to?(:tempfile)
preview = AircraftArchive::Importer.check(file.tempfile.path)
tail = params[:tail_number].to_s.strip.upcase.presence || preview.tail_number.to_s.upcase
+34
View File
@@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base
allow_browser versions: :modern
prepend_before_action :redirect_to_setup
prepend_before_action :reject_null_bytes
before_action { Current.ip_address = request.remote_ip } # for record history
helper_method :current_user
@@ -15,8 +16,41 @@ class ApplicationController < ActionController::Base
end
end
# Asked for a format a page doesn't have (/aircraft/1/report.pdf): not a crash.
rescue_from ActionView::MissingTemplate do |error|
raise error if request.format.html?
head :not_acceptable
end
# Input no form produces: a tampered upload reference, a number too big to
# store, a missing required value or a link to a record that isn't there.
# The forms validate what people can enter; this is the safety net.
rescue_from ActiveSupport::MessageVerifier::InvalidSignature, ActiveModel::RangeError,
ActiveRecord::NotNullViolation, ActiveRecord::InvalidForeignKey do |error|
Rails.logger.warn("[input] #{error.class}: #{error.message.truncate(200)}")
respond_to do |format|
format.html { redirect_back_or_to root_path, alert: t("flash.application.invalid_input") }
format.any { head :unprocessable_entity }
end
end
private
# No form sends a null byte; some libraries (password hashing, file paths)
# raise on one, so refuse the request up front.
def reject_null_bytes
head :bad_request if contains_null_byte?(request.params)
end
def contains_null_byte?(value)
case value
when String then value.include?("\0")
when Hash then value.any? { |k, v| contains_null_byte?(k) || contains_null_byte?(v) }
when Array then value.any? { contains_null_byte?(_1) }
else false
end
end
def current_user = Current.user
# First run: no users yet → create the initial admin.
@@ -10,7 +10,7 @@ class LogbookImportsController < ApplicationController
end
def create
files = Array(params[:pages]).compact_blank
files = Array(params[:pages]).select { _1.respond_to?(:original_filename) } # uploaded files only
return redirect_to(new_aircraft_logbook_import_path(@aircraft), alert: t("flash.logbook_imports.choose_the_scanned_pages")) if files.empty?
# Page order follows the file names ("page-002" before "page-010").
+1 -1
View File
@@ -35,7 +35,7 @@ class OilAnalysesController < ApplicationController
# earlier samples, so samples already on file are skipped rather than duplicated.
def import
file = params[:report]
return redirect_to(new_aircraft_oil_analysis_path(@aircraft), alert: t("flash.oil_analyses.choose_a_lab_report")) if file.blank?
return redirect_to(new_aircraft_oil_analysis_path(@aircraft), alert: t("flash.oil_analyses.choose_a_lab_report")) unless file.respond_to?(:tempfile)
report = OilReportParser.from_pdf(file.tempfile)
file.rewind