Pull remaining PHP handling into PHP module

nedjo - March 2, 2007 - 17:44
Project:Drupal
Version:7.x-dev
Component:base system
Category:task
Priority:normal
Assigned:jcnventura
Status:closed
Issue tags:PHP
Description

The page matching in block module - the code that determines if a block should be visible on a given page - is useful in many contexts. Several contrib modules have already duplicated it in part or in whole. To avoid duplication and increase standardization, we should pull it into a separate function.

Attached patch pulls page matching into a new function, drupal_page_match(), in common.inc. This shouldn't increase overhead, since block.module is one of the core required modules and so included in every page load.

AttachmentSizeStatusTest resultOperations
page_match.patch3.39 KBIgnoredNoneNone

#1

nedjo - April 8, 2007 - 03:15

A grep of the /modules directory in contrib suggests the relevant code from core is repeated in various ways in 14 contrib modules so far. So this simple patch would save hundreds of lines of surplus code in contrib as well as improving consistency.

#2

m3avrck - April 8, 2007 - 15:31

Nedjo, this is a great idea.

However, do you think we could take it on step farther and also encapsulate the proper form that goes along with this function too? Since that function isn't as useful without the form.

#3

erdemkose - April 8, 2007 - 19:13

This will make things easier. +1

But this may open some security holes in the future.

Module-X uses this function to make decision about visibility.
Site admin let User-A to configure this module.
User-A can evaluate PHP code if Module-X has a broken logic to set the visibility value. If User-A can set it above 2, he/she can do anything.

This may not be a common scenario but IMO we should at least explicitly warn module writers about it.

#4

chx - April 8, 2007 - 19:57

If this gets in, http://drupal.org/node/106559 needs to be revisited. If that gets in, this needs to be revisited.

#5

nedjo - April 9, 2007 - 20:19

@m3avrck, good point. Here's an updated patch that pulls the form fragment for page matching into a separate reusable function.

I've also pulled the relevant permission into a new more generalized one, 'use PHP for settings' (not entirely sure that's a self-explanatory name for the permission, any better suggestions?), that can be referenced by all modules offering PHP settings. This in itself is an important improvement. PHP access should always be restricted. Existing modules out there implementing page matching presumably either introduce an additional permission or leave this open to e.g. everyone with 'administer site configuration' permission.

@erdemkose, the new form handling and permission should help prevent such security holes.

@chx, thanks for the reference, these two patches seem to be complimentary. I'll try to keep an eye on http://drupal.org/node/106559 and update this patch accordingly.

(I haven't been able to test this patch yet as I can't get a new HEAD install going.)

AttachmentSizeStatusTest resultOperations
page_match_0.patch8.97 KBIgnoredNoneNone

#6

nedjo - May 21, 2007 - 02:51
Title:Pull page matching from block.module into separate function» Pull PHP page matching into PHP module; make page matching reusable

This now belongs in our new PHP module.

Additions/changes to the previous patch:

* move PHP input to php.module
* move drupal_eval() to php.module
* add an update to change existing 'use PHP for block visibility' permission to new 'use PHP for settings'

In sum: this patch accomplishes two aims: (1) complete the move to restrict potentially dangerous PHP input to the PHP module; (2) make page matching reusable to prevent the current proliferation of code copy and pasting.

AttachmentSizeStatusTest resultOperations
drupal-page-match-php.patch12.02 KBIgnoredNoneNone

#7

nedjo - May 21, 2007 - 02:56

I marked http://drupal.org/node/134779 a duplicate.

#8

nedjo - May 27, 2007 - 17:07

Refreshing the patch.

AttachmentSizeStatusTest resultOperations
drupal-page-match-php_0.patch4.91 KBIgnoredNoneNone

#9

David Strauss - May 27, 2007 - 17:40

Nice patch.

#10

Bèr Kessels - May 27, 2007 - 22:34

Tested on an upgraded database (not clean, upgraded all along from 3.x). And on a new, clean site.
Both work well. The upgrade code does what it needs to do: move the permissions to (ab)use PHP, if set, to 6.x php module.
The php module now handles all the functionality: when php.module is disabled I could not use 'php block visibility' to root the entire server :).

