Hello,
I am running Cachet behind an ALB, which is itself behind an HAProxy (because exposed on an intranet).
The ALB performs the SSL termination and the HAProxy exposes Cachet on a custom port.
As a result, Cachet is completely confused when generating its URLs:
http:// instead of https://I thought the APP_URL would solve my issue, but it is unfortunately not taken into account at all.
A lot of issues reference this problem:
Each time, we see only one solution: patch a file with \URL::forceSchema('https');...
Unfortunately, the solution, in addition to being ugly, is not complete because the redirection after login to the dashboard is still not fixed.
So, for those who might be interested, here is how I managed to patch the Docker image to make it fully work:
FROM cachethq/docker:2.3.15
RUN set -e; \
# HOTFIX #1: force root URL and schema
# We locate the boot() method in AppServiceProvider.php
line=$(grep -n 'public function boot(Dispatcher $dispatcher)$' /var/www/html/app/Foundation/Providers/AppServiceProvider.php | tail -n1 | cut -f1 -d:); \
\
# We insert the code 2 lines after because there is a "{" on a separate line
insertAtLine=$((line+2)); \
\
# We insert the hotfix
sed -i "$insertAtLine i \\
// Begin hotfix \n \
if (getenv('APP_URL')) { \n \
if (strpos(getenv('APP_URL'), 'https') === 0) { \n \
\\\URL::forceSchema('https'); \n \
} \n \
\\\URL::forceRootUrl(getenv('APP_URL')); \n \
} \n \
// End hotfix \n \
" /var/www/html/app/Foundation/Providers/AppServiceProvider.php; \
\
# We forward the APP_URL environment variable to FPM
echo '[www]' > /etc/php7/php-fpm.d/app-url-fix.conf; \
echo 'env[APP_URL] = $APP_URL' >> /etc/php7/php-fpm.d/app-url-fix.conf; \
\
# HOTFIX #2: fix login redirection to dashboard
# Normally it redirects to the last URL, but since Cachet is not able to detect URLs right, we force a redirection to dashboard
sed -i "s/Redirect::intended('dashboard');/Redirect::route('dashboard.index');/g" \
/var/www/html/app/Http/Controllers/AuthController.php
Of course, this is ugly and works only for a specific version of Cachet.
Is it possible to implement this fix correctly?
:wave: Thank you for opening your first issue. I'm just an automated bot that's here to help you get the information you need quicker, so please ignore this message if it doesn't apply to your issue.
If you're looking for support, you should try the Slack group by registering your email address at https://cachethq-slack.herokuapp.com. Alternatively, email [email protected] for our Professional support service (please note, this a paid service.)
If you're issue is with documentation, you can suggest edits by clicking the Suggest Edits link on any page, or open an issue at https://github.com/CachetHQ/Docs
And the slightly modified fix for the latest version (2.4 at this time) of the container:
```dockerfile
FROM cachethq/docker:latest
RUN set -e; \
# HOTFIX #1: force root URL and schema
# We locate the boot() method in AppServiceProvider.php
line=$(grep -n 'public function boot(Dispatcher $dispatcher)$' /var/www/html/app/Foundation/Providers/AppServiceProvider.php | tail -n1 | cut -f1 -d:); \
\
# We insert the code 2 lines after because there is a "{" on a separate line
insertAtLine=$((line+2)); \
\
# We insert the hotfix
sed -i "$insertAtLine i \
// Begin hotfix \n \
if (getenv('APP_URL')) { \n \
if (strpos(getenv('APP_URL'), 'https') === 0) { \n \
\\URL::forceScheme('https'); \n \
} \n \
\\URL::forceRootUrl(getenv('APP_URL')); \n \
} \n \
// End hotfix \n \
" /var/www/html/app/Foundation/Providers/AppServiceProvider.php; \
\
# We forward the APP_URL environment variable to FPM
echo '[www]' > /etc/php7/php-fpm.d/app-url-fix.conf; \
echo 'env[APP_URL] = $APP_URL' >> /etc/php7/php-fpm.d/app-url-fix.conf; \
\
# Normally it redirects to the last URL, but since Laravel is not able to detect URLs right, we force a redirection to dashboard
sed -i "s/Redirect::intended(cachet_route('dashboard'));/cachet_redirect('dashboard');/g" \
/var/www/html/app/Http/Controllers/AuthController.php; \
sed -i "s/Redirect::intended('dashboard');/cachet_redirect('dashboard');/g" \
/var/www/html/app/Http/Controllers/AuthController.php```
found how to do it based on https://laracasts.com/discuss/channels/laravel/forcescheme-or-forceschema-in-laravel-54
Merci @Thomvaill for the original fix!
Edit: Updated with updated hotfix 2 below
And the slightly modified fix for the
latestversion (2.4 at this time) of the container:FROM cachethq/docker:latest RUN set -e; \ # HOTFIX #1: force root URL and schema # We locate the boot() method in AppServiceProvider.php line=$(grep -n 'public function boot(Dispatcher $dispatcher)$' /var/www/html/app/Foundation/Providers/AppServiceProvider.php | tail -n1 | cut -f1 -d:); \ \ # We insert the code 2 lines after because there is a "{" on a separate line insertAtLine=$((line+2)); \ \ # We insert the hotfix sed -i "$insertAtLine i \\ // Begin hotfix \n \ if (getenv('APP_URL')) { \n \ if (strpos(getenv('APP_URL'), 'https') === 0) { \n \ \\\URL::forceScheme('https'); \n \ } \n \ \\\URL::forceRootUrl(getenv('APP_URL')); \n \ } \n \ // End hotfix \n \ " /var/www/html/app/Foundation/Providers/AppServiceProvider.php; \ \ # We forward the APP_URL environment variable to FPM echo '[www]' > /etc/php7/php-fpm.d/app-url-fix.conf; \ echo 'env[APP_URL] = $APP_URL' >> /etc/php7/php-fpm.d/app-url-fix.conf; \ \ # HOTFIX #2: fix login redirection to dashboard # Normally it redirects to the last URL, but since Cachet is not able to detect URLs right, we force a redirection to dashboard sed -i "s/Redirect::intended('dashboard');/Redirect::route('dashboard.index');/g" \ /var/www/html/app/Http/Controllers/AuthController.phpfound how to do it based on https://laracasts.com/discuss/channels/laravel/forcescheme-or-forceschema-in-laravel-54
Merci @Thomvaill for the original fix!
And here is the correct fix for "HOTFIX #2: fix login redirection to dashboard" for v2.4:
# HOTFIX #2: fix login redirection to dashboard
# Normally it redirects to the last URL, but since Laravel is not able to detect URLs right, we force a redirection to dashboard
sed -i "s/Redirect::intended(cachet_route('dashboard'));/cachet_redirect('dashboard');/g" \
/var/www/html/app/Http/Controllers/AuthController.php; \
sed -i "s/Redirect::intended('dashboard');/cachet_redirect('dashboard');/g" \
/var/www/html/app/Http/Controllers/AuthController.php
Thank you so much for this :)
Any chance of any of you doing a PR so this get's merged ?
If anyone else comes across this, I rectified this by simply adding TRUSTED_PROXIES=* to the environment variables on startup.
Looks like a series of recent commits broke this fix. Mostly https://github.com/CachetHQ/Cachet/pull/4071 but event just removing 'foundations' from the fix doesn't gives the same end result.
Most helpful comment
And the slightly modified fix for the
latestversion (2.4 at this time) of the container:```dockerfile
FROM cachethq/docker:latest
RUN set -e; \
# HOTFIX #1: force root URL and schema
# We locate the boot() method in AppServiceProvider.php
line=$(grep -n 'public function boot(Dispatcher $dispatcher)$' /var/www/html/app/Foundation/Providers/AppServiceProvider.php | tail -n1 | cut -f1 -d:); \
\
# We insert the code 2 lines after because there is a "{" on a separate line
insertAtLine=$((line+2)); \
\
# We insert the hotfix
sed -i "$insertAtLine i \
// Begin hotfix \n \
if (getenv('APP_URL')) { \n \
if (strpos(getenv('APP_URL'), 'https') === 0) { \n \
\\URL::forceScheme('https'); \n \
} \n \
\\URL::forceRootUrl(getenv('APP_URL')); \n \
} \n \
// End hotfix \n \
" /var/www/html/app/Foundation/Providers/AppServiceProvider.php; \
\
# We forward the APP_URL environment variable to FPM
echo '[www]' > /etc/php7/php-fpm.d/app-url-fix.conf; \
echo 'env[APP_URL] = $APP_URL' >> /etc/php7/php-fpm.d/app-url-fix.conf; \
\
HOTFIX #2: fix login redirection to dashboard
found how to do it based on https://laracasts.com/discuss/channels/laravel/forcescheme-or-forceschema-in-laravel-54
Merci @Thomvaill for the original fix!
Edit: Updated with updated hotfix 2 below