Skip to content

Commit

Permalink
Redact API key in RequestOpts debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Oct 18, 2019
1 parent 96c5974 commit a46b670
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/Util/RequestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ public function __construct($key = null, $headers = [], $base = null)
$this->apiBase = $base;
}

public function __debugInfo()
{
$redactedApiKey = strlen($this->apiKey) >= 12
? (substr($this->apiKey, 0, 8)
. str_repeat('*', strlen($this->apiKey) - 12)
. substr($this->apiKey, -4))
: $this->apiKey;

return [
'apiKey' => $redactedApiKey,
'headers' => $this->headers,
'apiBase' => $this->apiBase,
];
}

/**
* Unpacks an options array and merges it into the existing RequestOptions
* object.
Expand Down
7 changes: 7 additions & 0 deletions tests/Stripe/Util/RequestOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,11 @@ public function testDiscardNonPersistentHeaders()
$opts->discardNonPersistentHeaders();
$this->assertSame(['Stripe-Account' => 'foo'], $opts->headers);
}

public function testDebugInfo()
{
$opts = Util\RequestOptions::parse(['api_key' => 'sk_test_1234567890abcdefghijklmn']);
$debugInfo = print_r($opts, true);
$this->assertContains("sk_test_********************klmn", $debugInfo);
}
}

0 comments on commit a46b670

Please sign in to comment.