instructure/canvas-lms · error · ContentUnavailable

File access denied

Error message

File access denied

What it means

StudyAssist::ContentUnavailable with 'File access denied' is raised by resolve_file when the attachment exists but attachment.grants_right?(@user, :read) is false. The service checks per-user read permission on the file before extracting text.

Solutions

  1. Verify @user has :read on the attachment (attachment.grants_right?(user, :read))
  2. Fix enrollment/permissions for the user in the course
  3. Unhide the file or adjust folder permissions if it should be readable
  4. Rescue StudyAssist::ContentUnavailable and return a 403-style response to the client

Example fix

// before
StudyAssist.new(course: @course, user: @user, prompt:, file_id: file_id).call
// after
att = @course.attachments.find(file_id)
unless att.grants_right?(@user, :read)
  return render json: { error: 'file unavailable' }, status: :forbidden
end
StudyAssist.new(course: @course, user: @user, prompt:, file_id: file_id).call
Defensive patterns

Strategy: validation

Validate before calling

att = course.attachments.find_by(id: file_id)
return nil unless att&.grants_right?(user, :read)

Type guard

null

Try / catch

begin
  StudyAssist.new(course:, user:, prompt:, file_id:).call
rescue StudyAssist::ContentUnavailable
  render json: { error: 'file not available' }, status: :forbidden
end

Prevention

When it happens

Trigger: Calling #call with a valid file_id but a @user lacking :read on the attachment (hidden/restricted file, folder-level permissions, non-enrolled user, or files locked to certain roles).

Common situations: Restricted or hidden course files; user enrollment missing or concluded; file visibility limited to specific sections/roles; using a different user token than the file owner intended.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/5127b57d66bb4797. Report an issue: GitHub.

Appendix: source

Thrown at app/services/study_assist.rb:172

    end

    def resolve_page(page_id)
      page = @course.wiki_pages.not_deleted.find_by(url: page_id)
      raise ContentUnavailable, "Page not found" if page.nil?
      raise ContentUnavailable, "Page access denied" unless page.grants_right?(@user, :read)

      shard_safe_key = shard_safe_cache_key_for(page)
      text = Rails.cache.fetch(text_cache_key_for(:page, shard_safe_key), expires_in: TEXT_CACHE_TTL) do
        html_to_text(page.body.to_s)
      end

      Content.new(kind: :page, id: page.id, cache_key_with_version: shard_safe_key, text:)
    end

    def resolve_file(file_id)
      attachment = @course.attachments.find_by(id: file_id)
      raise ContentUnavailable, "File not found" if attachment.nil? || attachment.deleted?
      raise ContentUnavailable, "File access denied" unless attachment.grants_right?(@user, :read)
      raise ContentUnavailable, "File is locked" if attachment.locked_for?(@user, check_policies: true)
      raise UnsupportedContentType unless supported_attachment?(attachment)
      raise ContentTooLarge, "File exceeds #{MAX_FILE_BYTES} byte limit" if attachment.size && attachment.size > MAX_FILE_BYTES

      shard_safe_key = shard_safe_cache_key_for(attachment)
      text = Rails.cache.fetch(text_cache_key_for(:file, shard_safe_key), expires_in: TEXT_CACHE_TTL) do
        extract_attachment_text(attachment)
      end

      raise ContentUnavailable, "No text available for file" if text.blank?

      Content.new(kind: :file, id: attachment.id, cache_key_with_version: shard_safe_key, text:)
    end

    def supported_attachment?(attachment)
      return true if attachment.content_type&.start_with?("text/")

      ACCEPTED_FILE_MIMETYPES.include?(attachment.content_type)

View on GitHub (pinned to 1c9f0bb801)