View source with formatted 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(csrf,
   32          [ csrf_check/1                % +Request
   33          ]).   34:- use_module(library(uri)).   35:- use_module(library(lists)).   36:- use_module(library(debug)).   37
   38/** <module> Protect against cross-site request forgery
   39
   40Handlers that change  state  on  behalf  of   a  logged  in  user  call
   41csrf_check/1 before doing anything. We do not use tokens, but verify the
   42request was initiated from  our  own  site.   Modern  browsers  send the
   43``Sec-Fetch-Site`` header, which cannot be set by scripts.  For browsers
   44that do not, we fall back to the ``Origin`` or ``Referer`` header.
   45
   46Requests without any of these headers are refused.  Note that this
   47implies that state-changing links cannot be used from bookmarks or
   48mail, as browsers send ``Sec-Fetch-Site: none`` for these.
   49*/
   50
   51%!  csrf_check(+Request) is det.
   52%
   53%   Raise an HTTP 403 (Forbidden) reply if Request was not initiated
   54%   from our own site.
   55
   56csrf_check(Request) :-
   57    same_site_request(Request),
   58    !.
   59csrf_check(Request) :-
   60    memberchk(path(Path), Request),
   61    debug(csrf, 'CSRF: refused ~q', [Request]),
   62    throw(http_reply(forbidden(Path))).
   63
   64same_site_request(Request) :-
   65    memberchk(sec_fetch_site(Site), Request),
   66    !,
   67    memberchk(Site, ['same-origin', 'same-site']).
   68same_site_request(Request) :-
   69    (   memberchk(origin(URL), Request)
   70    ->  true
   71    ;   memberchk(referer(URL), Request)
   72    ),
   73    url_host(URL, Host),
   74    request_host(Request, Host).
   75
   76url_host(URL, Host) :-
   77    atom(URL),
   78    uri_components(URL, Components),
   79    uri_data(authority, Components, Authority),
   80    atom(Authority),
   81    uri_authority_components(Authority, AuthComponents),
   82    uri_authority_data(host, AuthComponents, Host0),
   83    atom(Host0),
   84    downcase_atom(Host0, Host).
   85
   86%   request_host(+Request, ?Host)
   87%
   88%   Host is the host the client used to address us.  Behind a proxy
   89%   this may be in X-Forwarded-Host (a comma-separated list if the
   90%   request passed multiple proxies; the first is the client's).
   91
   92request_host(Request, Host) :-
   93    (   memberchk(x_forwarded_host(Hosts), Request)
   94    ->  atomic_list_concat([First|_], ',', Hosts),
   95        normalize_space(atom(Host0), First)
   96    ;   memberchk(host(Host0), Request)
   97    ),
   98    downcase_atom(Host0, Host)