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)). 39 40/** <module> Protect against cross-site request forgery 41 42Handlers that change state on behalf of a logged in user call 43csrf_check/1 before doing anything. We do not use tokens, but verify the 44request was initiated from our own site. Modern browsers send the 45``Sec-Fetch-Site`` header, which cannot be set by scripts. For browsers 46that do not, we fall back to the ``Origin`` or ``Referer`` header. 47 48Requests without any of these headers are refused. Note that this 49implies that state-changing links cannot be used from bookmarks or 50mail, as browsers send ``Sec-Fetch-Site: none`` for these. 51*/ 52 53%! csrf_check(+Request) is det. 54% 55% Raise an HTTP 403 (Forbidden) reply if Request was not initiated 56% from our own site. 57 58csrf_check(Request) :- 59 csrf_check(Request, []). 60 61%! csrf_check(+Request, +Options) is det. 62% 63% As csrf_check/1. Options: 64% 65% - allow_direct(+Boolean) 66% If `true` (default `false`), also accept requests that were 67% not initiated from a web page: requests without any of the 68% headers above (e.g., from curl) and requests for which the 69% browser sends ``Sec-Fetch-Site: none`` (typed URL or 70% bookmark). Requests initiated from another site are still 71% refused. This is intended for handlers that are protected 72% by HTTP authentication, which browsers send along with 73% cross-site requests. 74 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)