2014-12-12 06:10:18 +00:00
|
|
|
package v2
|
2014-11-08 00:08:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-02-26 16:15:15 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2014-11-08 00:08:14 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
2015-02-24 22:59:01 +00:00
|
|
|
"strings"
|
2014-11-08 00:08:14 +00:00
|
|
|
"testing"
|
2015-02-26 16:15:15 +00:00
|
|
|
"time"
|
2014-11-08 00:08:14 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
type routeTestCase struct {
|
2015-02-26 16:15:15 +00:00
|
|
|
RequestURI string
|
|
|
|
ExpectedURI string
|
|
|
|
Vars map[string]string
|
|
|
|
RouteName string
|
|
|
|
StatusCode int
|
2014-11-08 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestRouter registers a test handler with all the routes and ensures that
|
|
|
|
// each route returns the expected path variables. Not method verification is
|
|
|
|
// present. This not meant to be exhaustive but as check to ensure that the
|
|
|
|
// expected variables are extracted.
|
|
|
|
//
|
|
|
|
// This may go away as the application structure comes together.
|
|
|
|
func TestRouter(t *testing.T) {
|
2015-02-26 16:15:15 +00:00
|
|
|
testCases := []routeTestCase{
|
2014-12-11 06:33:36 +00:00
|
|
|
{
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameBase,
|
2014-12-11 06:33:36 +00:00
|
|
|
RequestURI: "/v2/",
|
|
|
|
Vars: map[string]string{},
|
|
|
|
},
|
2015-01-28 22:51:02 +00:00
|
|
|
{
|
|
|
|
RouteName: RouteNameManifest,
|
|
|
|
RequestURI: "/v2/foo/manifests/bar",
|
|
|
|
Vars: map[string]string{
|
2015-02-26 02:04:28 +00:00
|
|
|
"name": "foo",
|
|
|
|
"reference": "bar",
|
2015-01-28 22:51:02 +00:00
|
|
|
},
|
|
|
|
},
|
2014-11-08 00:08:14 +00:00
|
|
|
{
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameManifest,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/manifests/tag",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2015-02-26 02:04:28 +00:00
|
|
|
"name": "foo/bar",
|
|
|
|
"reference": "tag",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RouteName: RouteNameManifest,
|
|
|
|
RequestURI: "/v2/foo/bar/manifests/sha256:abcdef01234567890",
|
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
|
|
|
"reference": "sha256:abcdef01234567890",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameTags,
|
2014-11-13 00:59:50 +00:00
|
|
|
RequestURI: "/v2/foo/bar/tags/list",
|
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameBlob,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/tarsum.dev+foo:abcdef0919234",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
2014-11-19 03:38:14 +00:00
|
|
|
"digest": "tarsum.dev+foo:abcdef0919234",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameBlob,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/sha256:abcdef0919234",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
2014-11-19 03:38:14 +00:00
|
|
|
"digest": "sha256:abcdef0919234",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameBlobUpload,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2014-11-19 03:38:14 +00:00
|
|
|
"name": "foo/bar",
|
2014-11-13 00:59:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameBlobUploadChunk,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/uuid",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2014-11-19 03:38:14 +00:00
|
|
|
"name": "foo/bar",
|
|
|
|
"uuid": "uuid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-04-02 01:57:59 +00:00
|
|
|
// support uuid proper
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameBlobUploadChunk,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/D95306FA-FAD3-4E36-8D41-CF1C93EF8286",
|
2014-11-19 03:38:14 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
|
|
|
"uuid": "D95306FA-FAD3-4E36-8D41-CF1C93EF8286",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameBlobUploadChunk,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/RDk1MzA2RkEtRkFEMy00RTM2LThENDEtQ0YxQzkzRUY4Mjg2IA==",
|
2014-11-19 03:38:14 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
|
|
|
"uuid": "RDk1MzA2RkEtRkFEMy00RTM2LThENDEtQ0YxQzkzRUY4Mjg2IA==",
|
2014-11-13 00:59:50 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-02 01:57:59 +00:00
|
|
|
{
|
|
|
|
// supports urlsafe base64
|
|
|
|
RouteName: RouteNameBlobUploadChunk,
|
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/RDk1MzA2RkEtRkFEMy00RTM2LThENDEtQ0YxQzkzRUY4Mjg2IA_-==",
|
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
|
|
|
"uuid": "RDk1MzA2RkEtRkFEMy00RTM2LThENDEtQ0YxQzkzRUY4Mjg2IA_-==",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// does not match
|
|
|
|
RouteName: RouteNameBlobUploadChunk,
|
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/totalandcompletejunk++$$-==",
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
},
|
2014-11-13 00:59:50 +00:00
|
|
|
{
|
|
|
|
// Check ambiguity: ensure we can distinguish between tags for
|
|
|
|
// "foo/bar/image/image" and image for "foo/bar/image" with tag
|
|
|
|
// "tags"
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameManifest,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/manifests/manifests/tags",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2015-02-26 02:04:28 +00:00
|
|
|
"name": "foo/bar/manifests",
|
|
|
|
"reference": "tags",
|
2014-11-13 00:59:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// This case presents an ambiguity between foo/bar with tag="tags"
|
2014-11-19 03:38:14 +00:00
|
|
|
// and list tags for "foo/bar/manifest"
|
2014-12-12 05:08:23 +00:00
|
|
|
RouteName: RouteNameTags,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/manifests/tags/list",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2014-12-11 06:29:58 +00:00
|
|
|
"name": "foo/bar/manifests",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
2015-02-26 16:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkTestRouter(t, testCases, "", true)
|
|
|
|
checkTestRouter(t, testCases, "/prefix/", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterWithPathTraversals(t *testing.T) {
|
|
|
|
testCases := []routeTestCase{
|
2014-11-13 22:39:13 +00:00
|
|
|
{
|
2015-02-26 16:15:15 +00:00
|
|
|
RouteName: RouteNameBlobUploadChunk,
|
|
|
|
RequestURI: "/v2/foo/../../blob/uploads/D95306FA-FAD3-4E36-8D41-CF1C93EF8286",
|
|
|
|
ExpectedURI: "/blob/uploads/D95306FA-FAD3-4E36-8D41-CF1C93EF8286",
|
|
|
|
StatusCode: http.StatusNotFound,
|
2014-11-13 22:39:13 +00:00
|
|
|
},
|
2015-02-26 16:15:15 +00:00
|
|
|
{
|
|
|
|
// Testing for path traversal attack handling
|
|
|
|
RouteName: RouteNameTags,
|
|
|
|
RequestURI: "/v2/foo/../bar/baz/tags/list",
|
|
|
|
ExpectedURI: "/v2/bar/baz/tags/list",
|
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "bar/baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
checkTestRouter(t, testCases, "", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterWithBadCharacters(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
testCases := []routeTestCase{
|
|
|
|
{
|
|
|
|
RouteName: RouteNameBlobUploadChunk,
|
|
|
|
RequestURI: "/v2/foo/blob/uploads/不95306FA-FAD3-4E36-8D41-CF1C93EF8286",
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Testing for path traversal attack handling
|
|
|
|
RouteName: RouteNameTags,
|
|
|
|
RequestURI: "/v2/foo/不bar/tags/list",
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
checkTestRouter(t, testCases, "", true)
|
|
|
|
} else {
|
|
|
|
// in the long version we're going to fuzz the router
|
|
|
|
// with random UTF8 characters not in the 128 bit ASCII range.
|
|
|
|
// These are not valid characters for the router and we expect
|
|
|
|
// 404s on every test.
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
testCases := make([]routeTestCase, 1000)
|
|
|
|
for idx := range testCases {
|
|
|
|
testCases[idx] = routeTestCase{
|
|
|
|
RouteName: RouteNameTags,
|
|
|
|
RequestURI: fmt.Sprintf("/v2/%v/%v/tags/list", randomString(10), randomString(10)),
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkTestRouter(t, testCases, "", true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkTestRouter(t *testing.T, testCases []routeTestCase, prefix string, deeplyEqual bool) {
|
|
|
|
router := RouterWithPrefix(prefix)
|
|
|
|
|
|
|
|
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
testCase := routeTestCase{
|
|
|
|
RequestURI: r.RequestURI,
|
|
|
|
Vars: mux.Vars(r),
|
|
|
|
RouteName: mux.CurrentRoute(r).GetName(),
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
|
|
|
|
if err := enc.Encode(testCase); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2015-02-24 22:59:01 +00:00
|
|
|
|
2015-02-26 16:15:15 +00:00
|
|
|
// Startup test server
|
|
|
|
server := httptest.NewServer(router)
|
|
|
|
|
|
|
|
for _, testcase := range testCases {
|
|
|
|
testcase.RequestURI = strings.TrimSuffix(prefix, "/") + testcase.RequestURI
|
2014-11-08 00:08:14 +00:00
|
|
|
// Register the endpoint
|
2014-12-19 02:21:57 +00:00
|
|
|
route := router.GetRoute(testcase.RouteName)
|
|
|
|
if route == nil {
|
|
|
|
t.Fatalf("route for name %q not found", testcase.RouteName)
|
|
|
|
}
|
|
|
|
|
|
|
|
route.Handler(testHandler)
|
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
u := server.URL + testcase.RequestURI
|
2014-11-08 00:08:14 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(u)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error issuing get request: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-11-13 22:39:13 +00:00
|
|
|
if testcase.StatusCode == 0 {
|
|
|
|
// Override default, zero-value
|
|
|
|
testcase.StatusCode = http.StatusOK
|
|
|
|
}
|
2015-02-26 16:15:15 +00:00
|
|
|
if testcase.ExpectedURI == "" {
|
|
|
|
// Override default, zero-value
|
|
|
|
testcase.ExpectedURI = testcase.RequestURI
|
|
|
|
}
|
2014-11-13 22:39:13 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != testcase.StatusCode {
|
2014-11-13 00:59:50 +00:00
|
|
|
t.Fatalf("unexpected status for %s: %v %v", u, resp.Status, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2014-11-13 22:39:13 +00:00
|
|
|
if testcase.StatusCode != http.StatusOK {
|
|
|
|
// We don't care about json response.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-11-08 00:08:14 +00:00
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
var actualRouteInfo routeTestCase
|
2014-11-08 00:08:14 +00:00
|
|
|
if err := dec.Decode(&actualRouteInfo); err != nil {
|
|
|
|
t.Fatalf("error reading json response: %v", err)
|
|
|
|
}
|
2014-11-13 22:39:13 +00:00
|
|
|
// Needs to be set out of band
|
|
|
|
actualRouteInfo.StatusCode = resp.StatusCode
|
2014-11-08 00:08:14 +00:00
|
|
|
|
2015-02-26 16:15:15 +00:00
|
|
|
if actualRouteInfo.RequestURI != testcase.ExpectedURI {
|
|
|
|
t.Fatalf("URI %v incorrectly parsed, expected %v", actualRouteInfo.RequestURI, testcase.ExpectedURI)
|
|
|
|
}
|
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
if actualRouteInfo.RouteName != testcase.RouteName {
|
|
|
|
t.Fatalf("incorrect route %q matched, expected %q", actualRouteInfo.RouteName, testcase.RouteName)
|
|
|
|
}
|
|
|
|
|
2015-02-26 16:15:15 +00:00
|
|
|
// when testing deep equality, the actualRouteInfo has an empty ExpectedURI, we don't want
|
|
|
|
// that to make the comparison fail. We're otherwise done with the testcase so empty the
|
|
|
|
// testcase.ExpectedURI
|
|
|
|
testcase.ExpectedURI = ""
|
|
|
|
if deeplyEqual && !reflect.DeepEqual(actualRouteInfo, testcase) {
|
2014-11-13 00:59:50 +00:00
|
|
|
t.Fatalf("actual does not equal expected: %#v != %#v", actualRouteInfo, testcase)
|
2014-11-08 00:08:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-02-26 16:15:15 +00:00
|
|
|
|
|
|
|
// -------------- START LICENSED CODE --------------
|
|
|
|
// The following code is derivative of https://github.com/google/gofuzz
|
|
|
|
// gofuzz is licensed under the Apache License, Version 2.0, January 2004,
|
|
|
|
// a copy of which can be found in the LICENSE file at the root of this
|
|
|
|
// repository.
|
|
|
|
|
|
|
|
// These functions allow us to generate strings containing only multibyte
|
|
|
|
// characters that are invalid in our URLs. They are used above for fuzzing
|
|
|
|
// to ensure we always get 404s on these invalid strings
|
|
|
|
type charRange struct {
|
|
|
|
first, last rune
|
|
|
|
}
|
|
|
|
|
|
|
|
// choose returns a random unicode character from the given range, using the
|
|
|
|
// given randomness source.
|
|
|
|
func (r *charRange) choose() rune {
|
|
|
|
count := int64(r.last - r.first)
|
|
|
|
return r.first + rune(rand.Int63n(count))
|
|
|
|
}
|
|
|
|
|
|
|
|
var unicodeRanges = []charRange{
|
|
|
|
{'\u00a0', '\u02af'}, // Multi-byte encoded characters
|
|
|
|
{'\u4e00', '\u9fff'}, // Common CJK (even longer encodings)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomString(length int) string {
|
|
|
|
runes := make([]rune, length)
|
|
|
|
for i := range runes {
|
|
|
|
runes[i] = unicodeRanges[rand.Intn(len(unicodeRanges))].choose()
|
|
|
|
}
|
|
|
|
return string(runes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------- END LICENSED CODE --------------
|