Skip to content

Commit

Permalink
Fix download mode leak for latest and list handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ngshiheng committed Feb 16, 2023
1 parent dbe3b5d commit feab0c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/download/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func (p *protocol) List(ctx context.Context, mod string) ([]string, error) {
var sErr, goErr error
var wg sync.WaitGroup

// if download mode is none for the specific mod, just return an error.
downloadMode := p.df.Match(mod)
if downloadMode == mode.None {
return nil, errors.E(op, errors.M(mod), errors.KindNotFound)
}

/*
TODO: potential refactor:
Expand Down Expand Up @@ -183,9 +189,16 @@ func (p *protocol) Latest(ctx context.Context, mod string) (*storage.RevInfo, er
const op errors.Op = "protocol.Latest"
ctx, span := observ.StartSpan(ctx, op.String())
defer span.End()

// if download mode is none for the specific mod, just return an error.
downloadMode := p.df.Match(mod)
if downloadMode == mode.None {
return nil, errors.E(op, errors.M(mod), errors.KindNotFound)
}

if p.networkMode == Offline {
// Go never pings the /@latest endpoint _first_. It always tries /list and if that
// endpoint returns an empty list then it fallsback to calling /@latest.
// endpoint returns an empty list then it fallbacks to calling /@latest.
return nil, errors.E(op, "Athens is in offline mode, use /list endpoint", errors.KindNotFound)
}
lr, _, err := p.lister.List(ctx, mod)
Expand Down
1 change: 1 addition & 0 deletions pkg/download/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func TestListMode(t *testing.T) {
storage: strg,
lister: ml,
networkMode: tc.networkmode,
df: &mode.DownloadFile{},
}
for _, tag := range tc.storageTags {
err := strg.Save(ctx, tc.path, tag, []byte("mod"), bytes.NewReader([]byte("zip")), []byte("info"))
Expand Down

0 comments on commit feab0c7

Please sign in to comment.