Debian Codegraph @

src/java/org/astrogrid/samp/web/UrlTracker.java

raw
1 package org.astrogrid.samp.web;
2
3 import java.net.InetAddress;
4 import java.net.URL;
5 import java.net.UnknownHostException;
6 import java.util.ArrayList;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12
13 /**
14 * Keeps track of which URLs have been seen in communications inbound to
15 * and outbound from Web Profile clients.
16 * On the basis of these observations it is able to advise whether a
17 * Web Profile client ought to be permitted to dereference a given URL.
18 * The idea is that a Web Profile client, which may not be entirely
19 * trustworthy, has no legitimate reason for dereferencing an arbitrary
20 * URL, and should only be permitted to dereference local URLs if they
21 * have previously been sent as message arguments to it.
22 * (so for instance an attempt to read file:///etc/password is likely to
23 * be blocked).
24 * Since a SAMP client may be able to provoke some kind of echo, any
25 * URL which was mentioned by a Web Profile client before any other
26 * client mentions it is automatically marked as suspicious.
27 *
28 * <p>Details of the implementation are arguable.
29 *
30 * @author Mark Taylor
31 * @since 22 Jul 2011
32 */
33 public class UrlTracker {
34
35 private final Set permittedSet_;
36 private final Set blockedSet_;
37 private final String[] localhostNames_;
38 private final Logger logger_ =
39 Logger.getLogger( UrlTracker.class.getName() );
40
41 /**
42 * Constructor.
43 */
44 public UrlTracker() {
45 permittedSet_ = new HashSet();
46 blockedSet_ = new HashSet();
47
48 // Set up a list of aliases for the local host so we can identify
49 // and prepare to restrict access to localhost URLs.
50 List localNameList = new ArrayList();
51 localNameList.add( "localhost" );
52 localNameList.add( "127.0.0.1" );
53 try {
54 InetAddress localAddr = InetAddress.getLocalHost();
55 localNameList.add( localAddr.getHostName() );
56 localNameList.add( localAddr.getHostAddress() );
57 localNameList.add( localAddr.getCanonicalHostName() );
58 }
59 catch ( UnknownHostException e ) {
60 logger_.log( Level.WARNING, "Can't determine local host name", e );
61 }
62 localhostNames_ = (String[]) localNameList.toArray( new String[ 0 ] );
63 }
64
65 /**
66 * Note that a URL has been communicated to a Web Profile client
67 * from the outside world.
68 *
69 * @param url incoming URL
70 */
71 public synchronized void noteIncomingUrl( URL url ) {
72 if ( isSensitive( url ) ) {
73 if ( ! blockedSet_.contains( url ) ) {
74 if ( permittedSet_.add( url ) ) {
75 logger_.config( "Mark for translate permission URL "
76 + url );
77 }
78 }
79 }
80 }
81
82 /**
83 * Note that a Web Profile client has communicated a URL to the
84 * outside world.
85 *
86 * @param url outgoing URL
87 */
88 public synchronized void noteOutgoingUrl( URL url ) {
89 if ( isSensitive( url ) ) {
90 if ( ! permittedSet_.contains( url ) ) {
91 if ( blockedSet_.add( url ) ) {
92 logger_.config( "Mark for translate blocking URL " + url );
93 }
94 }
95 }
96 }
97
98 /**
99 * Indicates whether access to a given URL should be permitted,
100 * according to the strategy implemented by this class,
101 * from a Web Profile client.
102 *
103 * @param url URL to assess
104 * @return true iff permission to access is appropriate
105 */
106 public synchronized boolean isUrlPermitted( URL url ) {
107 if ( isSensitive( url ) ) {
108 if ( permittedSet_.contains( url ) ) {
109 logger_.config( "Translation permitted for marked URL " + url );
110 return true;
111 }
112 else {
113 logger_.warning( "Translation denied for unmarked URL " + url );
114 return false;
115 }
116 }
117 else {
118 logger_.config( "Translation permitted for non-sensitive URL "
119 + url );
120 return true;
121 }
122 }
123
124 /**
125 * Indicates whether a given URL is potentially sensitive.
126 * The current implementation always returns true.
127 * This is probably correct, since it's not in general possible
128 * to tell whether or not a given URL accords privileges to
129 * requests from the local host. But if this ends up letting
130 * too much through, identifying only file URLs and http/https
131 * ones on the local domain would probably be OK.
132 *
133 * @param url URL to assess
134 * @return true iff access should be restricted
135 */
136 protected boolean isSensitive( URL url ) {
137 return true;
138 }
139
140 /**
141 * Determines whether a hostname appears to reference the localhost.
142 *
143 * @param host hostname from URL
144 * @return true iff host appears to be, or may be, local
145 */
146 private boolean isLocalHost( String host ) {
147 if ( host == null || host.length() == 0 ) {
148 return true;
149 }
150 for ( int i = 0; i < localhostNames_.length; i++ ) {
151 if ( host.equalsIgnoreCase( localhostNames_[ i ] ) ) {
152 return true;
153 }
154 }
155 return false;
156 }
157 }