-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error message sanitiziation (#3082)
- Loading branch information
1 parent
5dc37b1
commit 3c1b1ca
Showing
4 changed files
with
64 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package util | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// urlSafeError wraps an error whose message may contain a sensitive URL | ||
type urlSafeError struct { | ||
err error | ||
unsanitizedURL string | ||
} | ||
|
||
func (err urlSafeError) Error() string { | ||
return SanitizeMessage(err.err.Error(), err.unsanitizedURL) | ||
} | ||
|
||
// URLSanitizedError returns the sanitized version an error whose message may | ||
// contain a sensitive URL | ||
func URLSanitizedError(err error, unsanitizedURL string) error { | ||
return urlSafeError{err: err, unsanitizedURL: unsanitizedURL} | ||
} | ||
|
||
// SanitizeMessage sanitizes a message which may contains a sensitive URL | ||
func SanitizeMessage(message, unsanitizedURL string) string { | ||
sanitizedURL := SanitizeURLCredentials(unsanitizedURL, true) | ||
return strings.Replace(message, unsanitizedURL, sanitizedURL, -1) | ||
} | ||
|
||
// SanitizeURLCredentials sanitizes a url, either removing user credentials | ||
// or replacing them with a placeholder. | ||
func SanitizeURLCredentials(unsanitizedURL string, usePlaceholder bool) string { | ||
u, err := url.Parse(unsanitizedURL) | ||
if err != nil { | ||
// don't log the error, since it might contain unsanitized URL. | ||
return "(unparsable url)" | ||
} | ||
if u.User != nil && usePlaceholder { | ||
u.User = url.User("<credentials>") | ||
} else { | ||
u.User = nil | ||
} | ||
return u.String() | ||
} |
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