Problem/Motivation

Drupal enforces creation of .htaccess files yet some environments don't use them and others create errors when they are present (eg. IIS + WebsitePanel).

Proposed resolution

When Apache is reported as the web server, automatically create .htaccess files. Otherwise, do not create them. Allow users to explicitly enable or disable the automatic creation of .htaccess files using a config setting (and hence clear any warning message).

Remaining tasks

  • Decide whether to show a warning on the status report page.

User interface changes

New warning message in File System section of Status Report.

API changes

New config setting.

Original report by kbahey

Check for .htaccess, and creating .htaccess should only be done for Apache

We are currently doing this check for non-Apache servers too, and it causes errors to be displayed when using IIS and other servers.

So, this patch limits this checking and .htaccess creation to Apache only.

Issue fork drupal-360057

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

webchick’s picture

Status: Needs review » Needs work

Seems reasonable.

Can we replace the preg_match() with a strstr()?

Also, a test would be nice.

webchick’s picture

Actually, I think I remember reading that strpos() is faster than strstr(). But either is definitely faster than preg_match(), according to the docs: http://ca2.php.net/preg_match.

kbahey’s picture

Status: Needs work » Needs review
StatusFileSize
new2.39 KB

Your wish is my command mi lady ...

webchick’s picture

Status: Needs review » Needs work

See, this is why I'd like tests. :)

Because that code will fail to execute if "Apache" is in the first position (0) in the string. Need to compare to !== FALSE.

kbahey’s picture

Status: Needs work » Needs review
StatusFileSize
new2.47 KB

It is late, but here goes ...

kbahey’s picture

StatusFileSize
new4.02 KB

Dearest webchick ...

I have learned the errors of my way, and included tests for Apache and he-who-shall-not-be-named.

With this repentance, I hope you accept my tithe, errr, patch ...

Status: Needs review » Needs work

The last submitted patch failed testing.

dave reid’s picture

Status: Needs work » Needs review

Requesting retest.

Status: Needs review » Needs work

The last submitted patch failed testing.

Akaoni’s picture

Status: Needs work » Needs review
StatusFileSize
new571 bytes

I don't think we can apply the blanket rule of Apache=.htaccess and non-Apache=no .htaccess.
There are, and will continue to be, exceptions to this rule (eg. IIS + ISAPI_Rewrite supports .htaccess).

Perhaps the best way forward is allowing people to opt-out of enforced .htaccess files.
Patch attached.

For info, I'm using Drupal on an IIS7.5 host with WebsitePanel and any form of .htaccess file breaks the website.
I've used this patch and the following in settings.php to fix the issue:
$conf['no_htaccess'] = TRUE;

Akaoni’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

Moved to 8.

Akaoni’s picture

Issue tags: +Needs tests

Added "Needs tests" tag.

Akaoni’s picture

Title: Check for .htaccess, and creating .htaccess should only be done for Apache » Automatic creation of .htaccess files can cause errors

Applied Issue summary standards and renamed.

Akaoni’s picture

Issue summary: View changes

Applying Issue summary standards

Akaoni’s picture

Issue summary: View changes

Added original title for ease of reference.

Akaoni’s picture

StatusFileSize
new1.22 KB

Feeling like a bit of a one man army on this one, but oh well. ;)

Test patch - should fail (for the right reason).

Status: Needs review » Needs work

The last submitted patch, core-test_no_htaccess-360057-14.patch, failed testing.

Akaoni’s picture

Priority: Normal » Major
Status: Needs work » Needs review
StatusFileSize
new1.78 KB

Combined patch.

Upgraded to major.

catch’s picture

Can we make the variable a positive one, right now it's a double negative.

Like variable_get('automatically_generate_htaccess', TRUE);

Otherwise looks fine and using a configuration setting for this sounds seems better than trying to figure it out.

Akaoni’s picture

Thanks catch!!

Rerolled to be a little more up-beat (positive).

First is test only and should fail twice (for the right reason).
Second is test and fix.

Akaoni’s picture

Issue summary: View changes

Cleanup remaining tasks and add test path.

Akaoni’s picture

Issue tags: -Needs tests

Sweet!!

Issue summary updated.

catch’s picture

Issue tags: +Needs tests

Sorry one more thing here:

+ // Don't create .htaccess files if 'auto_create_htaccess' variable is FALSE.

This needs an explanatory note as to why you might want to disable it, although probably in the main function docblock.

