29
30
31:- module(public_url,
32 [ public_url/1, 33 must_be_public_url/1 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)).
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
83
84host_name(Host0, Host) :-
85 atom_concat('[', Rest, Host0),
86 atom_concat(Host, ']', Rest),
87 !.
88host_name(Host, Host).
94must_be_public_url(URL) :-
95 public_url(URL),
96 !.
97must_be_public_url(URL) :-
98 domain_error(public_url, URL).
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,_,_,_). 113private_ip4(10,_,_,_). 114private_ip4(100,B,_,_) :- B >= 64, B =< 127. 115private_ip4(127,_,_,_). 116private_ip4(169,254,_,_). 117private_ip4(172,B,_,_) :- B >= 16, B =< 31. 118private_ip4(192,0,0,_). 119private_ip4(192,168,_,_). 120private_ip4(198,B,_,_) :- B >= 18, B =< 19. 121private_ip4(A,_,_,_) :- A >= 224. 122
123private_ip6(0,0,0,0,0,0,0,_). 124private_ip6(0,0,0,0,0,0xffff,G,H) :- 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,_,_,_,_,_,_). 129private_ip6(A,_,_,_,_,_,_,_) :- A /\ 0xfe00 =:= 0xfc00. 130private_ip6(A,_,_,_,_,_,_,_) :- A /\ 0xffc0 =:= 0xfe80. 131private_ip6(A,_,_,_,_,_,_,_) :- A /\ 0xff00 =:= 0xff00.
Check URLs fetched on behalf of users
The server fetches URLs provided by users and pack authors: pack archives and git repositories and OpenID discovery documents. This module verifies such URLs are
http(s)URLs for hosts that only resolve to public addresses, such that they cannot be used to access services on the server itself or the internal network (Server Side Request Forgery).Note that this does not protect against redirects to internal addresses or DNS rebinding.