Skip to content

Commit 0e5547e

Browse files
authored
Improve URLToPath (#408)
- handle http:// scheme - cut out colons - make filename lowercase Signed-off-by: Cody Soyland <[email protected]>
1 parent 434b067 commit 0e5547e

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

pkg/tuf/client.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ func URLToPath(url string) string {
199199
// Strip scheme, replace slashes with dashes
200200
// e.g. https://github.github.com/prod-tuf-root -> github.github.com-prod-tuf-root
201201
fn := url
202-
if len(fn) > 8 && fn[:8] == "https://" {
203-
fn = fn[8:]
204-
}
202+
fn, _ = strings.CutPrefix(fn, "https://")
203+
fn, _ = strings.CutPrefix(fn, "http://")
205204
fn = strings.ReplaceAll(fn, "/", "-")
206-
return fn
205+
fn = strings.ReplaceAll(fn, ":", "-")
206+
207+
return strings.ToLower(fn)
207208
}

pkg/tuf/client_test.go

+49-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ package tuf
1717
import (
1818
"crypto"
1919
"crypto/sha256"
20+
"fmt"
2021
"net/url"
22+
"os"
23+
"path/filepath"
2124
"regexp"
2225
"strconv"
2326
"strings"
2427
"testing"
2528
"time"
2629

27-
"fmt"
28-
"os"
29-
"path/filepath"
30-
3130
"github.com/sigstore/sigstore/pkg/signature"
3231
"github.com/stretchr/testify/assert"
3332
"github.com/theupdateframework/go-tuf/v2/metadata"
@@ -463,3 +462,49 @@ func (r *testRepo) SetTimestamp(date time.Time) {
463462
r.t.Fatal(err)
464463
}
465464
}
465+
466+
func TestURLToPath(t *testing.T) {
467+
tests := []struct {
468+
name string
469+
url string
470+
want string
471+
}{
472+
{
473+
name: "no-change",
474+
url: "example.com",
475+
want: "example.com",
476+
},
477+
{
478+
name: "simple",
479+
url: "https://example.com",
480+
want: "example.com",
481+
},
482+
{
483+
name: "simple with path",
484+
url: "https://example.com/foo/bar",
485+
want: "example.com-foo-bar",
486+
},
487+
{
488+
name: "http scheme",
489+
url: "http://example.com/foo/bar",
490+
want: "example.com-foo-bar",
491+
},
492+
{
493+
name: "different port",
494+
url: "http://example.com:8080/foo/bar",
495+
want: "example.com-8080-foo-bar",
496+
},
497+
{
498+
name: "lowercase",
499+
url: "http://EXAMPLE.COM:8080/foo/bar",
500+
want: "example.com-8080-foo-bar",
501+
},
502+
}
503+
for _, tt := range tests {
504+
t.Run(tt.name, func(t *testing.T) {
505+
if got := URLToPath(tt.url); got != tt.want {
506+
t.Errorf("URLToPath() = %v, want %v", got, tt.want)
507+
}
508+
})
509+
}
510+
}

0 commit comments

Comments
 (0)