Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent direct access for .blade.php files #1075

Merged
merged 7 commits into from
Apr 30, 2019
Merged

Prevent direct access for .blade.php files #1075

merged 7 commits into from
Apr 30, 2019

Conversation

Log1x
Copy link
Member

@Log1x Log1x commented Mar 14, 2019

In Trellis' current state, .blade.php files are viewable in plain-text by accessing them directly. Laravel avoids this problem by having views outside of the public directory, but unfortunately, that isn't a sane solution with WordPress.

Outside of Trellis, this also calls for an addition to the Sage docs to provide examples on preventing access with apache and/or nginx.

Credit to pkarjala from this thread for finding this.

Real World Example

Copy link
Member

@retlehs retlehs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well block twig files, too

@Log1x
Copy link
Member Author

Log1x commented Mar 14, 2019

might as well block twig files, too

Done.

@Log1x
Copy link
Member Author

Log1x commented Mar 26, 2019

This should be ready to get merged assuming we don't want to add another default extension. I believe Blade and Twig as defaults should be sane.

Log1x added 2 commits March 26, 2019 00:37
Return a 401 in all cases to prevent snooping

{% block blade_twig_templates -%}
# Prevent Blade and Twig templates from being accessed directly.
location ~* \.(blade\.php|twig)(/.*)?$ {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the (/.*)? This is going to match a lot more than just files ending in .blade.php or .twig

Copy link
Member Author

@Log1x Log1x Mar 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see roots/docs#180 (comment)

I think it was a valid point raised– unless I'm missing something in the nginx syntax above compared to Apache. Tests show it works as intended though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 okay but I still don't understand that situation. Maybe @QWp6t can clear it up

Copy link
Member Author

@Log1x Log1x Mar 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, you're probably right in terms of it not being an issue in Trellis unless the end-user messed with nginx configuration. Do you just want to leave it as a safe-guard or revert it? I don't think it really "hurts" anything persay. It'd just make example.com/test.blade.php/asdf return the 401. It was mainly suggested due to certain webserver configurations seeing a URL like that and "smartly" returning the closest match which would then result in a status 200 response ignoring the exact location rule.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The twig case is the one that worries me slightly more since there's no .php in it as well. So example.com/foo.twig/asdf would match? It likely doesn't matter, but I don't want to break any URLs which might be valid.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would indeed match– but as far as WordPress permalinks go it'd be a non-issue as . is replaced with - when editing a slug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swalkinshaw I don't think (/.*) is needed here.

https://roots-example-project.com/app/themes/sage/resources/views/index.blade.php/more-stuff-here

Seems to work fine.

I was only suggesting it for Apache because after testing on a few Apache servers, they all displayed the blade file.

@Log1x
Copy link
Member Author

Log1x commented Mar 26, 2019

Curious which error code we should settle on? Possibilities are 401, 403, or maybe even a 301 back to homepage since returning a 401/403 will return the default web server template instead of being handled by WordPress.

@QWp6t
Copy link
Member

QWp6t commented Mar 26, 2019

I would suggest returning whatever the Not Allowed code is. I think that's 403 iirc (I'm on my phone, too lazy to look it up).

Revert to a 403 (`deny all`)
@Log1x
Copy link
Member Author

Log1x commented Mar 29, 2019

Apologies for the delay, I had sworn I had pushed an update already. The regex is now reverted as well as returning a 403 (deny all) instead of a 401.

This should now be ready.

@retlehs retlehs merged commit 9c50dbc into roots:master Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants