Skip to content

Commit 0dbfe03

Browse files
committed
requested fixes: allowableQuery regex, and sanitize queries with @ in the name
1 parent e393a99 commit 0dbfe03

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

internal/devpkg/package.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func (p *Package) FullPackageAttributePath() (string, error) {
270270
}
271271

272272
// NormalizedPackageAttributePath returns an attribute path normalized by nix
273-
// lookupNixInfo. This is useful for comparing different attribute paths that may
273+
// search. This is useful for comparing different attribute paths that may
274274
// point to the same package. Note, it may be an expensive call.
275275
func (p *Package) NormalizedPackageAttributePath() (string, error) {
276276
if p.normalizedPackageAttributePathCache != "" {
@@ -284,7 +284,7 @@ func (p *Package) NormalizedPackageAttributePath() (string, error) {
284284
return p.normalizedPackageAttributePathCache, nil
285285
}
286286

287-
// normalizePackageAttributePath calls nix lookupNixInfo to find the normalized attribute
287+
// normalizePackageAttributePath calls nix search to find the normalized attribute
288288
// path. It may be an expensive call (~100ms).
289289
func (p *Package) normalizePackageAttributePath() (string, error) {
290290
var query string

internal/nix/search.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,23 @@ type CachedSearchResult struct {
118118
Query string `json:"query"`
119119
}
120120

121+
var allowableQuery = regexp.MustCompile("^github:NixOS/nixpkgs/[0-9a-f]{40}#[^#]+$")
122+
123+
func isAllowableQuery(query string) bool {
124+
return allowableQuery.MatchString(query)
125+
}
126+
121127
// SearchNixpkgsAttribute is a wrapper around searchSystem that caches results.
122128
// NOTE: we should be very conservative in where we use this function. `nix search`
123129
// accepts generalized `installable regex` as arguments but is slow. For certain
124130
// queries of the form `nixpkgs/<commit-hash>#attribute`, we can know for sure that
125131
// once `nix search` returns a valid result, it will always be the very same result.
126132
// Hence we can cache it locally and answer future queries fast, by not calling `nix search`.
127133
func SearchNixpkgsAttribute(query string) (map[string]*Info, error) {
134+
if !isAllowableQuery(query) {
135+
return nil, errors.Errorf("invalid query: %s, must match regex: %s", query, allowableQuery)
136+
}
137+
128138
key := cacheKey(query)
129139

130140
// Check if the query was already cached, and return the result if so
@@ -170,10 +180,10 @@ func filecacheNeedsUpdate(err error) bool {
170180
// cacheKey sanitizes the search query to be a valid unix filename.
171181
// This cache key is used as the filename to store the cache value, and having a
172182
// representation of the query is important for debuggability.
173-
func cacheKey(input string) string {
183+
func cacheKey(query string) string {
174184
// Replace disallowed characters with underscores.
175-
re := regexp.MustCompile(`[:/#+]`)
176-
sanitized := re.ReplaceAllString(input, "_")
185+
re := regexp.MustCompile(`[:/#@+]`)
186+
sanitized := re.ReplaceAllString(query, "_")
177187

178188
// Remove any remaining invalid characters.
179189
sanitized = regexp.MustCompile(`[^\w\.-]`).ReplaceAllString(sanitized, "")

internal/nix/search_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ func TestSearchCacheKey(t *testing.T) {
1313
"github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go_1_19",
1414
"github_NixOS_nixpkgs_8670e496ffd093b60e74e7fa53526aa5920d09eb_go_1_19",
1515
},
16+
{
17+
"github:nixos/nixpkgs/7d0ed7f2e5aea07ab22ccb338d27fbe347ed2f11#emacsPackages.@",
18+
"github_nixos_nixpkgs_7d0ed7f2e5aea07ab22ccb338d27fbe347ed2f11_emacsPackages._",
19+
},
1620
}
1721

1822
for _, testCase := range testCases {
@@ -24,3 +28,35 @@ func TestSearchCacheKey(t *testing.T) {
2428
})
2529
}
2630
}
31+
32+
func TestIsAllowableQuery(t *testing.T) {
33+
testCases := []struct {
34+
in string
35+
expected bool
36+
}{
37+
{
38+
"github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go_1_19",
39+
true,
40+
},
41+
{
42+
"github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb",
43+
false,
44+
},
45+
{
46+
"github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#",
47+
false,
48+
},
49+
{
50+
"github:NixOS/nixpkgs/nixpkgs-unstable#go_1_19",
51+
false,
52+
},
53+
}
54+
for _, testCase := range testCases {
55+
t.Run(testCase.in, func(t *testing.T) {
56+
out := isAllowableQuery(testCase.in)
57+
if out != testCase.expected {
58+
t.Errorf("got %t, want %t", out, testCase.expected)
59+
}
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)