summaryrefslogtreecommitdiffstats
path: root/modules/util/sanitize.go
blob: b1c17b29cf59ebee35bd656c30e745a7a1430248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()
}