{"record":{"id":"f579a3376e6c5b9d","repo":"instructure/canvas-lms","slug":"unknown-mime-type-mime-type-for-archive-file-basename","errorCode":null,"errorMessage":"unknown mime type #{mime_type} for archive #{File.basename(archive_filename)}","messagePattern":"unknown mime type #(.+?) for archive #(.+?)","errorType":"exception","errorClass":"CanvasUnzip::UnknownArchiveType","httpStatus":null,"severity":"error","filePath":"gems/canvas_unzip/lib/canvas_unzip.rb","lineNumber":151,"sourceCode":"      Zip::File.open(file) do |zipfile|\n        zipfile.entries.each_with_index do |zip_entry, index|\n          yield(Entry.new(zip_entry), index)\n        end\n      end\n    when \"application/x-tar\"\n      index = 0\n      begin\n        Gem::Package::TarReader.new(file).each do |tar_entry|\n          next if tar_entry.header.typeflag == \"x\"\n\n          yield(Entry.new(tar_entry), index)\n          index += 1\n        end\n      rescue Gem::Package::TarInvalidError\n        raise UnknownArchiveType, \"invalid tar\"\n      end\n    else\n      raise UnknownArchiveType, \"unknown mime type #{mime_type} for archive #{File.basename(archive_filename)}\"\n    end\n  end\n\n  def self.compute_uncompressed_size(archive_filename)\n    total_size = 0\n    each_entry(archive_filename) { |entry, _index| total_size += entry.size }\n    total_size\n  end\n\n  class Entry\n    attr_reader :entry, :type\n\n    def initialize(entry)\n      case entry\n      when Zip::Entry\n        @type = :zip\n      when Gem::Package::TarReader::Entry\n        @type = :tar","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/gems/canvas_unzip/lib/canvas_unzip.rb#L133-L169","documentation":"CanvasUnzip.each_entry raises UnknownArchiveType with this message when neither the file's mime type (via File.mime_type magic detection, with extension fallback) nor the branch logic recognizes it as zip, tar, or gzip-supported archive. The library only supports known archive formats and refuses anything else.","triggerScenarios":"Extracting a file that isn't an archive at all (plain text, PDF, image) — often because the upload was never validated; a rare format like 7z, rar, or a mislabeled file whose magic bytes aren't recognized on the host (missing libmagic entries); empty/0-byte files detected as application/octet-stream with a non-archive extension.","commonSituations":"Users uploading unsupported formats (.7z, .rar) through a course-import or file-upload flow that assumed .zip; OS differences in mime detection producing unusual types; files renamed without changing content; empty file uploads.","solutions":["Validate the upload's mime type/extension client- and server-side, accepting only zip/tar/tgz before calling CanvasUnzip.","Tell the user the format is unsupported and ask for a zip or tar archive.","Rescue CanvasUnzip::UnknownArchiveType and show a clear 'Unsupported archive type' error instead of a stack trace.","If a legit format (e.g. 7z) is needed, convert it server-side with a tool that supports it before extraction.","Check the host's mime detection (file command/db) if valid .zip files are misdetected."],"exampleFix":"// before\nCanvasUnzip.extract_archive(upload_path, dest)\n// after\nunless upload_path =~ /\\.(zip|tar|tgz|tar\\.gz)\\z/\n  return errors.add(:file, \"must be a zip or tar archive\")\nend\nCanvasUnzip.extract_archive(upload_path, dest)","handlingStrategy":"validation","validationCode":"allowed = %r{\\.(zip|tar|tgz|tar\\.gz)\\z}\nraise UnsupportedArchive unless archive_filename.match?(allowed)","typeGuard":"def supported_archive?(path)\n  %w[application/zip application/x-tar application/gzip].include?(File.mime_type(File.open(path)))\nend","tryCatchPattern":"begin\n  CanvasUnzip.extract_archive(path, dest)\nrescue CanvasUnzip::UnknownArchiveType => e\n  errors.add(:file, \"Unsupported archive type. Please upload a .zip or .tar archive.\")\n  Rails.logger.info(\"unsupported archive: #{e.message}\")\nend","preventionTips":["Whitelist zip/tar extensions at upload","Content-sniff uploads, not just extensions","Tell users which formats are accepted","Convert unsupported formats server-side if needed"],"tags":["unsupported-format","archive","mime-type","upload"],"backgroundTag":"unsupported-operation","analyzedSha":"1c9f0bb8013ed69c4f2efe11fd483025469b7e6c","analyzedAt":"2026-09-15T20:33:18.891Z","contentChangedAt":"2026-09-15T20:33:18.891Z","schemaVersion":2},"datasetVersion":"2026-09-23T02:17:17.105Z"}