|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +part of dart.io; |
| 6 | + |
| 7 | +/// Whether insecure connections to [host] are allowed. |
| 8 | +/// |
| 9 | +/// [host] must be a [String] or [InternetAddress]. |
| 10 | +/// |
| 11 | +/// If any of the domain policies match [host], the matching policy will make |
| 12 | +/// the decision. If multiple policies apply, the top matching policy makes the |
| 13 | +/// decision. If none of the domain policies match, the embedder default is |
| 14 | +/// used. |
| 15 | +/// |
| 16 | +/// Loopback addresses are always allowed. |
| 17 | +bool isInsecureConnectionAllowed(dynamic host) { |
| 18 | + String hostString; |
| 19 | + if (host is String) { |
| 20 | + try { |
| 21 | + if ("localhost" == host || InternetAddress(host).isLoopback) return true; |
| 22 | + } on ArgumentError { |
| 23 | + // Assume not loopback. |
| 24 | + } |
| 25 | + hostString = host; |
| 26 | + } else if (host is InternetAddress) { |
| 27 | + if (host.isLoopback) return true; |
| 28 | + hostString = host.host; |
| 29 | + } else { |
| 30 | + throw ArgumentError.value( |
| 31 | + host, "host", "Must be a String or InternetAddress"); |
| 32 | + } |
| 33 | + final topMatchedPolicy = _findBestDomainNetworkPolicy(hostString); |
| 34 | + final envOverride = bool.fromEnvironment( |
| 35 | + "dart.library.io.may_insecurely_connect_to_all_domains", |
| 36 | + defaultValue: true); |
| 37 | + return topMatchedPolicy?.allowInsecureConnections ?? |
| 38 | + (envOverride && _EmbedderConfig._mayInsecurelyConnectToAllDomains); |
| 39 | +} |
| 40 | + |
| 41 | +/// Policy for a specific domain. |
| 42 | +/// |
| 43 | +/// [_DomainNetworkPolicy] can be used to create exceptions to the global |
| 44 | +/// network policy. |
| 45 | +class _DomainNetworkPolicy { |
| 46 | + /// https://tools.ietf.org/html/rfc1034#:~:text=Name%20space%20specifications |
| 47 | + /// |
| 48 | + /// We specifically do not allow IP addresses. |
| 49 | + static final _domainMatcher = RegExp( |
| 50 | + r"^(?:[a-z\d-]{1,63}\.)+[a-z][a-z\d-]{0,62}$", |
| 51 | + caseSensitive: false); |
| 52 | + |
| 53 | + /// The domain on which the policy is being set. |
| 54 | + /// |
| 55 | + /// This cannot be a numeric IP address. |
| 56 | + /// |
| 57 | + /// For example: `example.com`. |
| 58 | + final String domain; |
| 59 | + |
| 60 | + /// Whether to allow insecure socket connections for this domain. |
| 61 | + final bool allowInsecureConnections; |
| 62 | + |
| 63 | + /// Whether this domain policy covers sub-domains as well. |
| 64 | + /// |
| 65 | + /// If this is true, all subdomains inherit the same policy. For instance, |
| 66 | + /// a policy set on `example.com` would apply to `*.example.com` such as |
| 67 | + /// `subdomain.example.com` or `www.example.com`. |
| 68 | + final bool includesSubDomains; |
| 69 | + |
| 70 | + /// Creates a new domain exception in the network policy. |
| 71 | + /// |
| 72 | + /// [domain] is the domain on which the policy is being set. |
| 73 | + /// |
| 74 | + /// [includesSubDomains] determines whether the policy applies to |
| 75 | + /// all sub domains. If this is set to true, all subdomains inherit the |
| 76 | + /// same policy. For instance, a policy set on `example.com` would apply to |
| 77 | + /// `*.example.com` such as `subdomain.example.com` or `www.example.com`. |
| 78 | + /// |
| 79 | + /// [allowInsecureConnections] determines whether to allow insecure socket |
| 80 | + /// connections for this [domain]. |
| 81 | + _DomainNetworkPolicy(this.domain, |
| 82 | + {this.includesSubDomains = false, |
| 83 | + this.allowInsecureConnections = false}) { |
| 84 | + if (domain.length > 255 || !_domainMatcher.hasMatch(domain)) { |
| 85 | + throw ArgumentError.value(domain, "domain", "Invalid domain name"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Calculates how well the policy matches to a given host string. |
| 90 | + /// |
| 91 | + /// A host matches a [policy] if it ends with its [domain]. |
| 92 | + /// |
| 93 | + /// A score is given to such a match depending on the specificity of the |
| 94 | + /// [domain]: |
| 95 | + /// |
| 96 | + /// * A longer domain receives a higher score. |
| 97 | + /// * A domain that does not allow sub domains receives a higher score. |
| 98 | + /// |
| 99 | + /// Returns -1 if the policy does not match. |
| 100 | + int matchScore(String host) { |
| 101 | + final domainLength = domain.length; |
| 102 | + final hostLength = host.length; |
| 103 | + final lengthDelta = hostLength - domainLength; |
| 104 | + if (host.endsWith(domain) && |
| 105 | + (lengthDelta == 0 || |
| 106 | + includesSubDomains && host.codeUnitAt(lengthDelta - 1) == 0x2e)) { |
| 107 | + return domainLength * 2 + (includesSubDomains ? 0 : 1); |
| 108 | + } |
| 109 | + return -1; |
| 110 | + } |
| 111 | + |
| 112 | + /// Checks whether the [policy] to be added conflicts with existing policies. |
| 113 | + /// |
| 114 | + /// Returns [true] if policy is safe to add to existing policy set and [false] |
| 115 | + /// if policy can safely be ignored. |
| 116 | + /// |
| 117 | + /// Throws [ArgumentError] if a conflict is detected. |
| 118 | + bool checkConflict(List<_DomainNetworkPolicy> existingPolicies) { |
| 119 | + for (final existingPolicy in existingPolicies) { |
| 120 | + if (includesSubDomains == existingPolicy.includesSubDomains && |
| 121 | + domain == existingPolicy.domain) { |
| 122 | + if (allowInsecureConnections == |
| 123 | + existingPolicy.allowInsecureConnections) { |
| 124 | + // This is a duplicate policy |
| 125 | + return false; |
| 126 | + } |
| 127 | + throw StateError("Contradiction in the domain security policies: " |
| 128 | + "'$this' contradicts '$existingPolicy'"); |
| 129 | + } |
| 130 | + } |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + /// This is used for encoding information about the policy in user visible |
| 135 | + /// errors. |
| 136 | + @override |
| 137 | + String toString() { |
| 138 | + final subDomainPrefix = includesSubDomains ? '*.' : ''; |
| 139 | + final insecureConnectionPermission = |
| 140 | + allowInsecureConnections ? 'Allows' : 'Disallows'; |
| 141 | + return "$subDomainPrefix$domain: " |
| 142 | + "$insecureConnectionPermission insecure connections"; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/// Finds the top [DomainNetworkPolicy] instance that match given a single |
| 147 | +/// [domain]. |
| 148 | +/// |
| 149 | +/// We order the policies according to how specific they are. The final policy |
| 150 | +/// for a given [domain] is determined by the top matching |
| 151 | +/// [DomainNetworkPolicy]. |
| 152 | +/// |
| 153 | +/// Returns null if there's no matching policy. |
| 154 | +_DomainNetworkPolicy? _findBestDomainNetworkPolicy(String domain) { |
| 155 | + var topScore = 0; |
| 156 | + _DomainNetworkPolicy? topPolicy; |
| 157 | + for (final _DomainNetworkPolicy policy in _domainPolicies) { |
| 158 | + final score = policy.matchScore(domain); |
| 159 | + if (score > topScore) { |
| 160 | + topScore = score; |
| 161 | + topPolicy = policy; |
| 162 | + } |
| 163 | + } |
| 164 | + return topPolicy; |
| 165 | +} |
| 166 | + |
| 167 | +/// Domain level policies that dart:io is enforcing. |
| 168 | +late List<_DomainNetworkPolicy> _domainPolicies = |
| 169 | + _constructDomainPolicies(null); |
| 170 | + |
| 171 | +List<_DomainNetworkPolicy> _constructDomainPolicies( |
| 172 | + String? domainPoliciesString) { |
| 173 | + final domainPolicies = <_DomainNetworkPolicy>[]; |
| 174 | + domainPoliciesString ??= String.fromEnvironment( |
| 175 | + "dart.library.io.domain_network_policies", |
| 176 | + defaultValue: ""); |
| 177 | + if (domainPoliciesString.isNotEmpty) { |
| 178 | + final List<dynamic> policiesJson = json.decode(domainPoliciesString); |
| 179 | + for (final List<dynamic> policyJson in policiesJson) { |
| 180 | + assert(policyJson.length == 3); |
| 181 | + final policy = _DomainNetworkPolicy( |
| 182 | + policyJson[0], |
| 183 | + includesSubDomains: policyJson[1], |
| 184 | + allowInsecureConnections: policyJson[2], |
| 185 | + ); |
| 186 | + if (policy.checkConflict(domainPolicies)) { |
| 187 | + domainPolicies.add(policy); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + return domainPolicies; |
| 192 | +} |
0 commit comments