From f8bcca1ea0c0ad952fffd0539b625de69b9099f0 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 28 Apr 2022 09:48:27 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20path=20url=20maybe=20enco?= =?UTF-8?q?de=20(#1729)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/web_not_found.js | 6 +++++- test/controllers/web/package/show.test.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/middleware/web_not_found.js b/middleware/web_not_found.js index e2d1019..013e873 100644 --- a/middleware/web_not_found.js +++ b/middleware/web_not_found.js @@ -23,10 +23,14 @@ module.exports = function* notFound(next) { } // package not found - m = /^\/package\/([\w\-\_\.]+)\/?$/.exec(this.url); + m = /^\/package\/([\w\-\_\.]+)\/?$/.exec(this.path); if (!m) { // scoped packages m = /^\/package\/(@[\w\-\.]+\/[\w\-\.]+)$/.exec(this.path); + // maybe encode url: /package/%40foo%2Fawdawda + if (!m && this.path.startsWith('/package/%40')) { + m = /^\/package\/(@[\w\-\.]+\/[\w\-\.]+)$/.exec(decodeURIComponent(this.path)); + } } var name = null; var title = '404: Page Not Found'; diff --git a/test/controllers/web/package/show.test.js b/test/controllers/web/package/show.test.js index e405597..bf59a81 100644 --- a/test/controllers/web/package/show.test.js +++ b/test/controllers/web/package/show.test.js @@ -124,6 +124,16 @@ describe('test/controllers/web/package/show.test.js', () => { .expect(/Can not found package match @cnpmtest\/testmodule-repo-short-https-404/, done); }); + it('should get 404 show sync button on scoped package with encode url', done => { + mm(config, 'syncModel', 'all'); + request(app) + .get('/package/%40foo%2Fawdawda') + .expect(404) + .expect('content-type', 'text/html; charset=utf-8') + .expect(/>SYNC<\/a> from official npm registry/) + .expect(/Can not found package match @foo\/awdawda/, done); + }); + it('should get 404 show sync button on non-scoped package', done => { mm(config, 'syncModel', 'all'); request(app)