-
-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize sensitive data from URLs (span desc, span data, crumbs, clie…
…nt errors) (#1327) Co-authored-by: Manoel Aranda Neto <[email protected]> Co-authored-by: Manoel Aranda Neto <[email protected]>
- Loading branch information
1 parent
df16b96
commit 24f71aa
Showing
25 changed files
with
657 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// ignore: invalid_export_of_internal_element | ||
export 'sentry.dart'; | ||
export 'src/sentry_attachment/io_sentry_attachment.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../protocol.dart'; | ||
import 'url_details.dart'; | ||
|
||
@internal | ||
class HttpSanitizer { | ||
static final RegExp _authRegExp = RegExp("(.+://)(.*@)(.*)"); | ||
static final List<String> _securityHeaders = [ | ||
"X-FORWARDED-FOR", | ||
"AUTHORIZATION", | ||
"COOKIE", | ||
"SET-COOKIE", | ||
"X-API-KEY", | ||
"X-REAL-IP", | ||
"REMOTE-ADDR", | ||
"FORWARDED", | ||
"PROXY-AUTHORIZATION", | ||
"X-CSRF-TOKEN", | ||
"X-CSRFTOKEN", | ||
"X-XSRF-TOKEN" | ||
]; | ||
|
||
/// Parse and sanitize url data for sentry.io | ||
static UrlDetails? sanitizeUrl(String? url) { | ||
if (url == null) { | ||
return null; | ||
} | ||
|
||
final queryIndex = url.indexOf('?'); | ||
final fragmentIndex = url.indexOf('#'); | ||
|
||
if (queryIndex > -1 && fragmentIndex > -1 && fragmentIndex < queryIndex) { | ||
// url considered malformed because of fragment position | ||
return UrlDetails(); | ||
} else { | ||
try { | ||
final uri = Uri.parse(url); | ||
final urlWithAuthRemoved = _urlWithAuthRemoved(uri._url()); | ||
return UrlDetails( | ||
url: urlWithAuthRemoved.isEmpty ? null : urlWithAuthRemoved, | ||
query: uri.query.isEmpty ? null : uri.query, | ||
fragment: uri.fragment.isEmpty ? null : uri.fragment); | ||
} catch (_) { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
static Map<String, String>? sanitizedHeaders(Map<String, String>? headers) { | ||
if (headers == null) { | ||
return null; | ||
} | ||
final sanitizedHeaders = <String, String>{}; | ||
headers.forEach((key, value) { | ||
if (!_securityHeaders.contains(key.toUpperCase())) { | ||
sanitizedHeaders[key] = value; | ||
} | ||
}); | ||
return sanitizedHeaders; | ||
} | ||
|
||
static String _urlWithAuthRemoved(String url) { | ||
final userInfoMatch = _authRegExp.firstMatch(url); | ||
if (userInfoMatch != null && userInfoMatch.groupCount == 3) { | ||
final userInfoString = userInfoMatch.group(2) ?? ''; | ||
final replacementString = userInfoString.contains(":") | ||
? "[Filtered]:[Filtered]@" | ||
: "[Filtered]@"; | ||
return '${userInfoMatch.group(1) ?? ''}$replacementString${userInfoMatch.group(3) ?? ''}'; | ||
} else { | ||
return url; | ||
} | ||
} | ||
} | ||
|
||
extension UriPath on Uri { | ||
String _url() { | ||
var buffer = ''; | ||
if (scheme.isNotEmpty) { | ||
buffer += '$scheme://'; | ||
} | ||
if (userInfo.isNotEmpty) { | ||
buffer += '$userInfo@'; | ||
} | ||
buffer += host; | ||
if (path.isNotEmpty) { | ||
buffer += path; | ||
} | ||
return buffer; | ||
} | ||
} | ||
|
||
extension SanitizedSentryRequest on SentryRequest { | ||
SentryRequest sanitized() { | ||
final urlDetails = HttpSanitizer.sanitizeUrl(url) ?? UrlDetails(); | ||
return copyWith( | ||
url: urlDetails.urlOrFallback, | ||
queryString: urlDetails.query, | ||
fragment: urlDetails.fragment, | ||
headers: HttpSanitizer.sanitizedHeaders(headers), | ||
removeCookies: true, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:meta/meta.dart'; | ||
import '../../sentry.dart'; | ||
|
||
/// Sanitized url data for sentry.io | ||
@internal | ||
class UrlDetails { | ||
UrlDetails({this.url, this.query, this.fragment}); | ||
|
||
final String? url; | ||
final String? query; | ||
final String? fragment; | ||
|
||
late final urlOrFallback = url ?? 'unknown'; | ||
|
||
void applyToSpan(ISentrySpan? span) { | ||
if (span == null) { | ||
return; | ||
} | ||
if (url != null) { | ||
span.setData('url', url); | ||
} | ||
if (query != null) { | ||
span.setData("http.query", query); | ||
} | ||
if (fragment != null) { | ||
span.setData("http.fragment", fragment); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.