jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 1 | #ifndef _H_AllowedConnections |
| 2 | #define _H_AllowedConnections |
| 3 | /* |
| 4 | * Copyright 2008 Google Inc. |
jat@google.com | 5e86cbd | 2009-08-22 23:59:24 +0000 | [diff] [blame] | 5 | * |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 7 | * use this file except in compliance with the License. You may obtain a copy of |
| 8 | * the License at |
jat@google.com | 5e86cbd | 2009-08-22 23:59:24 +0000 | [diff] [blame] | 9 | * |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
jat@google.com | 5e86cbd | 2009-08-22 23:59:24 +0000 | [diff] [blame] | 11 | * |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | * License for the specific language governing permissions and limitations under |
| 16 | * the License. |
| 17 | */ |
| 18 | |
| 19 | #include <string> |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | #include <utility> |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
jat@google.com | 5e86cbd | 2009-08-22 23:59:24 +0000 | [diff] [blame] | 24 | * Manages rules to control access to other sites from the plugin. This is |
| 25 | * important since arbitrary web pages could try and use the plugin to connect |
| 26 | * to hosts the browser's machine has access to, such as doing port scanning |
| 27 | * behind a firewall. |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 28 | */ |
| 29 | class AllowedConnections { |
| 30 | public: |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Add a rule to match new requests against. |
| 33 | * |
| 34 | * @param pattern pattern to match |
| 35 | * @param exclude true if matches should be excluded instead of included |
| 36 | */ |
| 37 | static void addRule(const std::string& pattern, bool exclude = false); |
jat@google.com | 5e86cbd | 2009-08-22 23:59:24 +0000 | [diff] [blame] | 38 | |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Clear all rules. |
| 41 | */ |
| 42 | static void clearRules(); |
| 43 | |
| 44 | /** |
| 45 | * Get the host portion of the URL, not including the port. |
| 46 | * |
| 47 | * @return the host portion of the URL, or the unmodified URL if it does not |
| 48 | * appear to be valid |
| 49 | */ |
| 50 | static std::string getHostFromUrl(const std::string& url); |
| 51 | |
| 52 | /** |
| 53 | * Clear any existing rules and reinitialize from the supplied access list. |
| 54 | * |
| 55 | * This access list is of the form: |
| 56 | * [!]pattern,[!]pattern... |
| 57 | * where the optional exclamation indicates the following pattern is to be |
| 58 | * excluded, and an arbitrary number of patterns may be supplied with the |
| 59 | * first match being used. Each pattern currently is only an exact literal |
| 60 | * match against the host name, but will be extended to support simple |
| 61 | * wildcard patterns. |
| 62 | */ |
| 63 | static void initFromAccessList(const std::string& accessList); |
| 64 | |
| 65 | /** |
| 66 | * Returns true if the server for the requested URL matched any rule in |
| 67 | * our access list, and sets a flag based on whether that rule permits or |
| 68 | * denies the request. A host name of localhost or 127.0.0.1 is always |
| 69 | * allowed. |
| 70 | * |
| 71 | * @param url url of page initiating connection |
| 72 | * @param allowed pointer to return value indiciating that this URL should |
| 73 | * be allowed to initiate GWT development mode connections |
| 74 | * @return true if url matched a rule |
| 75 | */ |
| 76 | static bool matchesRule(const std::string& url, bool* allowed); |
| 77 | |
| 78 | private: |
| 79 | AllowedConnections() { |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 83 | * Internal class used for representing a rule. |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 84 | */ |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 85 | class Rule : std::pair<std::string, bool> { |
| 86 | public: |
| 87 | Rule(const std::string& pattern, bool exclude) |
| 88 | : std::pair<std::string, bool>(pattern, exclude) {} |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 89 | |
jat@google.com | 2318196 | 2009-09-03 22:22:56 +0000 | [diff] [blame] | 90 | const std::string& getPattern() const { |
| 91 | return first; |
| 92 | } |
| 93 | |
| 94 | bool isExcluded() const { |
| 95 | return second; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | static std::vector<Rule> rules; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | #endif |