-
Notifications
You must be signed in to change notification settings - Fork 5k
security: fix SSRF in repository migration #6812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
689ed33
Patch for CVE-2022-0870
michaellrowley 43e4246
Added SSRF test cases
michaellrowley 9c800c8
Corrected variable naming
michaellrowley 40bbd3f
Removed comment in Test_isLocalHostname case
michaellrowley c785478
Added comma to last test item
michaellrowley 7f64edb
gofmt
unknwon a6a0417
Parse CIDRs at init time
unknwon 7f7939b
Move to netutil
unknwon f8d9a74
Check local hostname in repository migration
unknwon f042516
CHANGELOG
unknwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,64 @@ | ||
// Copyright 2022 The Gogs 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 netutil | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
var localCIDRs []*net.IPNet | ||
|
||
func init() { | ||
// Parsing hardcoded CIDR strings should never fail, if in case it does, let's | ||
// fail it at start. | ||
rawCIDRs := []string{ | ||
// https://datatracker.ietf.org/doc/html/rfc5735: | ||
"127.0.0.0/8", // Loopback | ||
"0.0.0.0/8", // "This" network | ||
"100.64.0.0/10", // Shared address space | ||
"169.254.0.0/16", // Link local | ||
"172.16.0.0/12", // Private-use networks | ||
"192.0.0.0/24", // IETF Protocol assignments | ||
"192.0.2.0/24", // TEST-NET-1 | ||
"192.88.99.0/24", // 6to4 Relay anycast | ||
"192.168.0.0/16", // Private-use networks | ||
"198.18.0.0/15", // Network interconnect | ||
"198.51.100.0/24", // TEST-NET-2 | ||
"203.0.113.0/24", // TEST-NET-3 | ||
"255.255.255.255/32", // Limited broadcast | ||
|
||
// https://datatracker.ietf.org/doc/html/rfc1918: | ||
"10.0.0.0/8", // Private-use networks | ||
|
||
// https://datatracker.ietf.org/doc/html/rfc6890: | ||
"::1/128", // Loopback | ||
"FC00::/7", // Unique local address | ||
"FE80::/10", // Multicast address | ||
} | ||
for _, raw := range rawCIDRs { | ||
_, cidr, err := net.ParseCIDR(raw) | ||
if err != nil { | ||
panic(fmt.Sprintf("parse CIDR %q: %v", raw, err)) | ||
} | ||
localCIDRs = append(localCIDRs, cidr) | ||
} | ||
} | ||
|
||
// IsLocalHostname returns true if given hostname is a known local address. | ||
func IsLocalHostname(hostname string) bool { | ||
ips, err := net.LookupIP(hostname) | ||
if err != nil { | ||
return true | ||
} | ||
for _, ip := range ips { | ||
for _, cidr := range localCIDRs { | ||
if cidr.Contains(ip) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file contains hidden or 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,36 @@ | ||
// Copyright 2022 The Gogs 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 netutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsLocalHostname(t *testing.T) { | ||
tests := []struct { | ||
hostname string | ||
want bool | ||
}{ | ||
{hostname: "localhost", want: true}, | ||
{hostname: "127.0.0.1", want: true}, | ||
{hostname: "::1", want: true}, | ||
{hostname: "0:0:0:0:0:0:0:1", want: true}, | ||
{hostname: "fuf.me", want: true}, | ||
{hostname: "127.0.0.95", want: true}, | ||
{hostname: "0.0.0.0", want: true}, | ||
{hostname: "192.168.123.45", want: true}, | ||
|
||
{hostname: "gogs.io", want: false}, | ||
{hostname: "google.com", want: false}, | ||
{hostname: "165.232.140.255", want: false}, | ||
} | ||
for _, test := range tests { | ||
t.Run("", func(t *testing.T) { | ||
assert.Equal(t, test.want, IsLocalHostname(test.hostname)) | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michaellrowley This is where the real fix to the report as far as I understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that seems to be the case (although without the rewritten
IsLocalHostname
it would still have been possible to bypass this call and the one invalidateWebhook
)