However, two minor things. IMO not enough to keep this patch from going in, but still enough to have a look at, or else document: As soon as I disabled PHP module, all blocks with PHP-for-block-visibility no longer showed up. This is probably a 'feature' but some may classify it as a bug.
The other is that we will have to document more properly the fact taht the function 'drupal_eval' is now only callable if php.module is enabled. Not many contribs use it, but this may (will) break some sites that don't check for an existing/enabled php.module.

#11

Zen - May 28, 2007 - 08:29
Status:needs review» needs work

The last patch looks like it is missing a chunk ...

Purely visual review for #6:

  • I think the php.install updates should probably be moved to system.install.
  • Should drupal_eval now be php_eval?
  • I believe that constant declarations now require phpdoc style comments for API module compatibility.
  • Permission: Use PHP during configuration perhaps?
  • I don't think the amendment to the description in the .info file is necessary. Makes sufficient sense as it was IMO.
  • Pre-existing bug that can probably be fixed here:
    +      $page_match = $block->pages ? drupal_page_match($block->visibility, $block->pages) : TRUE;
    The above line will return TRUE even if $block->visibility is set to PAGE_MATCH_ONLY_LISTED and $block->pages is empty.

I will test against HEAD once the patch in #8 has been re-rolled correctly. I also think that Bèr's point (on how to handle existing PHP configurations when the PHP module is disabled) is a good one.

Thanks Nedjo.
-K

#12

nedjo - July 1, 2007 - 08:41

Thanks for the quality reviews and apologies I haven't found time to follow up with a new version of the patch. I intend to, but obviously not in time for code freeze.

#13

Gábor Hojtsy - November 21, 2007 - 23:37

Wow, this would have been great, if done for D6. I wish this is solved early in D7, or given that it is a security related improvement, maybe even in D6.

#14

Wim Leers - March 25, 2008 - 02:19
Version:6.x-dev» 7.x-dev

#15

jcnventura - December 17, 2008 - 19:37
Status:needs work» needs review

About #11's last commentary, the solution is to return !$block->visibility instead of TRUE.

Taking into account that the patch in #8 is functionally complete and moves drupal_eval() out of common.inc and places it in the php module, I would recommend that it be committed. Work on creating a drupal_page_match in common.inc and removing that code from block.module could be done in a separate issue.

The attached code corresponds to #8, updated to Drupal 7's current snapshot.

João

AttachmentSizeStatusTest resultOperations
drupal-php-eval.patch7.26 KBIdleFailed: Failed to apply patch.View details | Re-test

#16

Dave Reid - December 18, 2008 - 01:29

Small comment, do we really need to inroduce a drupal_page_match() when we already have drupal_match_path?

#17

System Message - December 18, 2008 - 02:05
Status:needs review» needs work

The last submitted patch failed testing.

#18

jcnventura - February 6, 2009 - 15:01

I don't see any drupal_page_match() being introduced in the code..

#19

Dave Reid - February 6, 2009 - 15:05

It was in response to "Work on creating a drupal_page_match in common.inc and removing that code from block.module could be done in a separate issue." in #15. This will probably need some tests.

#20

jcnventura - March 17, 2009 - 21:29
Status:needs work» needs review

Rerolling the patch

AttachmentSizeStatusTest resultOperations
drupal-php-eval.patch6.92 KBIdleFailed: Failed to install HEAD.View details | Re-test

#21

System Message - April 2, 2009 - 17:31
Status:needs review» needs work

The last submitted patch failed testing.

#22

jcnventura - April 29, 2009 - 14:48

Apparently the testing system failed to install Drupal, and the only way to restart it is to re-upload the patch.

AttachmentSizeStatusTest resultOperations
drupal-php-eval_0.patch6.92 KBIdleFailed: Failed to install HEAD.View details | Re-test

#23

jcnventura - April 29, 2009 - 14:49
Status:needs work» needs review

#24

System Message - April 29, 2009 - 15:05
Status:needs review» needs work

The last submitted patch failed testing.

#25

jcnventura - April 29, 2009 - 18:12
Status:needs work» needs review

Idem as #22

AttachmentSizeStatusTest resultOperations
drupal-php-eval_0.patch6.92 KBIdleFailed: Failed to install HEAD.View details | Re-test

#26

System Message - April 29, 2009 - 18:25
Status:needs review» needs work

The last submitted patch failed testing.

#27

jcnventura - April 29, 2009 - 18:31
Status:needs work» needs review

Last try..

