Skip to content

Commit 80dab97

Browse files
authored
feat: exclude headers (#115)
1 parent 41f756f commit 80dab97

File tree

5 files changed

+140
-6
lines changed

5 files changed

+140
-6
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,95 @@ you have them, simply add them to your `.ENV` file:
9494
TREBLLE_API_KEY=YOUR_API_KEY
9595
TREBLLE_PROJECT_ID=YOUR_PROJECT_ID
9696
```
97+
98+
## Configuration
99+
100+
Treblle Laravel SDK provides several configuration options that can be customized in your `config/treblle.php` file. If the config file doesn't exist, you can publish it using:
101+
102+
```bash
103+
php artisan vendor:publish --provider="Treblle\Laravel\TreblleServiceProvider"
104+
```
105+
106+
### Available Configuration Options
107+
108+
#### API Settings
109+
```php
110+
// Enable/disable Treblle monitoring
111+
'enable' => env('TREBLLE_ENABLE', true),
112+
113+
// Treblle API credentials
114+
'api_key' => env('TREBLLE_API_KEY'),
115+
'project_id' => env('TREBLLE_PROJECT_ID'),
116+
117+
// Override API URL (for debugging/testing)
118+
'url' => null,
119+
```
120+
121+
#### Environment Control
122+
```php
123+
// Environments where Treblle should be disabled
124+
'ignored_environments' => env('TREBLLE_IGNORED_ENV', 'dev,test,testing'),
125+
```
126+
127+
#### Data Masking
128+
```php
129+
// Fields that will be masked in request/response bodies
130+
'masked_fields' => [
131+
'password',
132+
'pwd',
133+
'secret',
134+
'password_confirmation',
135+
'cc',
136+
'card_number',
137+
'ccv',
138+
'ssn',
139+
'credit_score',
140+
'api_key',
141+
],
142+
```
143+
144+
#### Header Exclusion
145+
```php
146+
// Headers that will be excluded from logging
147+
'excluded_headers' => [
148+
'authorization', // Exact match (case-insensitive)
149+
'x-api-key', // Exact match (case-insensitive)
150+
'cookie', // Exact match (case-insensitive)
151+
'x-*', // Wildcard: all headers starting with 'x-'
152+
'*-token', // Wildcard: all headers ending with '-token'
153+
'/^x-(api|auth)-/i', // Regex: headers starting with 'x-api-' or 'x-auth-'
154+
],
155+
```
156+
157+
**Pattern Support for Header Exclusion:**
158+
159+
- **Exact match**: `'authorization'` matches exactly "authorization" (case-insensitive)
160+
- **Wildcards**: `'x-*'` matches any header starting with "x-", `'*-token'` matches headers ending with "-token"
161+
- **Regex patterns**: Full regex patterns like `'/^x-(api|auth)-/i'` for advanced matching
162+
163+
#### Debug Mode
164+
```php
165+
// Enable debug mode (development only)
166+
'debug' => env('TREBLLE_DEBUG_MODE', false),
167+
```
168+
169+
### Environment Variables
170+
171+
All configuration options can be controlled via environment variables:
172+
173+
```shell
174+
# Core settings
175+
TREBLLE_ENABLE=true
176+
TREBLLE_API_KEY=your_api_key
177+
TREBLLE_PROJECT_ID=your_project_id
178+
179+
# Environment control
180+
TREBLLE_IGNORED_ENV=dev,test,testing
181+
182+
# Debug mode (development only)
183+
TREBLLE_DEBUG_MODE=false
184+
```
185+
97186
## Enabling Treblle on your API
98187

99188
Your first step should be to register Treblle into your in your middleware aliases in `app/Http/Kernel.php`:

config/treblle.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
'api_key',
4545
],
4646

47+
/*
48+
* Define which headers should be excluded from logging
49+
*/
50+
'excluded_headers' => [],
51+
4752
/*
4853
* Should be used in development mode only.
4954
* Enable Debug mode, will throw errors on apis.

src/DataProviders/LaravelRequestDataProvider.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Carbon\Carbon;
88
use Treblle\Php\FieldMasker;
99
use Treblle\Php\DataTransferObject\Request;
10+
use Treblle\Laravel\Helpers\HeaderProcessor;
1011
use Treblle\Php\Contract\RequestDataProvider;
1112
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1213

@@ -27,9 +28,7 @@ public function getRequest(): Request
2728
user_agent: $this->request->userAgent() ?? '',
2829
method: $this->request->method(),
2930
headers: $this->fieldMasker->mask(
30-
collect($this->request->headers->all())->transform(
31-
fn ($item) => collect($item)->first(),
32-
)->toArray()
31+
HeaderProcessor::process($this->request->headers->all())
3332
),
3433
query: $this->fieldMasker->mask($this->request->query->all()),
3534
body: $this->fieldMasker->mask($this->getRequestBody()),

src/DataProviders/LaravelResponseDataProvider.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Http\JsonResponse;
99
use Treblle\Php\DataTransferObject\Error;
1010
use Treblle\Php\Contract\ErrorDataProvider;
11+
use Treblle\Laravel\Helpers\HeaderProcessor;
1112
use Treblle\Php\DataTransferObject\Response;
1213
use Treblle\Php\Contract\ResponseDataProvider;
1314
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
@@ -48,9 +49,7 @@ public function getResponse(): Response
4849
json_decode($body, true) ?? []
4950
),
5051
headers: $this->fieldMasker->mask(
51-
collect($this->response->headers->all())->transform(
52-
fn ($item) => collect($item)->first(),
53-
)->toArray()
52+
HeaderProcessor::process($this->response->headers->all())
5453
),
5554
);
5655
}

src/Helpers/HeaderProcessor.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Treblle\Laravel\Helpers;
6+
7+
final class HeaderProcessor
8+
{
9+
public static function process(array $headers): array
10+
{
11+
$excludedHeaders = config('treblle.excluded_headers', []);
12+
13+
return collect($headers)
14+
->transform(fn ($item) => collect($item)->first())
15+
->reject(function ($value, $key) use ($excludedHeaders) {
16+
foreach ($excludedHeaders as $pattern) {
17+
// Convert shell-style wildcards to regex if needed
18+
$regex = self::convertPatternToRegex($pattern);
19+
if (preg_match($regex, $key)) {
20+
return true;
21+
}
22+
}
23+
24+
return false;
25+
})
26+
->toArray();
27+
}
28+
29+
private static function convertPatternToRegex(string $pattern): string
30+
{
31+
// If it's already a regex (starts and ends with delimiters), use as-is
32+
if (preg_match('/^\/.*\/[gimxsu]*$/', $pattern)) {
33+
return $pattern;
34+
}
35+
36+
// Convert shell-style pattern to regex
37+
$regex = preg_quote($pattern, '/');
38+
$regex = str_replace(['\*', '\?'], ['.*', '.'], $regex);
39+
40+
return '/^' . $regex . '$/i';
41+
}
42+
}

0 commit comments

Comments
 (0)