catch’s picture

Issue summary: View changes

New positive patch.

Akaoni’s picture

Issue tags: -Needs tests +Novice

Adding novice.
Removing cross post "Needs tests".

Akaoni’s picture

StatusFileSize
new2.36 KB

Improved comments as per catch's suggestion.

Paul Simard’s picture

Assigned: Unassigned » Paul Simard
Paul Simard’s picture

At the moment, I am unable to persuade either Drupal or Apache to create an .htaccess file in the public:// directory using the 8.x clean install. Status of test thus far:

  1. clean Drupal install with a module that allows file uploads (eg. http://drupal.org/project/media) ... OK
  2. checked sites/default/files for no .htaccess present ... OK
  3. upload a file to public ... OK
  4. see that a .htaccess file has been created in the public filesystem path (sites/default/files) ... FAIL
  • Added $conf['auto_create_htaccess'] = TRUE; to settings.php
  • Repeat step 2 ... FAIL (no .htaccess)
  • Repeat step 2 with patch applied ... OK
  • checked for .htaccess in sites/default/files ... not present ... FAIL

I am trying to track down the problem. I am not sure if it's in the WAMP stack, or Drupal as yet.

Akaoni’s picture

Assigned: Paul Simard » Unassigned

@Paul Simard: Thanks for giving testing this a crack!! ;) Perhaps the test path I wrote is a bit flawed? Will have a play and see.

Akaoni’s picture

Issue summary: View changes

Improved test path.

Akaoni’s picture

Issue summary: View changes

Better test path.

Akaoni’s picture

Yep. The test path was *completely* wrong. My bad.
In fact, you got further than I did - I don't even know how you installed Media 7.x onto Drupal 8.x. ;)

Updated it in issue summary.

catch’s picture

Status: Needs review » Reviewed & tested by the community

There's a nice automated test here that fails correctly without the fix applied, so no need for manual testing.

Paul Simard’s picture

There was no need to install Media on 8.x until and unless the patch passed using only core in a standard install. I just added a file field to the Article content type, and uploaded files that way.

Since it didn't pass, i.e. no .htaccess was created, either with or without the patch...

@catch I don't know if the previous was aimed at my inbox (re: automated test). If so, thanks for the info. If not, ignore this line.

Paul

Akaoni’s picture

@Paul Simard: Ah. I must admit, I wasn't aware that there was a way of uploading files with just core - I'm still only relatively new to Drupal. Good to know. ;)

Turns out uploading files doesn't "prepare" the directory anyways which is why you didn't get a .htaccess file. Oddly enough, changing colour scheme does so I've updated the test path accordingly.

Up to you whether you test this any further or not as catch reckons it's good to go as is. Thanks again!! ;)

Paul Simard’s picture

Akaoni,

In all fairness, it was catch who reminded me of 'File' module's presence in core. I merely added the extra field. I'll bow to catch's greater experience and Drupal expertise, as far as continued testing goes, as he is far higher on the food chain than I. I may spend a bit of time in the code trying to ferret out its secrets, though, in my quest to learn Drupal myself. (I'm still very low on the food chain, myself.)

Let me know if I can help any further, as time permits, I'd be interested.

Paul

dries’s picture

Instead of a global killswitch variable, wouldn't it be better to check for the existence of Apache and make this behavior automatic?

  $apache = strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== FALSE;
  if ($apache) { ... }

I understand there are exceptions to this rule (e.g. some IIS configurations support .htaccess files), but does that really hold up as an argument against automatic behavior? Let's brainstorm about it a bit more ...

I'd think that the automatic behavior avoids errors/problems, whereas the global killswitch variable doesn't. Because the variable defaults to generating .htaccess files (i.e. variable_get('auto_create_htaccess', TRUE)), it still causes problems on installations that don't support .htaccess files. Hence, it requires people to research the problem, and to find out about the existence of the global killswitch variable. That is non-trivial and most people might have given up by the time they learn about the global killswitch variable ... in other words, I find the variable kind of a poor solution.

yoroy’s picture

Status: Reviewed & tested by the community » Needs work

Sounds like this needs some more work then

catch’s picture

Automatic detection with no killswitch is going to break things for those IIS installs, all I can think of as a compromise is this:

Automatically create the .htaccess if it's Apache.

Don't create it automatically for anything else, however, warn in system_requirements() if the .htaccess doesn't exist. For sites that really don't want it, keep the killswitch which will mainly function to disable the system_requirements() warning.

