Cannot download files using `send_data`

Open
#1,910 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
rails, ruby
Domain
backend

Research direction

Start with app/middlewares/shopify_session_middleware.rb at the middleware call shown in the logs, then compare the Rails controller response with and without skip_around_action :activate_shopify_session. Reproduce the FailedRowsCSVController#show example using send_data and verify that the response downloads del.csv while the Shopify session remains active.

Written by the indexing model from the issue text.

Description

Issue summary

Before opening this issue, I have:

  • Upgraded to the latest version of the package
    • shopify_app version: 22.4.0
    • Ruby version: ruby 3.2.5 (2024-07-26 revision 31d0f1a2e7) [x86_64-linux]
    • Operating system: Linux inni 6.6.50-1-lts #1 SMP PREEMPT_DYNAMIC Sun, 08 Sep 2024 12:27:02 +0000 x86_64 GNU/Linux
  • Set log_level: :debug in my configuration, if applicable
  • Found a reliable way to reproduce the problem that indicates it's a problem with the package
  • Looked for similar issues in this repository
  • Checked that this isn't an issue with a Shopify API

Given a rails app with

# app/controllers/failed_rows_csv_controller.rb
class FailedRowsCSVController < ApplicationController
  include ShopifyApp::EnsureHasSession
  # skip_around_action :activate_shopify_session
  def show
    send_data 'foo,bar', filename: 'del.csv'
  end
end

and a link to that route:
<a href="/location_import_files/5/failed_rows_csv?session=whatever&amp;shop=prolo-dev.myshopify.com">download</a>

Expected behavior

What do you think should happen?

The file should be downloaded

Actual behavior

What actually happens?

The file is shown in the browser

Steps to reproduce the problem

I think I've detailed it above, but please let me know if you would like more details.

Debug logs

logs where the issue is shown
Started GET "/location_import_files/5/failed_rows_csv?session=67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728&shop=prolo-dev.myshopify.com" for 108.180.124.202 at 2024-09-20 21:13:30 -0700
Cannot render console from 108.180.124.202! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by FailedRowsCSVController#show as HTML
  Parameters: {"session"=>"67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728", "shop"=>"prolo-dev.myshopify.com", "location_import_file_id"=>"5"}
[ ShopifyApp | DEBUG | Shop Not Found ] Responding to invalid Shopify ID token: Missing Shopify ID Token
[ ShopifyApp | DEBUG | Shop Not Found ] Redirecting to bounce page for patching Shopify ID token
Redirected to https://prolo.ngrok.app//patch_shopify_id_token?session=67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728&shop=prolo-dev.myshopify.com&shopify-reload=%2Flocation_import_files%2F5%2Ffailed_rows_csv%3Fsession%3D67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728%26shop%3Dprolo-dev.myshopify.com
[ ShopifyApp | DEBUG | Shop Not Found ] Deactivating session
Completed 302 Found in 2ms (ActiveRecord: 0.2ms | Allocations: 914)


Started GET "//patch_shopify_id_token?session=67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728&shop=prolo-dev.myshopify.com&shopify-reload=%2Flocation_import_files%2F5%2Ffailed_rows_csv%3Fsession%3D67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728%26shop%3Dprolo-dev.myshopify.com" for 108.180.124.202 at 2024-09-20 21:13:30 -0700
Cannot render console from 108.180.124.202! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by ShopifyApp::SessionsController#patch_shopify_id_token as HTML
  Parameters: {"session"=>"67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728", "shop"=>"prolo-dev.myshopify.com", "shopify-reload"=>"/location_import_files/5/failed_rows_csv?session=67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728&shop=prolo-dev.myshopify.com"}
  Rendering layout vendor/bundle/ruby/3.2.0/gems/shopify_app-22.4.0/app/views/shopify_app/layouts/app_bridge.html.erb
  Rendering vendor/bundle/ruby/3.2.0/gems/shopify_app-22.4.0/app/views/shopify_app/sessions/patch_shopify_id_token.html.erb within shopify_app/layouts/app_bridge
  Rendered vendor/bundle/ruby/3.2.0/gems/shopify_app-22.4.0/app/views/shopify_app/sessions/patch_shopify_id_token.html.erb within shopify_app/layouts/app_bridge (Duration: 0.2ms | Allocations: 84)
  Rendered layout vendor/bundle/ruby/3.2.0/gems/shopify_app-22.4.0/app/views/shopify_app/layouts/app_bridge.html.erb (Duration: 2.0ms | Allocations: 653)
Completed 200 OK in 4ms (Views: 3.2ms | ActiveRecord: 0.3ms | Allocations: 1717)


Finished "/cable" [WebSocket] for 108.180.124.202 at 2024-09-20 21:13:30 -0700
Hotwire::Livereload::ReloadChannel stopped streaming from hotwire-reload
Started GET "/location_import_files/5/failed_rows_csv?session=67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728&shop=prolo-dev.myshopify.com" for 108.180.124.202 at 2024-09-20 21:13:30 -0700
Cannot render console from 108.180.124.202! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by FailedRowsCSVController#show as HTML
  Parameters: {"session"=>"67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728", "shop"=>"prolo-dev.myshopify.com", "location_import_file_id"=>"5"}
[ ShopifyApp | DEBUG | Shop Not Found ] Loading session by domain - domain: prolo-dev.myshopify.com
  Shop Load (0.1ms)  SELECT "shops".* FROM "shops" WHERE "shops"."shopify_domain" = $1 LIMIT $2  [["shopify_domain", "prolo-dev.myshopify.com"], ["LIMIT", 1]]
  ↳ app/middlewares/shopify_session_middleware.rb:16:in `call'
[ ShopifyApp | DEBUG | Shop Not Found ] Activating Shopify session
  Rendering text template
  Rendered text template (Duration: 0.0ms | Allocations: 1)
Sent data del.csv (0.4ms)
[ ShopifyApp | DEBUG | prolo-dev.myshopify.com ] Deactivating session
Completed 200 OK in 4ms (Views: 0.3ms | ActiveRecord: 0.4ms | Allocations: 1546)

However if I uncomment skip_around_action :activate_shopify_session then file successfully downloads with the following logs


Started GET "/location_import_files/5/failed_rows_csv?session=67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728&shop=prolo-dev.myshopify.com" for 108.180.124.202 at 2024-09-20 21:14:54 -0700
Cannot render console from 108.180.124.202! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by FailedRowsCSVController#show as HTML
  Parameters: {"session"=>"67ebebfce25c7668a6ced116994464711faae739888b3c3c773989cc3c10b728", "shop"=>"prolo-dev.myshopify.com", "location_import_file_id"=>"5"}
  Rendering text template
  Rendered text template (Duration: 0.0ms | Allocations: 1)
Sent data del.csv (0.3ms)
Completed 200 OK in 2ms (Views: 0.2ms | ActiveRecord: 0.2ms | Allocations: 551)

Dominant language
Ruby
Stars
1.9k
Forks
739
Avg merge
21h 2m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Shopify/shopify_app

All issues in Shopify/shopify_app

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.