Transferring this issue from security.drupal.org
The Security Team considered including this in SA-2014-06, but after debate about unexpected side effects decide to have it as a public issue and commit the prepared patch soon after the SA.
catch agreed that this could be committed to 7.x and 6.x in advance of an 8.x patch, as if it had been part of the SA.
Commit credit: klausi, pwolanin, tsphethean, sun
Problem/Motivation
posting an array as value of a required and maxlength limited textfield will cause the field to pass both the #required => TRUE and #maxlength validation.
Proposed resolution
Enforce the expected data type in the Form API
Remaining tasks
commit this patch for 7 and 6, make follow-up issues for other form elements
User interface changes
API changes
FAPI change to enforce correct data type of certain fields
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | increment.txt | 1.32 KB | pwolanin |
| #23 | 2380053-D6-23.patch | 4.21 KB | pwolanin |
| #22 | 2380053-D6-22.patch | 3.76 KB | pwolanin |
| #15 | increment.txt | 3.21 KB | pwolanin |
| #15 | 2380053-D6-15.patch | 3.73 KB | pwolanin |
Comments
Comment #1
pwolanin commentedComment #2
gregglesI should not get commit credit - I commented, but didn't work on the patch specifically.
Comment #3
pwolanin commentedComment #4
tstoecklerThis makes a lot of sense. I had thought about how to fix this problem in the light of the SA but did not come up with a solution. A #value_callback is a very elegant solution and is exactly the right spot. Nice!
Leaving for some others to review, but to me this is fine.
If contrib or custom modules have set their own #value_callback's this would not conflict (all though they of course would not get the added security) so the only impact I see with this would be contrib modules relying on arrays to be able to be input into textfields and textareas. Which I doubt anyone realized was possible before the SA and doubt even more anyone is actively expecting.
What is seemingly not supported in the patch is passing e.g. integers into a textarea (not a textfield) programmatically. I don't think this is a very common use-case either, so from my point of view this would be good to go.
Comment #5
pwolanin commented@tstockler - yeah, I don't think putting an int into a textarea was much of a valid use case, compared to it being more likely for a textfield.
This patch was reviewed and rtbc by the Security team, so it shouldn't need further change before commit unless there is a bug.
Comment #6
tstoecklerahh yes, I missed that this was already marked RTBC. awesome
Comment #7
David_Rothstein commentedI think the point about integers being possible in a programmatically-submitted textarea is a good one, actually. (Also, what about password confirm elements, where it might be more likely?) Let's not break any such code, if it exists. Here's a reroll that allows scalars in those cases, plus adds tests for that. I also fixed a few other minor things.
---
A little more background on the security team discussion:
We decided we can go ahead and fix this for the core text-like fields via the patches here first, to make some progress. But the same problem seems to exist for any other form element that expects a string (regardless of whether it's a text-like field or not). So as a followup, unless we want every form element in Drupal core and contrib to deal with this via a custom #value_callback we talked about some other ideas:
<input name="some_name[some_key]">on it, we expect $_POST['some_name']['some_key'] to be a string (if it's present at all) but not an array.This might be an ugly and fragile solution due to Drupal's architecture, but technically it's the correct, comprehensive fix - it ensures that no one messes with the array vs. string structure of any part of the form as it was built server-side.
Comment #8
David_Rothstein commentedComment #10
pwolanin commentedChanges looks fine, though we'll need to make the same adjustments to the Drupal 6 patch.
Comment #11
catchTagging as a reminder.
Comment #12
David_Rothstein commentedCommitted to 7.x - thanks!
So... this goes to Drupal 8 now to forward-port there first? (I'm not sure what the prospects of getting this committed to Drupal 6 are.)
Comment #14
pwolanin commentedDid this go into 6.x also?
Comment #15
pwolanin commentedTakes David's changes into account for Drupal 6.x
Comment #16
David_Rothstein commentedUpdating tags, since 7.35 was a security release instead.
Comment #17
David_Rothstein commentedDecided to add a change notice here, since there are some recommended changes as a result of this that probably apply to contrib modules who define their own form element types, or individual form elements with value callbacks, etc.
https://www.drupal.org/node/2462723
Comment #18
rajab natshahTesting :)
Comment #19
klausiThis caused a regression with input handling for textareas, see #2502263: Drupal 7.36 regression: hidden field textarea #default_value is ignored.
Comment #20
pwolanin commentedCan we get a RTBC for 6 or no?
Comment #21
dawehnerI kinda like the explicit
return NULL;for exampleMh out of scope, but I'm curious why we need to
str_replace()hereComment #22
pwolanin commentedReroll for conflict due to DRUPAL-SA-CORE-2015-003
Comment #23
pwolanin commentedinclude fix from #2502263: Drupal 7.36 regression: hidden field textarea #default_value is ignored
Comment #24
pwolanin commentedmoved 8.0.x issue to #2560137: Posting an array as value of a form element is allowed even when a string is expected (and bypasses #maxlength constraints) - first step: text fields so we are not stuck waiting for 6.x
Comment #26
dsnopekSince this issue is now only about Drupal 6, moving to the D6LTS queue and re-opening.
Comment #27
dsnopekPut [core] into the title for easier sorting in this queue
Comment #28
dalinIn #7 it was mentioned:
> For the fundamental issue here (arrays being allowed where string are expected) we could not think of any direct security implications.
And I don't see anything else in this thread that suggests otherwise, so I think it makes sense to demote this.
Comment #29
izmeez commentedUsing patch in #23 for sometime without problems on php 7.3
Comment #30
izmeez commentedContinuing to use this for sometime without problems. Is it time to reconsider for commit?