elastic / elastic/elastic-transport-php

ArgumentCountError in logHeaders when HTTP body contains percent signs

Open Beginner friendly
#41 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
21
Forks
20
PR merge metrics
No merged PRs in 30d

Description

## Description

The `logHeaders()` method in `Transport.php` uses `sprintf()` with the HTTP message body, which causes an `ArgumentCountError` when the body contains `%` characters followed by format specifier characters (common in URL-encoded data).

## Steps to Reproduce

1. Perform a bulk indexing operation where document content contains `%` characters (e.g., URL-encoded strings like `%20`, `%3A`, etc.)
2. The error occurs during the `sprintf()` call in `logHeaders()`

## Error Message

```
ArgumentCountError in sprintf called at vendor/elastic/transport/src/Transport.php (280)
```

## Stack Trace

```
in Elastic\Transport\Transport::logHeaders called at Transport.php (296)
in Elastic\Transport\Transport::logRequest called at Transport.php (325)
in Elastic\Transport\Transport::sendRequest called at Client.php (202)
in Elastic\Elasticsearch\Client::sendRequest
in Elastic\Elasticsearch\Client::bulk
```

## Root Cause

In `src/Transport.php` lines 275-284:

```php
private function logHeaders(MessageInterface $message): void
{
$this->logger->debug(sprintf(
"Headers: %s\nBody: %s",
json_encode($message->getHeaders()),
(string) $message->getBody() // <-- If this contains %, sprintf fails
));

$message->getBody()->rewind();
}
```

The `sprintf()` arguments are evaluated **before** `$this->logger->debug()` is called, so even with a `NullLogger`, the error still occurs when the body contains patterns like `%s`, `%d`, `%20`, etc.

## Suggested Fix

Replace `sprintf()` with string concatenation:

```php
private function logHeaders(MessageInterface $message): void
{
$this->logger->debug(
"Headers: ".json_encode($message->getHeaders()).
"\nBody: ".(string) $message->getBody()
);

$message->getBody()->rewind();
}
```

## Environment

- PHP: 8.1+
- elastic/transport: v9.0.1
- elasticsearch/elasticsearch: ^9.1

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in src/Transport.php at Transport::logHeaders and reproduce the bulk request with a body containing URL-encoded percent sequences such as %20. Done means logging completes without ArgumentCountError, the request body is still rewound, and the existing request flow continues normally.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.