Akaoni’s picture

StatusFileSize
new2.21 KB

@Dries: Good point on automation being much better for users. I agree with catch though, in that we still need a killswitch/override. There's so much variation out there on how web servers and even web applications handle .htaccess files. As such, let's automate what we know and allow people to override for the edge cases.

New test only patch as described by catch (should fail twice for the right reason).

Akaoni’s picture

Status: Needs work » Needs review
StatusFileSize
new5.21 KB

New test and fix patch as described by catch.

catch’s picture

Status: Needs review » Needs work
+++ b/includes/file.incundefined
@@ -470,15 +470,45 @@ function file_ensure_htaccess() {
+  if (!variable_get('auto_create_htaccess', stripos($_SERVER['SERVER_SOFTWARE'], 'apache') !== FALSE)) {
+    return;

This and the next check:

+++ b/modules/system/system.installundefined
@@ -356,6 +356,13 @@ function system_requirements($phase) {
+    // variable hasn't been overridden.
+    if (stripos($_SERVER['SERVER_SOFTWARE'], 'apache') === FALSE && is_null(variable_get('auto_create_htaccess', NULL))) {
+      $requirements['file system']['description'] = $t('Your web server has been detected as different from Apache so creation of .htaccess files is not being enforced. As such your file system may not be secure. See the !link for more information.', array('!link' => l('API documentation', 'http://api.drupal.org/api/drupal/includes--file.inc/function/file_create_htaccess')));

The different arguments to variable_get() can make things harder to debug, as well as likely being incompatible as soon as CMI comes on - since we'll no longer pass defaults every time we check a variable (at least I hope so).

1 days to next Drupal core point release.

Akaoni’s picture

Status: Needs work » Needs review
StatusFileSize
new5.33 KB

New patch fixes different variable_get() arguments and cleans things up a bit.
Test code hasn't changed.

Akaoni’s picture

StatusFileSize
new5.36 KB

Made it more better after IRC chat with catch.
Please let this be the last one... ;)

catch’s picture

OK this looks good to me. It's hard to explain what's going on here, but the alternative is completely failing out on people and/or risking a security issue on sites that aren't using apache but are relying on .htaccess.

chx’s picture

Status: Needs review » Needs work

one, the variable_get is repeated needlessly. Two, I think the api.drupal.org link needs to be file_save_htaccess not file_create_htaccess otherwise we are good here.

Akaoni’s picture

StatusFileSize
new5.34 KB

@catch, chx Thanks, guys!!

chx, another patch, just for you. ;)

Akaoni’s picture

Status: Needs work » Needs review
chx’s picture

Status: Needs review » Reviewed & tested by the community

Good to go.

sun’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/modules/system/system.install
@@ -356,6 +356,15 @@ function system_requirements($phase) {
+      $requirements['file system']['description'] = $t('Your web server has been detected as different from Apache so creation of .htaccess files is not being enforced. As such your file system may not be secure. See the !link for more information.', array('!link' => l('API documentation', 'http://api.drupal.org/file_save_htaccess')));

This is potentially one of the first messages/warnings/errors that users are going to see when installing Drupal.

It's unlikely that users are going to (be able to) switch to another web server when reading this, so the entire drama about "Apache" is information clutter.

"Files on this web server may not be stored securely. Configure your server securely."

Give or take grammar errors. ;)

We also need to link to a handbook page, not api.d.o.

-2 days to next Drupal core point release.

sun’s picture

Issue summary: View changes

Link to new patch.

Akaoni’s picture

Issue summary: View changes

New fix.

Akaoni’s picture

Issue summary updated.

In **further** IRC discussions with sun we could:

  • Expand the File Directories section of the handbook (http://drupal.org/node/394704) and link to that instead of api.drupal.org
  • Add automated creation of web.config file in private:// for IIS to prevent direct access

*sigh* The rabbit hole gets deeper...

yoroy’s picture

Glad to see sun pick up on this very wordy warning message. I think it would be useful to use the ".htaccess file" keyword somewhere in this message. Maybe the link in suns' example could be "Secure your server with .htaccess" or similar:

Files on this web server may not be stored securely. [link]Secure your server with .htaccess files[/link]
dries’s picture

This approach looks good.

+++ b/includes/file.inc
@@ -470,15 +470,46 @@ function file_ensure_htaccess() {
+  // Don't create .htaccess files if auto_create_htaccess variable is FALSE.
+  // For non-Apache web servers, don't create .htaccess files unless
+  // auto_create_htaccess variable has been explicitly set to TRUE.
+  $not_apache = stripos($_SERVER['SERVER_SOFTWARE'], 'apache') === FALSE;
+  $htaccess_variable = variable_get('auto_create_htaccess');
+  if ($htaccess_variable === FALSE || ($not_apache && !isset($htaccess_variable))) {
+    return;
+  }

Instead of checking this over and over again, why don't we set variable_get('auto_create_htaccess') at install time? Then, we can streamline this code to just check variable_get('auto_create_htaccess'). I think that would streamline the code a bit.

rbayliss’s picture

StatusFileSize
new5.4 KB

Just a reroll of Akaoni's patch from above to apply to the /core directory structure. Still needs changes based on comments from Sun in 44.

Akaoni’s picture

Thanks, rbayliss.

Unfortunately I can't commit anymore time to this one. :(
Hopefully someone can pick it up or we settle for a less ideal, but implementable now solution.

synth3tk’s picture

StatusFileSize
new5.28 KB

Includes the suggestions in #44 (sun), #45 (Akaoni), and #46 (yoroy).
("Files on this web server may not be stored securely. Secure your server with .htaccess files.")

synth3tk’s picture

Status: Needs work » Needs review
oriol_e9g’s picture

Status: Needs review » Needs work

I think that the message is fine in #50 but we still need to apply/discuss #47

gdd’s picture

Status: Needs work » Needs review
StatusFileSize
new0 bytes

Here is a new patch, which sets the variable in file.install per #47.

catch’s picture

Status: Needs review » Needs work

0 byte patch.

gdd’s picture

Status: Needs work » Needs review
StatusFileSize
new5.8 KB

Oops. We should make the bot complain about that maybe. New patch attached. Note that I did not add a hook_uninstall() because so far as I can tell, File module can not be uninstalled.

David_Rothstein’s picture

Status: Needs review » Needs work

Doing that in hook_install() seems like it would cause problems if you ever move your site to a different server. I think it's better to just check the server software each time (like in the earlier patches). It's not like this is code which runs often...

***

Overall, I'm somewhat concerned about the security implications of this patch. Currently, Drupal guarantees that if your server is configured to support .htaccess, it will provide this protection in the files directory. But this patch is going to make this security feature "opt-in" for anyone who isn't running Apache. I don't think that's a good change.

I think we need to go back to something like #22, which forces you to opt-out if you are having problems. The concern with that was that it's not discoverable, but couldn't we use a status report message to make it discoverable (i.e. combine the two approaches)? If someone is running into trouble with their site, they'll probably go to the status report page eventually, and if the warning there links to somewhere which tells them how to turn off .htaccess protection, we get the best of both worlds.

Although... that all depends on the person being able to make it to the status report page in the first place :) This issue refers to "errors" caused by .htaccess when using IIS + WebsitePanel, but I don't see any explanation of what the errors are. That seems like useful information for this issue. I did a little searching and came up with http://forum.arvixe.com/smf/general/installed-drupal-dotnetpanel-iis-mod... but I'm not even sure that's the same problem; it refers to a conflict between Drupal's .htaccess and one which is automatically created by the control panel software. If that's the problem then the solution may be different also.

NROTC_Webmaster’s picture

StatusFileSize
new5.75 KB

Instead of saying that anything not apache gets a .htaccess file created why not instead list the known exceptions? I did not do this because I'm not sure which ones will create the error.
I also updated this to default to creating the .htaccess file and assume that admin can turn it off if they need to.
Fix a typo in the install function.

NROTC_Webmaster’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, core-no_htaccess-360057-57.patch, failed testing.

randomtide’s picture

Patch in #57 doesn't even apply anymore to current D8 (2013-03-09).
Can we remove the "novice" tag from this issue in the meantime?

heddn’s picture

Issue tags: -Novice

Removing novice tag until its decided what approach will be used.

heddn’s picture

Issue summary: View changes

Not RTbC.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue summary: View changes

Updated issue summary.

Anonymous’s picture

If the goal is to put .htaccess in place if and only if it would be used by the webserver, would a possible be test be to generate a file and an .htaccess that would disallow access to the file? At that point, if the Drupal made a request to itself, it seems that it would be able to discover if .htaccess files are honored or not by whether it got an error or the file it requested.

If my understanding of the problem is correct, it seems that that would give us the knowledge of whether to create a file or whether to give some form of notification.

Anonymous’s picture

Issue summary: View changes

Updated issue summary.

jhedstrom’s picture

Version: 8.0.x-dev » 8.1.x-dev
Issue summary: View changes
Status: Needs work » Postponed

Moving to 8.1 for now. It might be accepted into 8.0 since it is classified as a bug if somebody wants to pick this back up.

catch’s picture

Version: 8.1.x-dev » 8.0.x-dev

There's no new API here, so it could feasibly be done in a patch release, moving back for now.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

20th’s picture

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rjzaar’s picture

This little patch just adds Options +FollowSymLinks to .htaccess for those who want to add it using composer patch.

EDIT: Mike Potter has a working solution to this issue: https://github.com/acquia/blt/issues/1135#issuecomment-285404408

Here is my application of his advice: https://github.com/rjzaar/opencourse/blob/8.6.x/composer.json
and the patch: https://github.com/rjzaar/opencourse/blob/8.6.x/scripts/Add-FollowSymLin...

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kim.pepper’s picture

kim.pepper’s picture

Version: 8.6.x-dev » 8.8.x-dev
Status: Postponed » Needs review
StatusFileSize
new26.1 KB
new2.62 KB

Here's a patch that builds on #2620304: htaccess functions should be a service to add a settings check to see if auto create is enabled/disabled.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dww’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll, +Bug Smash Initiative

This came up as a daily #bugsmash triage target.

The filename of the latest patch is "360057-combined-74.patch" and the comment says:

Here's a patch that builds on #2620304: htaccess functions should be a service to add a settings check to see if auto create is enabled/disabled.

So I think that includes all of what was committed for #2620304 plus the additional stuff to satisfy the needs of this issue. If @kim.pepper still has the Git branches lying around and can untangle that from what's already committed, great. Otherwise, someone will have to do that manually (which will probably be interesting / fun). 😉

kim.pepper’s picture

Version: 9.3.x-dev » 9.4.x-dev
Status: Needs work » Needs review
StatusFileSize
new4.43 KB

Re-wrote this for Drupal 9.4.x so there's no real useful interdiff.

kim.pepper’s picture

StatusFileSize
new4.42 KB
new447 bytes

Fixes incorrect comment.

kim.pepper’s picture

StatusFileSize
new4.42 KB
new802 bytes

Fix wrong service in constructor.

quietone’s picture

Status: Needs review » Needs work

@kim.pepper, thanks for updating the patch.

Some things I noticed

  1. It would be very helpful to have steps to manually test this.
  2. The change record needs an update
  3. The new setting needs to be added to settings.php with a comment.

I did some testing with ddev.
I installed the patch, removed sites/default/files/.htaccess, and then installed Drupal. After the install that .htaccess file existed.
I tried again, I added dded $settings['auto_create_htaccess'] = FALSE; to settings.local.php, removed sites/default/files/.htaccess, and then installed Drupal. After the install that .htaccess file did not exist. There is no warning in the logs. And the Status report has an error that the public directory is not fully protected.

Setting to NW for the above.

kim.pepper’s picture

Issue tags: +#pnx-sprint
kostyashupenko’s picture

Issue tags: -Needs reroll

No need for reroll

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley’s picture

+++ b/core/lib/Drupal/Core/File/HtaccessWriter.php
@@ -35,16 +42,27 @@ class HtaccessWriter implements HtaccessWriterInterface {
+    if (!$this->settings->get('auto_create_htaccess', TRUE)) {
+      $this->logger->warning('Auto-creating htaccess disabled.');
+      return;
+    }

Do we need both warnings in ensure and write functions? Also, should it really be a warning?

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave made their first commit to this issue’s fork.

smustgrave’s picture

Issue tags: -Needs backport to D7 +Needs steps to reproduce

So this one is coming as the "One" for bugsmash so thought I'd give it a quick look.

Took the patch in #82 and only slightly fixed up with today's checks: Constructor promotion, return types, deprecation version.

#87 I agree and changed that to an error()

Also tweaked the CR some but it was pretty straight forward.

Still NW for

The new setting needs to be added to settings.php with a comment.

kim.pepper’s picture

Status: Needs work » Needs review

Rebased on 11.x and added docs to default.settings.php

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Settings read well to me.

16 years and this one may be good

kim.pepper’s picture

Status: Reviewed & tested by the community » Needs review

Back to NR for the feedback from @acbramley

acbramley’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs steps to reproduce
poker10’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

I reviewed the MR and added some comments.

I think this needs an issue summary update, because these parts from IS are not implemented:

  • When Apache is reported as the web server, automatically create .htaccess files. Otherwise, do not create them and warn the user about the possible implications of this
  • New warning message in File System section of Status Report.

When manually tested this, I was unable to find the warning entry in the log (Auto-creating htaccess disabled.), as the file creation is blocked in the parent function (HtaccessWriter::ensure()), so if the auto_create_htaccess config is set to FALSE, it will never reach the code in HtaccessWriter::write(). Not sure it was the intent. Personally I think that we do not need this logging at all and it will be sufficient to create a follow-up to sort-out the errors in the status report, which are displayed if .htaccess files are missing and the auto_create_htaccess config is set to FALSE:

Private files directory - Not fully protected
See https://www.drupal.org/SA-CORE-2013-003 for information about the recommended .htaccess file which should be added to the private:// directory to help protect against arbitrary code execution.

I think it does not make sense to have it as errors anymore, when you use this new switch and disable it's creation on purpose.

Moving to NW for these.

Thanks!

kim.pepper’s picture

Issue summary: View changes
Status: Needs work » Needs review

Addressed feedback and updated the IS.

I think we can create a new requirements check for the config setting. However, there was some pushback (#44_) on showing warnings on a standard install, so we need to be sure this is considered.

poker10’s picture

However, there was some pushback (#44_) on showing warnings on a standard install, so we need to be sure this is considered.

Yes, I was thinking more about adjusting the existing error messages, which are displayed when .htaccess files are missing, so that these have a different wording when the auto_create_htaccess config is set to FALSE, or hide these messages entirely. But I think that is not in scope of this issue, thus I mentioned the potential follow-up. I agree that adding a new requirements check with another warning or so is not ideal.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Believe feedback has been addressed here.

If we need a follow up ping me and I’ll happily do that

kim.pepper’s picture

Added a CR about the new setting.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new235.17 KB

Updating issue credits.

I recreated @quietone's testing from #83 and yielded the same result.

Steps to test

  1. Delete sites/default/files/.htaccess
  2. sudo touch sites/default/files/.htaccess so that the webserver cannot write to it
  3. Set $settings['auto_create_htaccess'] = FALSE; in settings.php
  4. Visit status report - I still get an error about public files beingnot fully protected because I have the file, but it is empty - this is coming from this hunk in system_requirements
     foreach ($htaccessWriter->defaultProtectedDirs() as $protected_dir) {
          $htaccess_file = $protected_dir->getPath() . '/.htaccess';
          // Check for the string which was added to the recommended .htaccess file
          // in the latest security update.
          if (!file_exists($htaccess_file) || !($contents = @file_get_contents($htaccess_file)) || !str_contains($contents, 'Drupal_Security_Do_Not_Remove_See_SA_2013_003')) {
            $url = 'https://www.drupal.org/SA-CORE-2013-003';
            $requirements[$htaccess_file] = [
              // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
              'title' => new TranslatableMarkup($protected_dir->getTitle()),
              'value' => t('Not fully protected'),
              'severity' => REQUIREMENT_ERROR,
              'description' => t('See <a href=":url">@url</a> for information about the recommended .htaccess file which should be added to the %directory directory to help protect against arbitrary code execution.', [':url' => $url, '@url' => $url, '%directory' => $protected_dir->getPath()]),
            ];
          }
        } 

    - I think we need that code to be skipped if this flag is set to FALSE

kim.pepper’s picture

Status: Needs work » Needs review

Added a check for the setting in system_requirements().

kim.pepper’s picture

Fixed conflict with #3493718: Convert system_requirements() into OOP hooks and install time class and added check for setting in SystemRequirements::checkRequirements() instead of system_requirements().

quietone’s picture

Made some changes to the CR, https://www.drupal.org/node/3525119

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update

Believe feedback has been addressed regarding system_requirements

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 11.x - congrats folks, this was the oldest open bug in Drupal core, AKA 'the one ⭕️' in #bugsmash parlance

Published change record.

  • larowlan committed 4a6a0bc3 on 11.x
    Issue #360057 by kim.pepper, Akaoni, kbahey, smustgrave, gdd,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.