Skip to content

Commit

Permalink
Check scope/package name
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Sep 7, 2021
1 parent 579383f commit 5f24469
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 13 additions & 7 deletions server/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"errors"
"fmt"
"strings"

"github.com/ije/gox/utils"
Expand All @@ -21,24 +22,29 @@ func parsePkg(pathname string) (*pkg, error) {
scope := ""
packageName := a[0]
submodule := strings.Join(a[1:], "/")
if strings.HasPrefix(a[0], "@") && len(a) > 1 {
scope = a[0]
if strings.HasPrefix(packageName, "@") && len(a) > 1 {
scope = packageName[1:]
packageName = a[1]
submodule = strings.Join(a[2:], "/")
}

if strings.HasSuffix(submodule, ".d.ts") {
return nil, errors.New("invalid path")
// ref https://github.com/npm/validate-npm-package-name
if scope != "" && (len(scope) > 214 || !npmNaming.Is(scope)) {
return nil, fmt.Errorf("invalid scope '%s'", scope)
}

name, version := utils.SplitByLastByte(packageName, '@')
if scope != "" {
name = scope + "/" + name
if name != "" && (len(name) > 214 || !npmNaming.Is(name)) {
return nil, fmt.Errorf("invalid package name '%s'", name)
}
if name == "" {

if strings.HasSuffix(submodule, ".d.ts") {
return nil, errors.New("invalid path")
}

if scope != "" {
name = fmt.Sprintf("@%s/%s", scope, name)
}
if version == "" {
version = "latest"
}
Expand Down
2 changes: 2 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"sync"

"github.com/ije/gox/utils"
"github.com/ije/gox/valid"
)

var (
regFullVersion = regexp.MustCompile(`^\d+\.\d+\.\d+[a-zA-Z0-9\.\-]*$`)
regBuildVersionPath = regexp.MustCompile(`^/v\d+/`)
npmNaming = valid.Validator{valid.FromTo{'a', 'z'}, valid.FromTo{'0', '9'}, valid.Eq('.'), valid.Eq('_'), valid.Eq('-')}
)

// A Country record of mmdb.
Expand Down

0 comments on commit 5f24469

Please sign in to comment.