Skip to content

Commit f21cfd5

Browse files
committed
🔒 Sanitize raw [html] BBCode from incoming webhook payloads before processing, add header-based conditions with case-insensitive matching, and expose a matching UI field in the webhook form 🔧🛡️📝
1 parent 9447ba1 commit f21cfd5

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

‎lhc_web/design/defaulttheme/tpl/lhwebhooks/form_incoming.tpl.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@
255255
<input type="text" class="form-control form-control-sm" ng-model="webhookincomingsctl.conditions.add_field_2_value" value="" />
256256
</div>
257257
</div>
258+
<div class="col-6">
259+
<div class="form-group">
260+
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Header conditions. General conditions for messages being processed based on HTTP request headers.');?></label>
261+
<input type="text" class="form-control form-control-sm" placeholder="X-Webhook-Secret=mysecret||Content-Type=application/json" ng-model="webhookincomingsctl.conditions.header_cond" value="" />
262+
</div>
263+
</div>
258264
</div>
259265

260266
<hr>

‎lhc_web/lib/core/lhchat/lhchatwebhookincomming.php‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ public static function processEvent($incomingWebhook, array $payload) {
1414
}
1515
}
1616

17+
if (isset($conditions['header_cond']) && $conditions['header_cond'] != "") {
18+
// HTTP request headers are matched with the standard condition matcher
19+
if (!self::isValidCondition('header_cond', array('header_cond' => self::normalizeHeaderCondition($conditions['header_cond'])), self::getRequestHeadersPayload())) {
20+
throw new Exception('Header conditions does not met!');
21+
}
22+
}
23+
1724
$messages = isset($conditions['messages']) && $conditions['messages'] != '' ? $payload[$conditions['messages']] : [$payload];
1825

1926
if (isset($conditions['messages']) && $conditions['messages'] != '' && isset($conditions['message_direct']) && $conditions['message_direct'] == true) {
@@ -337,6 +344,82 @@ public static function isValidCondition($field, $conditions, $payloadMessage)
337344
return $validConditions;
338345
}
339346

347+
/**
348+
* Current request headers as a flat payload so webhook "Header conditions" can be
349+
* matched with the standard condition matcher (isValidCondition()).
350+
*
351+
* E.g. "X-Webhook-Secret=s3cr3t||Content-Type=application/json" checks two headers.
352+
* Keys are lower cased and normalizeHeaderCondition() lower cases attribute names,
353+
* so header names are matched case insensitively.
354+
*/
355+
public static function getRequestHeadersPayload()
356+
{
357+
$headers = array();
358+
359+
foreach ($_SERVER as $key => $value) {
360+
if (strpos($key, 'HTTP_') === 0) {
361+
$headers[strtolower(str_replace('_', '-', substr($key, 5)))] = $value;
362+
} elseif (in_array($key, array('CONTENT_TYPE', 'CONTENT_LENGTH'))) {
363+
$headers[strtolower(str_replace('_', '-', $key))] = $value;
364+
}
365+
}
366+
367+
if (function_exists('getallheaders')) {
368+
$requestHeaders = getallheaders();
369+
if (is_array($requestHeaders)) {
370+
foreach ($requestHeaders as $name => $value) {
371+
$headers[strtolower($name)] = $value;
372+
}
373+
}
374+
}
375+
376+
return $headers;
377+
}
378+
379+
/**
380+
* Lower cases header names in "Header conditions" so they match lower cased keys
381+
* returned by getRequestHeadersPayload(). Values are untouched.
382+
*/
383+
public static function normalizeHeaderCondition($condition)
384+
{
385+
$conditionsPairs = array();
386+
387+
foreach (explode('||', $condition) as $conditionsPair) {
388+
$equalsPosition = strpos($conditionsPair, '=');
389+
390+
if ($equalsPosition === false) {
391+
$conditionsPairs[] = strtolower(trim($conditionsPair));
392+
} else {
393+
$conditionsPairs[] = strtolower(trim(substr($conditionsPair, 0, $equalsPosition))) . substr($conditionsPair, $equalsPosition);
394+
}
395+
}
396+
397+
return implode('||', $conditionsPairs);
398+
}
399+
400+
/**
401+
* Removes [html]...[/html] BBCode wrapper from every value of the incoming payload.
402+
*
403+
* That BBCode is rendered as raw HTML in the back office, so an external system must
404+
* never be able to deliver HTML/JavaScript into an operator session.
405+
*/
406+
public static function stripHtmlBBCode($payload)
407+
{
408+
if (is_array($payload)) {
409+
foreach ($payload as $key => $value) {
410+
$payload[$key] = self::stripHtmlBBCode($value);
411+
}
412+
413+
return $payload;
414+
}
415+
416+
if (is_string($payload)) {
417+
return str_ireplace(array('[html]', '[/html]'), '', $payload);
418+
}
419+
420+
return $payload;
421+
}
422+
340423
public static function processMessage($incomingWebhook, $payloadMessage, $payloadAll, $returnDeterminedData = false) {
341424

342425
$conditions = $incomingWebhook->conditions_array;

‎lhc_web/modules/lhwebhooks/incoming.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
$data = $dummyPayload;
2727
}
2828

29+
// [html] BBCode is rendered as raw HTML in the back office. Incoming payload
30+
// is untrusted input, so the tags are always removed before anything is processed.
31+
$data = erLhcoreClassChatWebhookIncoming::stripHtmlBBCode($data);
32+
2933
if ($incomingWebhook->log_incoming == 1) {
3034
erLhcoreClassLog::write(json_encode($data,JSON_PRETTY_PRINT),
3135
ezcLog::SUCCESS_AUDIT,

0 commit comments

Comments
 (0)