View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        jan@swi-prolog.org
    5    WWW:           https://www.swi-prolog.org
    6    Copyright (C): 2026, SWI-Prolog Solutions b.v.
    7
    8    This program is free software; you can redistribute it and/or
    9    modify it under the terms of the GNU General Public License
   10    as published by the Free Software Foundation; either version 2
   11    of the License, or (at your option) any later version.
   12
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17
   18    You should have received a copy of the GNU General Public
   19    License along with this library; if not, write to the Free Software
   20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21
   22    As a special exception, if you link this library with other files,
   23    compiled with a Free Software compiler, to produce an executable, this
   24    library does not by itself cause the resulting executable to be covered
   25    by the GNU General Public License. This exception does not however
   26    invalidate any other reasons why the executable file might be covered by
   27    the GNU General Public License.
   28*/
   29
   30
   31:- module(webhook,
   32          [ webhook_authorized/1        % +Request
   33          ]).   34:- use_module(library(settings)).   35:- use_module(library(sha)).   36:- use_module(library(lists)).   37:- use_module(library(apply)).   38:- use_module(library(debug)).   39:- use_module(openid).   40:- use_module(csrf).

Authorize GitHub webhook requests

Handlers such as /blog/pull and /examples/pull run `git pull`, rebuild indexes and purge the CDN. They may only be triggered by

Anything else is refused with 403 (Forbidden).

   60:- setting(secret, atom, '',
   61           'Shared secret for GitHub webhooks (/blog/pull, /examples/pull)').   62
   63max_body_size(1_000_000).
 webhook_authorized(+Request) is det
Succeed if Request may trigger a webhook action. Otherwise throw an HTTP 403 reply. For signed requests the body is read.
   70webhook_authorized(Request) :-
   71    memberchk(x_hub_signature_256(Signature), Request),
   72    !,
   73    (   setting(secret, Secret),
   74        Secret \== '',
   75        read_body(Request, Body),
   76        hmac_sha(Secret, Body, HMAC, [ algorithm(sha256),
   77                                       encoding(octet)
   78                                     ]),
   79        hash_atom(HMAC, Hex),
   80        atom_concat('sha256=', Hex, Expected),
   81        same_string(Expected, Signature)
   82    ->  debug(webhook, 'Valid webhook signature', [])
   83    ;   refuse(Request)
   84    ).
   85webhook_authorized(Request) :-
   86    site_user_logged_in(User),
   87    site_user_property(User, granted(admin)),
   88    !,
   89    csrf_check(Request).
   90webhook_authorized(Request) :-
   91    refuse(Request).
   92
   93refuse(Request) :-
   94    memberchk(path(Path), Request),
   95    throw(http_reply(forbidden(Path))).
 read_body(+Request, -Body:string) is semidet
Read the raw request body as a string of bytes (octets).
  101read_body(Request, Body) :-
  102    memberchk(content_length(Len), Request),
  103    max_body_size(Max),
  104    Len =< Max,
  105    memberchk(input(In), Request),
  106    (   stream_property(In, encoding(octet))
  107    ->  true
  108    ;   set_stream(In, encoding(octet))
  109    ),
  110    read_string(In, Len, Body),
  111    string_length(Body, Len).
 same_string(+A, +B) is semidet
Compare in time that only depends on the length, such that the signature cannot be guessed from response times.
  118same_string(A, B) :-
  119    atom_codes(A, CA),
  120    atom_codes(B, CB),
  121    length(CA, Len),
  122    length(CB, Len),
  123    foldl(xor_or, CA, CB, 0, Diff),
  124    Diff == 0.
  125
  126xor_or(A, B, D0, D) :-
  127    D is D0 \/ (A xor B)