Skip to content

Commit 8d28eed

Browse files
Avish34Avish Porwaldomdomeggclaude
authored
Check for localhost in remote url (#355)
Add check for localhost in remote url. #274 (comment) ## Motivation and Context We don't want authors to publish server with remote url set as localhost. ## How Has This Been Tested? It has been tested using UTs. ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update --------- Co-authored-by: Avish Porwal <[email protected]> Co-authored-by: Adam Jones <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent b7a3a44 commit 8d28eed

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

internal/validators/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func IsValidURL(rawURL string) bool {
4343
return false
4444
}
4545

46-
if u.Host == "" {
46+
if u.Host == "" || u.Hostname() == "localhost" {
4747
return false
4848
}
4949
return true

internal/validators/validators_test.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,46 @@ func TestValidate(t *testing.T) {
179179
},
180180
expectedError: validators.ErrInvalidRemoteURL.Error(),
181181
},
182+
{
183+
name: "remote with localhost url",
184+
serverDetail: apiv0.ServerJSON{
185+
Name: "com.example/test-server",
186+
Description: "A test server",
187+
Repository: model.Repository{
188+
URL: "https://github.com/owner/repo",
189+
Source: "github",
190+
},
191+
VersionDetail: model.VersionDetail{
192+
Version: "1.0.0",
193+
},
194+
Remotes: []model.Remote{
195+
{
196+
URL: "http://localhost",
197+
},
198+
},
199+
},
200+
expectedError: validators.ErrInvalidRemoteURL.Error(),
201+
},
202+
{
203+
name: "remote with localhost url with port",
204+
serverDetail: apiv0.ServerJSON{
205+
Name: "com.example/test-server",
206+
Description: "A test server",
207+
Repository: model.Repository{
208+
URL: "https://github.com/owner/repo",
209+
Source: "github",
210+
},
211+
VersionDetail: model.VersionDetail{
212+
Version: "1.0.0",
213+
},
214+
Remotes: []model.Remote{
215+
{
216+
URL: "http://localhost:3000",
217+
},
218+
},
219+
},
220+
expectedError: validators.ErrInvalidRemoteURL.Error(),
221+
},
182222
{
183223
name: "multiple remotes with one invalid",
184224
serverDetail: apiv0.ServerJSON{
@@ -311,16 +351,6 @@ func TestValidate_RemoteNamespaceMatch(t *testing.T) {
311351
expectError: true,
312352
errorMsg: "remote URL host api.github.com does not match publisher domain microsoft.com",
313353
},
314-
{
315-
name: "localhost URLs allowed with any namespace",
316-
serverDetail: apiv0.ServerJSON{
317-
Name: "com.example/test-server",
318-
Remotes: []model.Remote{
319-
{URL: "http://localhost:3000/sse"},
320-
},
321-
},
322-
expectError: false,
323-
},
324354
{
325355
name: "invalid URL format",
326356
serverDetail: apiv0.ServerJSON{

0 commit comments

Comments
 (0)