Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mime] Throw InvalidArgumentException on invalid form field type inside array #52033

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

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Symfony/Component/Mime/Part/Multipart/FormDataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ public function __construct(array $fields = [])
{
parent::__construct();

foreach ($fields as $name => $value) {
if (!\is_string($value) && !\is_array($value) && !$value instanceof TextPart) {
throw new InvalidArgumentException(sprintf('The value of the form field "%s" can only be a string, an array, or an instance of TextPart ("%s" given).', $name, get_debug_type($value)));
}
$this->fields = $fields;

$this->fields[$name] = $value;
}
// HTTP does not support \r\n in header values
$this->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
}
Expand Down Expand Up @@ -75,6 +70,10 @@ private function prepareFields(array $fields): array
return;
}

if (!\is_string($item) && !$item instanceof TextPart) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can move this check to the constructor directly? That would be more consistent as we already check some of that there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also considered this, but in my opinion it has two disadvantages:

  1. We have to traverse through the array recursively. In prepareFields this is already the case, but in the constructor we only traverse at top-level.
  2. We don't have the field name in form of "foo[qux][quux]" in the constructor. Either we cannot include it in the error message, or we have to generate it redundantly.

Also we already have a check (regarding the form field values with integer keys) inside prepareFields, so I don't feel like adding another check there affects the consistency much.

How about we remove the check in the constructor altogether? All the checks could be done inside prepareFields and the constructor would look like this:

 public function __construct(array $fields = [])
    {
        parent::__construct();

        $this->fields = $fields;

        // HTTP does not support \r\n in header values
        $this->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
    }

Copy link
Member

Choose a reason for hiding this comment

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

Having all the checks in prepareFields works for me.

throw new InvalidArgumentException(sprintf('The value of the form field "%s" can only be a string, an array, or an instance of TextPart, "%s" given.', $fieldName, get_debug_type($item)));
}

$values[] = $this->preparePart($fieldName, $item);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ public function testExceptionOnFormFieldsWithIntegerKeysAndMultipleValues()
$f->getParts();
}

public function testExceptionOnFormFieldsWithDisallowedTypesInsideArray()
{
$f = new FormDataPart([
'foo' => [
'bar' => 'baz',
'qux' => [
'quux' => 1,
],
],
]);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The value of the form field "foo[qux][quux]" can only be a string, an array, or an instance of TextPart, "int" given.');
$f->getParts();
}

public function testToString()
{
$p = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif');
Expand Down