Skip to content

Commit

Permalink
Fix mixed content when using a load balancer (#127)
Browse files Browse the repository at this point in the history
* Changed src/helpers.php

* Update swagger-lume.php config

added force_https config key.

* Updated swagger-lume.php config

Updated config file to conform to styling.

* Added unit tests

* Cleanup

* StyleCI Fixes

* Final cleanup

---------

Co-authored-by: R Mathieson <[email protected]>
  • Loading branch information
ADReece and R Mathieson authored May 9, 2023
1 parent 45e0d0f commit a91f7aa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
7 changes: 7 additions & 0 deletions config/swagger-lume.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,11 @@
'constants' => [
'SWAGGER_LUME_CONST_HOST' => env('SWAGGER_LUME_CONST_HOST', 'http://my-default-host.com'),
],

/*
|--------------------------------------------------------------------------
| Force assets to be loaded over HTTPS (Solves mixed content errors when application is behind a load balancer.)
|--------------------------------------------------------------------------
*/
'force_https' => env('SWAGGER_LUME_FORCE_HTTPS', false),
];
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ function swagger_lume_asset($asset)
throw new SwaggerLumeException(sprintf('Requested L5 Swagger asset file (%s) does not exists', $asset));
}

return route('swagger-lume.asset', ['asset' => $asset, 'v' => md5($file)], app('request')->secure());
return route('swagger-lume.asset', ['asset' => $asset, 'v' => md5($file)], config('swagger-lume.force_https')) ?? app('request')->secure();
}
}
31 changes: 31 additions & 0 deletions tests/ForceHttpsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests;

class ForceHttpsTest extends LumenTestCase
{
/** @test */
public function defaultFromEnv()
{
$this->assertNull(env('SWAGGER_LUME_FORCE_HTTPS'));
$this->assertStringContainsString('http://', swagger_lume_asset('swagger-ui.css'));
}

/** @test */
public function forcesHttpsFromConfig()
{
config(['swagger-lume.force_https' => true]);

$this->assertStringContainsString('https://', swagger_lume_asset('swagger-ui.css'));

config(['swagger-lume.force_https' => false]);
}

/** @test */
public function doesNotForceHttpsFromConfig()
{
config(['swagger-lume.force_https' => false]);

$this->assertStringContainsString('http://', swagger_lume_asset('swagger-ui.css'));
}
}

0 comments on commit a91f7aa

Please sign in to comment.