AttachmentSizeStatusTest resultOperations
drupal-php-eval.patch6.92 KBIdleFailed: Failed to install HEAD.View details | Re-test

#28

System Message - April 29, 2009 - 18:35
Status:needs review» needs work

The last submitted patch failed testing.

#29

nedjo - April 29, 2009 - 19:23

Thanks for picking up the patch. Looks generally good, I haven't tested.

Currently PHP is used in contrib in many places for settings that don't have to do with visibility. So, to ensure this permission is broadly usable, it would be better to rename the permission "Use PHP for settings" instead of "Use PHP for visibility settings". Suggested description: "Enter PHP in settings fields where PHP is allowed. %warning"

#30

Arancaytar - April 29, 2009 - 21:52

jcnventura: As a rule of thumb, if the testbot can't install the patched site the first two times, it's probably the patch. ;)

Edit: (Assuming the testing platform is working, which you can see by find if it is passing other patches.)

#31

jcnventura - April 30, 2009 - 09:09
Assigned to:nedjo» jcnventura
Status:needs work» needs review

@Arancaytar: it failed to install HEAD.. That has nothing to do with the patch itself.. Try downloading HEAD yourself and apply the patch. You'll see that it applies perfectly and passes all tests. You may be able to see in #20, that the patch was working perfectly until April 1st, and then the testing system began having issues to install Drupal itself. I had a patch in #222926: htmlcorrector filter escapes HTML comments that failed for the exact same reasons at the time I was trying to upload this.

I will change the patch according to nedjo's description in #29 and try again.

João

#32

Damien Tournoud - April 30, 2009 - 09:16
Status:needs review» needs work

Given that your patch was the only one to fail during the period, yes, that's definitely the patch. Probably some E_ALL warning somewhere. That it "works for you" doesn't mean it should work for everyone else.

#33

Damien Tournoud - April 30, 2009 - 09:19

Fatal error: Cannot redeclare system_update_7021()

So it is not the patch?

#34

jcnventura - April 30, 2009 - 09:54

Hi Damien,

Thanks for that.. Indeed it is in the patch.. Because of the similar problem in #222926: htmlcorrector filter escapes HTML comments, I was assuming it was Drupal.org being stubborn, especially because the error was "failed to install HEAD" and not "Cannot redeclare system_update_7021" as it should be...

I will move it to system_update_7999() until this becomes reviewed by the community so that I don't have to be tracking Drupal's last system_update every day..

João

#35

Arancaytar - April 30, 2009 - 13:16

"Failed to install HEAD" means that the patch broke the installer. Those are all the details you get in the summary - if the patch fails, it's either because the patch failed to apply, or because it introduces a syntax error, or because the patched Drupal is unable to install, or because a test case fails after it has installed.

However, I would also like a more detailed view on the PIFR page. Counting failures and passes isn't very helpful when you need to know which assertion failed, exactly - or what the "exception" (ie notice/warning) was. You end up having to run the unit test yourself, unless I missed a link somewhere.

#36

jcnventura - April 30, 2009 - 14:04

"Failed to install HEAD" means that the patch broke the installer.

@Arancaytar: sometimes it means option e) that the installer is broken. At the time I was re-uploading this, as I mentioned 2 times before, I was also re-uploading a patch that started working after uploading it twice. That this patch was telling me the EXACT SAME ERROR at the EXACT SAME TIME, led me to believe naturally, that the solution would be EXACTLY THE SAME: re-upload it.

Thanks to Damien Tournoud, I was able to find out the root cause of the problem in this case.

#37

jcnventura - May 4, 2009 - 22:12
Status:needs work» needs review

OK,

I have changed the name of the permission as requested by nedjo in #29. The system.install update is 7022, so it may expire soon.

João

AttachmentSizeStatusTest resultOperations
drupal-php-eval.patch6.88 KBIdleFailed: Failed to install HEAD.View details | Re-test

#38

System Message - May 7, 2009 - 08:00
Status:needs review» needs work

The last submitted patch failed testing.

#39

jcnventura - May 7, 2009 - 09:21
Status:needs work» needs review

Bumping system_update to 7023 and hoping for the best..

João

AttachmentSizeStatusTest resultOperations
drupal-php-eval.patch6.88 KBIdlePassed: 11182 passes, 0 fails, 0 exceptionsView details | Re-test

#40

