elastic / elastic/elastic-transport-php

TypeError in sanitizeBody() when request body contains terms query

Open
#43 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

When `OTEL_PHP_INSTRUMENTATION_ELASTICSEARCH_CAPTURE_SEARCH_QUERY=sanitize` is set, any Elasticsearch request containing a terms query throws:

```php
TypeError: preg_match(): Argument #2 ($subject) must be of type string, int given
at OpenTelemetry.php:117
```

The bug is in [src/OpenTelemetry.php](https://github.com/elastic/elastic-transport-php/blob/d6500b4cbb7d68fbbf0f60c50c9f4f39b53d0fdd/src/OpenTelemetry.php#L102) in the sanitizeBody() method, called from redactBody() at line 67, which is called from addOtelAttributes() in the EndpointTrait.

`sanitizeBody()` uses array_walk_recursive and passes each key to preg_match(). A terms query like `{"terms": {"field": ["val1", "val2"]}}` produces leaf values with integer keys (0, 1, ...), which preg_match() rejects in PHP 8+.

I can make a PR with a proposed fix:

```
- if (preg_match($regex, $key, $matches)) {
+ if (preg_match($regex, (string) $key, $matches)) {
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in src/OpenTelemetry.php, tracing addOtelAttributes() through redactBody() to sanitizeBody(). Reproduce the failure with a terms query while OTEL_PHP_INSTRUMENTATION_ELASTICSEARCH_CAPTURE_SEARCH_QUERY=sanitize is set, then verify sanitization handles integer-indexed values without throwing a TypeError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.