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(public_url,
   32          [ public_url/1,               % +URL
   33            must_be_public_url/1        % +URL
   34          ]).   35:- use_module(library(uri)).   36:- use_module(library(socket)).   37:- use_module(library(error)).   38:- use_module(library(lists)).   39:- use_module(library(apply)).   40
   41/** <module> Check URLs fetched on behalf of users
   42
   43The server fetches URLs provided by users and pack authors: pack
   44archives and git repositories and OpenID discovery documents.  This
   45module verifies such URLs are http(s) URLs for hosts that only resolve
   46to public addresses, such that they cannot be used to access services
   47on the server itself or the internal network (Server Side Request
   48Forgery).
   49
   50Note that this does not protect against redirects to internal
   51addresses or DNS rebinding.
   52*/
   53
   54%!  public_url(+URL) is semidet.
   55%
   56%   True if URL is an http or https URL whose host resolves only to
   57%   public addresses.
   58
   59public_url(URL) :-
   60    atom(URL),
   61    uri_components(URL, Components),
   62    uri_data(scheme, Components, Scheme),
   63    memberchk(Scheme, [http, https]),
   64    uri_data(authority, Components, Authority),
   65    atom(Authority),
   66    uri_authority_components(Authority, AC),
   67    uri_authority_data(host, AC, Host0),
   68    atom(Host0),
   69    Host0 \== '',
   70    host_name(Host0, Host),
   71    catch(findall(Addr,
   72                  ( host_address(Host, SockAddr, [type(stream)]),
   73                    get_dict(address, SockAddr, Addr)
   74                  ),
   75                  Addrs),
   76          _, fail),
   77    Addrs \== [],
   78    maplist(public_address, Addrs).
   79
   80%   host_name(+URIHost, -Host)
   81%
   82%   Remove the brackets from an IPv6 address literal.
   83
   84host_name(Host0, Host) :-
   85    atom_concat('[', Rest, Host0),
   86    atom_concat(Host, ']', Rest),
   87    !.
   88host_name(Host, Host).
   89
   90%!  must_be_public_url(+URL) is det.
   91%
   92%   @error domain_error(public_url, URL)
   93
   94must_be_public_url(URL) :-
   95    public_url(URL),
   96    !.
   97must_be_public_url(URL) :-
   98    domain_error(public_url, URL).
   99
  100%!  public_address(+Address) is semidet.
  101%
  102%   True if Address is not a loopback, private, link-local, multicast
  103%   or otherwise reserved address.
  104
  105public_address(ip(A,B,C,D)) :-
  106    !,
  107    \+ private_ip4(A,B,C,D).
  108public_address(ip(A,B,C,D,E,F,G,H)) :-
  109    !,
  110    \+ private_ip6(A,B,C,D,E,F,G,H).
  111
  112private_ip4(0,_,_,_).                           % "this" network
  113private_ip4(10,_,_,_).                          % private
  114private_ip4(100,B,_,_) :- B >= 64, B =< 127.    % carrier-grade NAT
  115private_ip4(127,_,_,_).                         % loopback
  116private_ip4(169,254,_,_).                       % link-local (cloud metadata)
  117private_ip4(172,B,_,_) :- B >= 16, B =< 31.     % private
  118private_ip4(192,0,0,_).                         % IETF protocol assignments
  119private_ip4(192,168,_,_).                       % private
  120private_ip4(198,B,_,_) :- B >= 18, B =< 19.     % benchmarking
  121private_ip4(A,_,_,_) :- A >= 224.               % multicast and reserved
  122
  123private_ip6(0,0,0,0,0,0,0,_).                   % :: and ::1
  124private_ip6(0,0,0,0,0,0xffff,G,H) :-            % IPv4 mapped
  125    A is G >> 8, B is G /\ 0xff,
  126    C is H >> 8, D is H /\ 0xff,
  127    private_ip4(A,B,C,D).
  128private_ip6(0x64,0xff9b,_,_,_,_,_,_).           % NAT64
  129private_ip6(A,_,_,_,_,_,_,_) :- A /\ 0xfe00 =:= 0xfc00.  % unique local
  130private_ip6(A,_,_,_,_,_,_,_) :- A /\ 0xffc0 =:= 0xfe80.  % link-local
  131private_ip6(A,_,_,_,_,_,_,_) :- A /\ 0xff00 =:= 0xff00.  % multicast