nedjo - May 7, 2009 - 13:56
Title:Pull PHP page matching into PHP module; make page matching reusable» Security: pull remaining PHP handling into PHP module
Status:needs review» needs work

Removing obsolete part of title.

We should also change the name and description of the php.module in the php.info file. Currently it cites only the PHP filter:

name = PHP filter
description = Allows embedded PHP code/snippets to be evaluated.

Suggested change:

name = PHP
description = Enables the use of PHP in settings and provides a filter allowing embedded PHP code/snippets to be evaluated.

#41

nedjo - May 7, 2009 - 14:03

Or maybe simply remove "filter" from the name and leave the existing concise description.

#42

nedjo - May 7, 2009 - 14:08
Status:needs work» needs review

I was forgetting that Ber suggested in #11 the .info change wasn't needed. Resetting to needs review.

#43

Damien Tournoud - May 7, 2009 - 14:22
Title:Security: pull remaining PHP handling into PHP module» Pull remaining PHP handling into PHP module
Status:needs review» reviewed & tested by the community

Very nice clean up, I want. Don't abuse security: this solves no security issue by itself.

#44

webchick - May 7, 2009 - 15:31

Oh, what a great little patch! Also, yay for permission descriptions, since "use PHP for settings" isn't descriptive enough (and I don't really see any alternatives), but the actual description of the permission is great and very clear. Hopefully with drupal_eval() moving to PHP module, we'll see contrib somewhat forced to conform to this standard as well, instead of having to set 50 different "use PHP for blah" permissions.

Committed to HEAD. Great job! :D

Gábor mentioned in #13 that it'd be great to have this in 6, and I agree it would've been, but a major API change like this is far too late for that now, I think.

Marking "needs work" because we need this to be documented at the 6.x -> 7.x upgrade page.

#45

webchick - May 7, 2009 - 15:34
Status:reviewed & tested by the community» needs work
Issue tags:+Needs Documentation

Hm, I should read the tags before I commit stuff. ;) "Needs tests" is also true. We can probably pick that up in another issue, though, since I don't think any of the block visibility settings are tested atm.

#46

Gábor Hojtsy - May 7, 2009 - 15:36

Well, right, it is too late for Drupal 6. So sad.

#47

jcnventura - May 7, 2009 - 17:43

webchick, thanks for committing the patch! After 3029 commits to drupal contrib, this is the first time that code modified by me gets to be committed to Drupal core.. Yay!

Indeed, I didn't modify any test because I couldn't find one that I could modify.. In the existing php.test, the PHP filter functionality test (function testPHPFilter()) executes some PHP code introduced in a node with the PHP filter.. The execution of that filter is handled by the newly renamed php_eval() function. The only part not tested is, as you indicated, the use of PHP for visibility settings (which in core is done only by the block module).
Can we add this to a list of TBD tests for the block module and mark this issue fixed?

And Gábor, don't you dare commit this to Drupal 6... The two modules I maintain both use drupal_eval, and I would hate to have to answer to users saying that their visibility no longer works because they don't have the PHP module enabled..

As to the documentation, one needs to say that drupal_eval() is now called php_eval() and that calls to drupal_eval() should be wrapped in a call to if (module_exists('php')). And that the 'use PHP for settings' should be used.

João

#48

webchick - May 7, 2009 - 18:43
Issue tags:-Needs tests

Yay, congrats for the commit! :D And agreed, it looks like tests for block visibility are at #370172: refactor block visibility.

As to the documentation, one needs to say that drupal_eval() is now called php_eval() and that calls to drupal_eval() should be wrapped in a call to if (module_exists('php')). And that the 'use PHP for settings' should be used.

Right. João, can you edit the page to add that in, please? Then feel free to mark this back as fixed.

#49

jcnventura - May 7, 2009 - 21:19

@webchick: Just to confirm, the page to be edited is http://drupal.org/node/224333?

#50

tstoeckler - May 7, 2009 - 21:41

That's the one.

#51

jcnventura - May 7, 2009 - 23:50
Status:needs work» fixed

Documented at http://drupal.org/node/224333#php_eval

PS: Can someone remove the 'Needs documentation' tag? I don't seem to have permissions for that...

#52

tstoeckler - May 8, 2009 - 00:51
Issue tags:-Needs Documentation

Yes you do =)

#53

System Message - May 22, 2009 - 01:00
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.