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(csrf,
   32          [ csrf_check/1,               % +Request
   33            csrf_check/2                % +Request, +Options
   34          ]).   35:- use_module(library(uri)).   36:- use_module(library(lists)).   37:- use_module(library(debug)).   38:- use_module(library(option)).

Protect against cross-site request forgery

Handlers that change state on behalf of a logged in user call csrf_check/1 before doing anything. We do not use tokens, but verify the request was initiated from our own site. Modern browsers send the Sec-Fetch-Site header, which cannot be set by scripts. For browsers that do not, we fall back to the Origin or Referer header.

Requests without any of these headers are refused. Note that this implies that state-changing links cannot be used from bookmarks or mail, as browsers send Sec-Fetch-Site: none for these.

 csrf_check(+Request) is det
Raise an HTTP 403 (Forbidden) reply if Request was not initiated from our own site.
   58csrf_check(Request) :-
   59    csrf_check(Request, []).
 csrf_check(+Request, +Options) is det
As csrf_check/1. Options:
allow_direct(+Boolean)
If true (default false), also accept requests that were not initiated from a web page: requests without any of the headers above (e.g., from curl) and requests for which the browser sends Sec-Fetch-Site: none (typed URL or bookmark). Requests initiated from another site are still refused. This is intended for handlers that are protected by HTTP authentication, which browsers send along with cross-site requests.
   75csrf_check(Request, _) :-
   76    same_site_request(Request),
   77    !.
   78csrf_check(Request, Options) :-
   79    option(allow_direct(true), Options),
   80    direct_request(Request),
   81    !.
   82csrf_check(Request, _) :-
   83    memberchk(path(Path), Request),
   84    debug(csrf, 'CSRF: refused ~q', [Request]),
   85    throw(http_reply(forbidden(Path))).
   86
   87same_site_request(Request) :-
   88    memberchk(sec_fetch_site(Site), Request),
   89    !,
   90    memberchk(Site, ['same-origin', 'same-site']).
   91same_site_request(Request) :-
   92    (   memberchk(origin(URL), Request)
   93    ->  true
   94    ;   memberchk(referer(URL), Request)
   95    ),
   96    url_host(URL, Host),
   97    request_host(Request, Host).
   98
   99direct_request(Request) :-
  100    memberchk(sec_fetch_site(Site), Request),
  101    !,
  102    Site == none.
  103direct_request(Request) :-
  104    \+ memberchk(origin(_), Request),
  105    \+ memberchk(referer(_), Request).
  106
  107url_host(URL, Host) :-
  108    atom(URL),
  109    uri_components(URL, Components),
  110    uri_data(authority, Components, Authority),
  111    atom(Authority),
  112    uri_authority_components(Authority, AuthComponents),
  113    uri_authority_data(host, AuthComponents, Host0),
  114    atom(Host0),
  115    downcase_atom(Host0, Host).
  116
  117%   request_host(+Request, ?Host)
  118%
  119%   Host is the host the client used to address us.  Behind a proxy
  120%   this may be in X-Forwarded-Host (a comma-separated list if the
  121%   request passed multiple proxies; the first is the client's).
  122
  123request_host(Request, Host) :-
  124    (   memberchk(x_forwarded_host(Hosts), Request)
  125    ->  atomic_list_concat([First|_], ',', Hosts),
  126        normalize_space(atom(Host0), First)
  127    ;   memberchk(host(Host0), Request)
  128    ),
  129    downcase_atom(Host0, Host)