10

With my new project, when I deploy my app to my https:// domain, every {{ asset() }} and every {{ route() }} is being served over http (which causes "mixed content" security issues in browsers).

I'm using AWS with a load-balanced Elastic Beanstalk application.

I've tried ensuring APP_URL is correctly set to https, and I understand I can use secure_asset or forceScheme, however I didn't have to do this with my previous project and I want to understand why.

How can I see where Laravel is making a decision about protocol? I want to get to the root of the problem rather than plaster over it.

Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
  • 3
    Is your SSL cert being implemented by a AWS load balancer? If so, Laravel includes the Fideloper/TrustedProxies package to handle this. You need to publish the config and set the proxies to * as the load balancer forwarding IP can vary. – Rob Fonseca Jul 05 '18 at 10:28
  • @RobFonseca Yes, I've just discovered this. You are absolutely right – Chuck Le Butt Jul 05 '18 at 11:02

2 Answers2

19

This is an easy gotcha. If you're using AWS you need to change your config. It's very simple and, as usual, Laravel's documentation has the solution. You can read more here:

https://laravel.com/docs/5.6/requests#configuring-trusted-proxies

enter image description here

All I had to do (as an AWS Elastic Beanstalk user) was edit app/Http/Middleware/TrustProxies.php:

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies = '*';

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_AWS_ELB;
}

Now everything is fine. Easy to miss when setting up a new project.

Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
  • 2
    Thanks for the tip about trust proxy. I use https://github.com/fideloper/TrustedProxy and it works perfectly. – shalonteoh Nov 23 '18 at 01:38
  • @shalonteoh Not sure what you mean. Laravel comes with TrustedProxy bundled already. That's what the above code refers to – Chuck Le Butt Nov 23 '18 at 10:45
0

I believe secure_asset is what you're looking for.
Here an example:

<link href="{{ secure_asset('assets/mdi/css/materialdesignicons.min.css') }}" media="all" rel="stylesheet" type="text/css" />

Update:

A better solution to do it right (tested in laravel 5.4):

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    if(env('APP_ENV') == 'production') {
        \URL::forceScheme('https');
    }
}

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    //
}
}
Gothiquo
  • 841
  • 1
  • 8
  • 31