Skip to content

NPM Package Registry returns HTTP400 for packages with single-character names #26551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TimberBro opened this issue Aug 16, 2023 · 2 comments · Fixed by #26595
Closed

NPM Package Registry returns HTTP400 for packages with single-character names #26551

TimberBro opened this issue Aug 16, 2023 · 2 comments · Fixed by #26595

Comments

@TimberBro
Copy link
Contributor

Description

Steps to reproduce the issue:

  1. Create new token or find existing token with privileges to publish packages.
  2. Install NPM locally or use container.
  3. Run npm i q in environment from previous step
    It'll download https://www.npmjs.com/package/q/v/1.5.1 as your dependency and put it in ./node_modules/q/ directory.
  4. Run
    npm config set registry http://{gitea_url}:3000/api/packages/{username}/npm/
    npm config set -- '//{gitea_url}:3000/api/packages/{username}/npm/:_authToken' "{token}"
    
  5. Run npm publish ./node_modules/q/ to publish this package to Gitea.
  6. Error from NPM
    npm ERR! code E400
    npm ERR! 400 Bad Request - PUT http://{gitea_url}:3000/api/packages/{username}/npm/q - package name is invalid
    

Error from Gitea log

2023/08/16 23:11:16 ...ges/helper/helper.go:34:LogAndProcessError() [D] package name is invalid
2023/08/16 23:11:16 ...eb/routing/logger.go:102:func1() [I] router: completed PUT /api/packages/{username}/npm/q for 172.18.0.1:52196, 400 Bad Request in 19.4ms @ npm/npm.go:154(npm.UploadPackage)

Gitea Version

3b129aa

Can you reproduce the bug on the Gitea demo site?

No

Log Gist

No response

Screenshots

No response

Git Version

2.40.1

Operating System

Ubuntu 22.04

How are you running Gitea?

Gitea running in docker.

Docker version:

vagrant@ubuntu:~$ docker version
Client:
 Version:           20.10.25
 API version:       1.41
 Go version:        go1.18.1
 Git commit:        20.10.25-0ubuntu1~22.04.1
 Built:             Fri Jul 14 21:58:09 2023
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server:
 Engine:
  Version:          20.10.25
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.1
  Git commit:       20.10.25-0ubuntu1~22.04.1
  Built:            Thu Jun 29 21:21:05 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.2
  GitCommit:
 runc:
  Version:          1.1.7-0ubuntu1~22.04.1
  GitCommit:
 docker-init:
  Version:          0.19.0
  GitCommit:

With docker-compose version:

vagrant@ubuntu:~$ docker-compose version
docker-compose version 1.29.2, build unknown
docker-py version: 5.0.3
CPython version: 3.10.12
OpenSSL version: OpenSSL 3.0.2 15 Mar 2022

Database

PostgreSQL

@TimberBro
Copy link
Contributor Author

Additional information:

  1. q is not the only package affected by the error.
    More examples:
    https://www.npmjs.com/package/d
    https://www.npmjs.com/package/@types/q

  2. I was able to find a method, that leads to this problem.
    For some reason, these names does not match against this regular expression:

    var nameMatch = regexp.MustCompile(`\A((@[^\s\/~'!\(\)\*]+?)[\/])?([^_.][^\s\/~'!\(\)\*]+)\z`)

@silverwind
Copy link
Member

silverwind commented Aug 17, 2023

Should check/test that regex. This package may be a good starting point:

https://github.com/npm/validate-npm-package-name

silverwind pushed a commit that referenced this issue Aug 20, 2023
- Added new tests to cover corner cases
- Replace existing regex with new one
Closes #26551 

---
As @silverwind suggested, I started from
[validate-npm-package-name](https://github.com/npm/validate-npm-package-name),
but found this solution too complicated.
Then I tried to fix existing regex myself, but thought, that exclude all
restricted symbols is harder, than set only allowed symbols.
Then I search a bit more and found
[package-name-regex](https://github.com/dword-design/package-name-regex)
and regex from it works for all new test cases.

Let me know, if more information or help with this PR is needed.
GiteaBot pushed a commit to GiteaBot/gitea that referenced this issue Aug 20, 2023
- Added new tests to cover corner cases
- Replace existing regex with new one
Closes go-gitea#26551 

---
As @silverwind suggested, I started from
[validate-npm-package-name](https://github.com/npm/validate-npm-package-name),
but found this solution too complicated.
Then I tried to fix existing regex myself, but thought, that exclude all
restricted symbols is harder, than set only allowed symbols.
Then I search a bit more and found
[package-name-regex](https://github.com/dword-design/package-name-regex)
and regex from it works for all new test cases.

Let me know, if more information or help with this PR is needed.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants