Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webhook signature #3351

Merged
merged 8 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/config/collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,17 @@
'array' => true,
'filters' => [],
],
[
'$id' => 'signatureKey',
'type' => Database::VAR_STRING,
'format' => '',
'size' => 2048,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
],
'indexes' => [
[
Expand Down
11 changes: 9 additions & 2 deletions app/controllers/api/projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@

$security = (bool) filter_var($security, FILTER_VALIDATE_BOOLEAN);



$webhook = new Document([
'$id' => $dbForConsole->getId(),
'$read' => ['role:all'],
Expand All @@ -593,6 +595,7 @@
'security' => $security,
'httpUser' => $httpUser,
'httpPass' => $httpPass,
'signatureKey' => \bin2hex(\random_bytes(64)),
]);

$webhook = $dbForConsole->createDocument('webhooks', $webhook);
Expand Down Expand Up @@ -686,9 +689,10 @@
->param('security', false, new Boolean(true), 'Certificate verification, false for disabled or true for enabled.')
->param('httpUser', '', new Text(256), 'Webhook HTTP user. Max length: 256 chars.', true)
->param('httpPass', '', new Text(256), 'Webhook HTTP password. Max length: 256 chars.', true)
->param('signatureKey', null, new Text(256), 'Webhook signature key. Max length: 256 chars.', true)
->inject('response')
->inject('dbForConsole')
->action(function (string $projectId, string $webhookId, string $name, array $events, string $url, bool $security, string $httpUser, string $httpPass, Response $response, Database $dbForConsole) {
->action(function (string $projectId, string $webhookId, string $name, array $events, string $url, bool $security, string $httpUser, string $httpPass, string $signatureKey, Response $response, Database $dbForConsole) {

$project = $dbForConsole->getDocument('projects', $projectId);

Expand Down Expand Up @@ -716,8 +720,11 @@
->setAttribute('httpPass', $httpPass)
;

$dbForConsole->updateDocument('webhooks', $webhook->getId(), $webhook);
if(!empty($signatureKey)){
$webhook->setAttribute('signatureKey', $signatureKey);
}

$dbForConsole->updateDocument('webhooks', $webhook->getId(), $webhook);
$dbForConsole->deleteCachedDocument('projects', $project->getId());

$response->dynamic($webhook, Response::MODEL_WEBHOOK);
Expand Down
6 changes: 4 additions & 2 deletions app/workers/webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public function run(): void

protected function execute(array $events, string $payload, Document $webhook, Document $user, Document $project): void
{
$url = \rawurldecode($webhook->getAttribute('url'));
$signatureKey = $webhook->getAttribute('signatureKey');
$signature = base64_encode(hash_hmac('sha1', $url . $payload, $signatureKey, true));
$httpUser = $webhook->getAttribute('httpUser');
$httpPass = $webhook->getAttribute('httpPass');

$ch = \curl_init($webhook->getAttribute('url'));

\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
Expand All @@ -68,7 +70,7 @@ protected function execute(array $events, string $payload, Document $webhook, Do
'X-' . APP_NAME . '-Webhook-Name: ' . $webhook->getAttribute('name', ''),
'X-' . APP_NAME . '-Webhook-User-Id: ' . $user->getId(),
'X-' . APP_NAME . '-Webhook-Project-Id: ' . $project->getId(),
'X-' . APP_NAME . '-Webhook-Signature: ' . $webhook->getAttribute('signature', 'not-yet-implemented'),
'X-' . APP_NAME . '-Webhook-Signature: ' . $signature,
]
);

Expand Down
6 changes: 6 additions & 0 deletions src/Appwrite/Utopia/Response/Model/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public function __construct()
'default' => '',
'example' => 'password',
])
->addRule('signatureKey', [
'type' => self::TYPE_STRING,
'description' => 'Signature key which can be used to validated incoming',
'default' => '',
'example' => 'ad3d581ca230e2b7059c545e5a0d1defd8c349f8979b12579e03890aab973bcd23318e4b0ff6190fa3be09d746358821e19147d995210d45855eb3c069f6fd6e',
shimonewman marked this conversation as resolved.
Show resolved Hide resolved
])
;
}

Expand Down
1 change: 1 addition & 0 deletions tests/e2e/Scopes/ProjectCustom.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function getProject(): array
'name' => $project['body']['name'],
'apiKey' => $key['body']['secret'],
'webhookId' => $webhook['body']['$id'],
'signatureKey' => $webhook['body']['signatureKey'],
];

return self::$project;
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/Services/Projects/ProjectsConsoleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ public function testUpdateProjectWebhook($data): array
'security' => false,
'httpUser' => '',
'httpPass' => '',
'signatureKey' => 'My own uniq key',
]);

$this->assertEquals(200, $response['headers']['status-code']);
Expand All @@ -1037,6 +1038,7 @@ public function testUpdateProjectWebhook($data): array
$this->assertEquals(false, $response['body']['security']);
$this->assertEquals('', $response['body']['httpUser']);
$this->assertEquals('', $response['body']['httpPass']);
$this->assertEquals('My own uniq key', $response['body']['signatureKey']);

$response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/webhooks/' . $webhookId, array_merge([
'content-type' => 'application/json',
Expand Down