Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Headers invalid argument in addHeaderLine method #116

Closed
necromant2005 opened this issue Mar 9, 2017 · 4 comments
Closed

Headers invalid argument in addHeaderLine method #116

necromant2005 opened this issue Mar 9, 2017 · 4 comments

Comments

@necromant2005
Copy link

Headers failing by sending empty "user-agent"
The main issue in Headers::addHeaders

if (is_string($value)) {
   $this->addHeaderLine($value);

But below in method addHeaderLine signature

    public function addHeaderLine($headerFieldNameOrLine, $fieldValue = null)
    {
        $matches = null;
        if (preg_match('/^(?P<name>[^()><@,;:\"\\/\[\]?=}{ \t]+):.*$/', $headerFieldNameOrLine, $matches)
            && $fieldValue === null) {
            // is a header
            $headerName = $matches['name'];
            $headerKey  = static::createKey($matches['name']);
            $line = $headerFieldNameOrLine;
        } elseif ($fieldValue === null) {
            throw new Exception\InvalidArgumentException('A field name was provided without a field value');
        }

Witch is wrong typing NULL on string only fields

@Xerkus
Copy link
Member

Xerkus commented Mar 9, 2017

Headers without value are invalid.
https://tools.ietf.org/html/rfc7230#section-3.2

What is trying to send empty user-agent?

@weierophinney
Copy link
Member

@necromant2005

I'm unclear what you expect to occur. If the idea is to unset the header, you would need to use:

if ($headers->has($header)) {
    $headers->removeHeader($headers->get($name));
}

If the idea is to set an empty value, as @Xerkus notes, that's invalid per the HTTP specifications. You could do something like this:

$headers->addHeaderLine($header, '');

Both of the above would require that you validate the value being provided before you try and create the header, which is likely a good idea anyways.

We do not intend to support the usage $headers->addHeaderLine($value) where the value is not a fully-formed header line, but just the header name, as its purpose would be too vague:

  • Should it remove such a header, if it's already present?
  • Or set a header with an empty string as a value (invalid by the specification)?

Neither feels like a valid option.

@necromant2005
Copy link
Author

I'm getting error on production because some bots comes with empty "user-agent" headers

file:vendor/zendframework/zend-servicemanager/src/ServiceManager.php | 
ln:765 | 
msg: message => 'Uncaught Zend\\Http\\Exception\\InvalidArgumentException: A field name was provided without a field value in vendor/zendframework/zend-http/src/Headers.php:192

Stack trace:
#0 vendor/zendframework/zend-http/src/Headers.php(155): Zend\\Http\\Headers->addHeaderLine(\'User-Agent\')
#1 vendor/zendframework/zend-http/src/PhpEnvironment/Request.php(238): Zend\\Http\\Headers->addHeaders(Array)
#2 vendor/zendframework/zend-http/src/PhpEnvironment/Request.php(86): Zend\\Http\\PhpEnvironment\\Request->setServer(Object(Zend\\Stdlib\\Parameters))
#3 vendor/zendframework/zend-mvc/src/Service/RequestFactory.php(29): Zend\\Http\\PhpEnvironment\\Request->__construct()
#4 vendor/zendframework/zend-servicemanager/src/ServiceManager.php(758): Zend\\Mvc\\Service\\RequestFactory->__invoke(Object(Zend\\ServiceManage',
  'file' => 'vendor/zendframework/zend-servicemanager/src/ServiceManager.php',
  'line' => 765,
) => array (
)

You're right, there shouldn't be requests with empty value, but they are. It's incorrect zf behaviour to fail on every request, I'm expecting something simpler - "skip it and move forward"

@Xerkus
Copy link
Member

Xerkus commented Mar 10, 2017

@weierophinney may be he is on to something here, it is actually zend-mvc related issue, exception in request factory will result in uncaught exception and error log entry while it should be normal abort with 400 Bad Request

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants