Define Default URL for ActiveStorage to fix Mixed Content Error

Reading time: 3 minutes

Using ActiveStorage with external services like AWS S3 for saving and surfing attachments like images is great. The setup is well explained in Active Storage Overview in Ruby on Rails Guides.

But if you start using it, you will maybe encounter some problems, which are not that easy to find or debug, as they mostly appear in production. One of the problems we encountered was a Mixed content related to ActiveStorage. This was not a big problem at first, as you get only a warning in a browser with default configuration. But after we added an iOS App, which was using WkWebViews, some random redirects to AWS S3 images stopped working. We couldn’t even see connections for these images to ActiveStorage endpoint of our Ruby on Rails Application in logs, which made it harder to debug.

If you want a deep dive regarding URLs in Ruby on Rails, check this post: Default URL Options in Ruby on Rails.

Find the problem

After some analysis we found some image redirect links to load with “HTTP” and most with “HTTPS”. The next step was to figure out, why it is happening and how to fix it.

Understand it

The first simple step for a developer is “ask a search engine” (Google/Bing/….). But the results were fuzzy and most of them were covering unrelated topics or were just “it works now”, which I consider being a problem for most developers just trying the fix without understanding the issue, finding the right solution and maybe write a test for it.

Documentation

I’ve checked the documentation for ActiveStorage and the related page in Ruby on Rails Guides, but couldn’t find anything useful.

Project Issues

For me, it looked like an issue, that someone else might have had before. Let’s check GitHub issues for Ruby on Rails first. There I found this issue, which told me, that someone else had a similar problem and he tried to fix it by a pull request: Ruby on Rails Issue 42947. This pull request was closed. But I got a slight idea of where to look for the implementation, to figure out, how to fix it in our project.

The next step was to check the source code, which might tell me, where I have to set the default URL options for the project. It was in the class DiskService on Line 168:

def url_helpers
  @url_helpers ||= Rails.application.routes.url_helpers
end

Solution

As the project was more of a monolith, for me it was enough to set the default URL options for routes and therefore define it in for ActiveStorage too. As it was only relevant for production, I’ve added this on the top of config/environments/production.rb:


Rails.application.routes.default_url_options = { host: "www.my.project",
                                                 protocol: "https" }

After deploying the application to production the problem was gone and ActiveStorage used always the right URL https://www.my.project/.

Summary

Sometimes it’s not that easy to find a problem, which seems to be random and maybe not a problem on all devices, operating systems or browsers. You need a deep understanding what might be the reason for the problem and where look for the reason.

As most problems were encountered by other developers, you can try to search for it on web, stackoverflow, check the documentation, check project issues, or to be sure check the source code itself, which is the most secure documentation you can have.

If you want a deep dive regarding URLs in Ruby on Rails, check this post: Default URL Options in Ruby on Rails.


Newsletter



Tags