Skip to content

Commit 634544f

Browse files
authored
devpkg: return error for invalid flakeref scheme (#1629)
1 parent f94da94 commit 634544f

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

internal/devpkg/flakeref.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error)
187187
if err := parseGitHubFlakeRef(refURL, &parsed); err != nil {
188188
return FlakeRef{}, "", err
189189
}
190+
default:
191+
return FlakeRef{}, "", redact.Errorf("unsupported flake reference URL scheme: %s", redact.Safe(refURL.Scheme))
190192
}
191193
return parsed, refURL.Fragment, nil
192194
}

internal/devpkg/flakeref_test.go

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,7 @@ import (
77
)
88

99
func TestParseFlakeRef(t *testing.T) {
10-
// Test cases use the zero-value to check for invalid flakerefs because
11-
// we don't care about the specific error message.
1210
cases := map[string]FlakeRef{
13-
// Empty string is not a valid flake reference.
14-
"": {},
15-
16-
// Not a path and not a valid URL.
17-
"://bad/url": {},
18-
19-
// Invalid escape.
20-
"path:./relative/my%flake": {},
21-
2211
// Path-like references start with a '.' or '/'.
2312
// This distinguishes them from indirect references
2413
// (./nixpkgs is a directory; nixpkgs is an indirect).
@@ -35,14 +24,6 @@ func TestParseFlakeRef(t *testing.T) {
3524
"./Ûñî©ôδ€/flake\n": {Type: FlakeTypePath, Path: "./Ûñî©ôδ€/flake\n"},
3625
"/Ûñî©ôδ€/flake\n": {Type: FlakeTypePath, Path: "/Ûñî©ôδ€/flake\n"},
3726

38-
// Path-like references don't allow paths with a '?' or '#'.
39-
"./invalid#path": {},
40-
"./invalid?path": {},
41-
"/invalid#path": {},
42-
"/invalid?path": {},
43-
"/#": {},
44-
"/?": {},
45-
4627
// URL-like path references.
4728
"path:": {Type: FlakeTypePath, Path: ""},
4829
"path:.": {Type: FlakeTypePath, Path: "."},
@@ -82,12 +63,6 @@ func TestParseFlakeRef(t *testing.T) {
8263
"github:NixOS/nix?rev=5233fd2ba76a3accb5aaa999c00509a11fd0793c": {Type: FlakeTypeGitHub, Owner: "NixOS", Repo: "nix", Rev: "5233fd2ba76a3accb5aaa999c00509a11fd0793c"},
8364
"github:NixOS/nix?host=example.com": {Type: FlakeTypeGitHub, Owner: "NixOS", Repo: "nix", Host: "example.com"},
8465

85-
// GitHub references with invalid ref + rev combinations.
86-
"github:NixOS/nix?ref=v1.2.3&rev=5233fd2ba76a3accb5aaa999c00509a11fd0793c": {},
87-
"github:NixOS/nix/v1.2.3?ref=v4.5.6": {},
88-
"github:NixOS/nix/5233fd2ba76a3accb5aaa999c00509a11fd0793c?rev=e486d8d40e626a20e06d792db8cc5ac5aba9a5b4": {},
89-
"github:NixOS/nix/5233fd2ba76a3accb5aaa999c00509a11fd0793c?ref=v1.2.3": {},
90-
9166
// The github type allows clone-style URLs. The username and
9267
// host are ignored.
9368
"github://[email protected]/NixOS/nix": {Type: FlakeTypeGitHub, Owner: "NixOS", Repo: "nix"},
@@ -142,7 +117,6 @@ func TestParseFlakeRef(t *testing.T) {
142117
"http://example.com/flake.git": {Type: FlakeTypeFile, URL: "http://example.com/flake.git"},
143118
"http://example.com/flake?dir=subdir": {Type: FlakeTypeFile, URL: "http://example.com/flake?dir=subdir", Dir: "subdir"},
144119
}
145-
146120
for ref, want := range cases {
147121
t.Run(ref, func(t *testing.T) {
148122
got, err := ParseFlakeRef(ref)
@@ -156,6 +130,67 @@ func TestParseFlakeRef(t *testing.T) {
156130
}
157131
}
158132

133+
func TestParseFlakeRefError(t *testing.T) {
134+
t.Run("EmptyString", func(t *testing.T) {
135+
ref := ""
136+
_, err := ParseFlakeRef(ref)
137+
if err == nil {
138+
t.Error("got nil error for bad flakeref:", ref)
139+
}
140+
})
141+
t.Run("InvalidURL", func(t *testing.T) {
142+
ref := "://bad/url"
143+
_, err := ParseFlakeRef(ref)
144+
if err == nil {
145+
t.Error("got nil error for bad flakeref:", ref)
146+
}
147+
})
148+
t.Run("InvalidURLEscape", func(t *testing.T) {
149+
ref := "path:./relative/my%flake"
150+
_, err := ParseFlakeRef(ref)
151+
if err == nil {
152+
t.Error("got nil error for bad flakeref:", ref)
153+
}
154+
})
155+
t.Run("UnsupportedURLScheme", func(t *testing.T) {
156+
ref := "runx:mvdan/gofumpt@latest"
157+
_, err := ParseFlakeRef(ref)
158+
if err == nil {
159+
t.Error("got nil error for bad flakeref:", ref)
160+
}
161+
})
162+
t.Run("PathLikeWith?#", func(t *testing.T) {
163+
in := []string{
164+
"./invalid#path",
165+
"./invalid?path",
166+
"/invalid#path",
167+
"/invalid?path",
168+
"/#",
169+
"/?",
170+
}
171+
for _, ref := range in {
172+
_, err := ParseFlakeRef(ref)
173+
if err == nil {
174+
t.Error("got nil error for bad flakeref:", ref)
175+
}
176+
}
177+
})
178+
t.Run("GitHubInvalidRefRevCombo", func(t *testing.T) {
179+
in := []string{
180+
"github:NixOS/nix?ref=v1.2.3&rev=5233fd2ba76a3accb5aaa999c00509a11fd0793c",
181+
"github:NixOS/nix/v1.2.3?ref=v4.5.6",
182+
"github:NixOS/nix/5233fd2ba76a3accb5aaa999c00509a11fd0793c?rev=e486d8d40e626a20e06d792db8cc5ac5aba9a5b4",
183+
"github:NixOS/nix/5233fd2ba76a3accb5aaa999c00509a11fd0793c?ref=v1.2.3",
184+
}
185+
for _, ref := range in {
186+
_, err := ParseFlakeRef(ref)
187+
if err == nil {
188+
t.Error("got nil error for bad flakeref:", ref)
189+
}
190+
}
191+
})
192+
}
193+
159194
func TestFlakeRefString(t *testing.T) {
160195
cases := map[FlakeRef]string{
161196
{}: "",

0 commit comments

Comments
 (0)