Skip to content

Commit c91c5d6

Browse files
author
Andrey Helldar
authored
Merge pull request #3 from TheDragonCode/1.x
Added the ability to disable the hiding of secret keys
2 parents dbc599a + f67258b commit c91c5d6

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

config/http-logger.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<?php
22

33
return [
4-
'enabled' => env('LOG_HTTP_ENABLED', true),
4+
'enabled' => env('HTTP_LOG_ENABLED', true),
55

66
'connection' => env('DB_CONNECTION'),
77

88
'table' => 'http_logs',
99

1010
'hide' => [
11-
'authorization',
12-
'token',
13-
'access_token',
14-
'password',
15-
'password_confirmation',
11+
'enabled' => env('HTTP_LOG_HIDE_ENABLED', true),
12+
13+
'keys' => [
14+
'authorization',
15+
'token',
16+
'access_token',
17+
'password',
18+
'password_confirmation',
19+
],
1620
],
1721
];

src/Casts/Hide.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ public function get($model, string $key, $value, array $attributes): array
1919

2020
public function set($model, string $key, $value, array $attributes): string
2121
{
22-
$value = $this->process((array) $value);
22+
if ($this->enabled()) {
23+
$value = $this->process((array) $value);
24+
}
2325

24-
return json_encode($value);
26+
return json_encode((array) $value, JSON_NUMERIC_CHECK);
2527
}
2628

2729
protected function process(array $values): array
@@ -54,8 +56,13 @@ protected function hide(mixed $value): string
5456
return str_pad('', $length, $this->mask);
5557
}
5658

59+
protected function enabled(): bool
60+
{
61+
return (bool) config('http-logger.hide.enabled', true);
62+
}
63+
5764
protected function hides(): array
5865
{
59-
return config('http-logger.hide', []);
66+
return config('http-logger.hide.keys', []);
6067
}
6168
}

tests/HideTest.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class HideTest extends TestCase
1010
{
11-
public function testLogging(): void
11+
public function testEnabled(): void
1212
{
1313
$method = 'POST';
1414
$name = 'api.pages.create';
@@ -70,4 +70,69 @@ public function testLogging(): void
7070
'content-type' => ['application/x-www-form-urlencoded'],
7171
], $log->headers);
7272
}
73+
74+
public function testDisabled(): void
75+
{
76+
config(['http-logger.hide.enabled' => false]);
77+
78+
$method = 'POST';
79+
$name = 'api.pages.create';
80+
$path = 'api/pages';
81+
82+
$uri = $path . '?' . http_build_query([
83+
'foo' => 'Foo',
84+
'token' => 123,
85+
'access_token' => 456,
86+
]);
87+
88+
$this->assertDatabaseLogsCount(0);
89+
90+
$response = $this->post($uri, [
91+
'bar' => 'Bar',
92+
93+
'password' => 'q123456',
94+
'password_confirmation' => 'q123456',
95+
], [
96+
'Authorization' => 'Bearer QwErTy',
97+
]);
98+
99+
$response->assertNoContent();
100+
101+
$this->assertDatabaseLogsCount(1);
102+
$this->assertDatabaseHasRecord($method, $name, $path);
103+
104+
$log = HttpLog::where(compact('method', 'name'))->first();
105+
106+
$this->assertSame($method, $log->method);
107+
$this->assertSame($name, $log->name);
108+
$this->assertSame($path, $log->path);
109+
110+
$this->assertSame('http', $log->scheme);
111+
$this->assertSame('localhost', $log->host);
112+
$this->assertSame(80, $log->port);
113+
$this->assertSame('127.0.0.1', $log->ip);
114+
115+
$this->assertSame([
116+
'foo' => 'Foo',
117+
'token' => 123,
118+
'access_token' => 456,
119+
], $log->query);
120+
121+
$this->assertSame([
122+
'bar' => 'Bar',
123+
124+
'password' => 'q123456',
125+
'password_confirmation' => 'q123456',
126+
], $log->payload);
127+
128+
$this->assertSame([
129+
'host' => ['localhost'],
130+
'user-agent' => ['Symfony'],
131+
'accept' => ['text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
132+
'accept-language' => ['en-us,en;q=0.5'],
133+
'accept-charset' => ['ISO-8859-1,utf-8;q=0.7,*;q=0.7'],
134+
'authorization' => ['Bearer QwErTy'],
135+
'content-type' => ['application/x-www-form-urlencoded'],
136+
], $log->headers);
137+
}
73138
}

0 commit comments

Comments
 (0)