Compare commits

...

1 Commits

Author SHA1 Message Date
天玎
1a67afac72 [WIP] fix: version length validation 2021-07-16 13:39:14 +08:00
18 changed files with 94 additions and 31 deletions

View File

@@ -16,6 +16,11 @@ var config = {
version: version,
dataDir: dataDir,
// overriding length should alter database table length
versionLen: 70, // semver max length
nameLen: 214, // name max length
tagLen: 70, // tag name max length
/**
* Cluster mode
*/
@@ -120,7 +125,7 @@ var config = {
*/
database: {
db: 'cnpmjs_test',
db: 'cnpmjs',
username: 'root',
password: '',

View File

@@ -81,7 +81,7 @@ CREATE TABLE IF NOT EXISTS `module` (
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`author` varchar(100) NOT NULL COMMENT 'module author',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`version` varchar(70) NOT NULL COMMENT 'module version',
`description` longtext COMMENT 'module description',
`package` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'package.json',
`dist_shasum` varchar(100) DEFAULT NULL COMMENT 'module dist SHASUM',
@@ -104,7 +104,7 @@ CREATE TABLE IF NOT EXISTS `module` (
-- ALTER TABLE `module`
-- CHANGE `author` `author` varchar(100) NOT NULL COMMENT 'module author',
-- CHANGE `name` `name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
-- CHANGE `version` `version` varchar(30) NOT NULL COMMENT 'module version',
-- CHANGE `version` `version` varchar(70) NOT NULL COMMENT 'module version',
-- CHANGE `description` `description` longtext COMMENT 'module description',
-- CHANGE `package` `package` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'package.json',
-- CHANGE `dist_shasum` `dist_shasum` varchar(100) DEFAULT NULL COMMENT 'module dist SHASUM',
@@ -117,7 +117,7 @@ CREATE TABLE IF NOT EXISTS `module_abbreviated` (
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`version` varchar(70) NOT NULL COMMENT 'module version',
`package` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'the abbreviated metadata',
`publish_time` bigint(20) unsigned COMMENT 'the publish time',
PRIMARY KEY (`id`),
@@ -135,7 +135,7 @@ CREATE TABLE IF NOT EXISTS `package_readme` (
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`readme` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'the latest version readme',
`version` varchar(30) NOT NULL COMMENT 'module version',
`version` varchar(70) NOT NULL COMMENT 'module version',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`),
KEY `idx_gmt_modified` (`gmt_modified`)
@@ -164,8 +164,8 @@ CREATE TABLE IF NOT EXISTS `tag` (
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`tag` varchar(30) NOT NULL COMMENT 'tag name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`tag` varchar(70) NOT NULL COMMENT 'tag name',
`version` varchar(70) NOT NULL COMMENT 'module version',
`module_id` bigint(20) unsigned NOT NULL COMMENT 'module id',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`, `tag`),

View File

@@ -56,10 +56,12 @@
// KEY `idx_date` (`date`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module download total info';
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('DownloadTotal', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
},

View File

@@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS `module` (
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`author` varchar(100) NOT NULL COMMENT 'module author',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`version` varchar(70) NOT NULL COMMENT 'module version',
`description` longtext COMMENT 'module description',
`package` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'package.json',
`dist_shasum` varchar(100) DEFAULT NULL COMMENT 'module dist SHASUM',
@@ -21,6 +21,7 @@ CREATE TABLE IF NOT EXISTS `module` (
KEY `idx_author` (`author`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module info';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('Module', {
@@ -30,12 +31,12 @@ module.exports = function (sequelize, DataTypes) {
comment: 'first maintainer name'
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name'
},
version: {
type: DataTypes.STRING(30),
type: DataTypes.STRING(config.versionLen),
allowNull: false,
comment: 'module version'
},

View File

@@ -6,7 +6,7 @@ CREATE TABLE IF NOT EXISTS `module_abbreviated` (
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`version` varchar(70) NOT NULL COMMENT 'module version',
`package` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'the abbreviated metadata',
`publish_time` bigint(20) unsigned COMMENT 'the publish time',
PRIMARY KEY (`id`),
@@ -16,15 +16,17 @@ CREATE TABLE IF NOT EXISTS `module_abbreviated` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module abbreviated info';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('ModuleAbbreviated', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name'
},
version: {
type: DataTypes.STRING(30),
type: DataTypes.STRING(config.versionLen),
allowNull: false,
comment: 'module version'
},

View File

@@ -19,17 +19,18 @@ CREATE TABLE IF NOT EXISTS `module_deps` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`deps` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'which module depend on this module',
`deps` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'which module depend on this module',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name_deps` (`name`,`deps`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module deps';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('ModuleDependency', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
},

View File

@@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS `module_keyword` (
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module keyword';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('ModuleKeyword', {
@@ -35,7 +36,7 @@ module.exports = function (sequelize, DataTypes) {
comment: 'keyword',
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
},

View File

@@ -26,6 +26,7 @@
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module sync log';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('ModuleLog', {
@@ -35,7 +36,7 @@ module.exports = function (sequelize, DataTypes) {
comment: 'user name'
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
},

View File

@@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS `module_maintainer` (
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='private module maintainers';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('ModuleMaintainer', {
@@ -34,7 +35,7 @@ module.exports = function (sequelize, DataTypes) {
comment: 'user name'
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
}

View File

@@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS `module_star` (
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module star';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('ModuleStar', {
@@ -34,7 +35,7 @@ module.exports = function (sequelize, DataTypes) {
comment: 'user name'
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
}

View File

@@ -28,11 +28,12 @@ CREATE TABLE IF NOT EXISTS `module_unpublished` (
KEY `idx_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module unpublished info';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('ModuleUnpublished', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
},

View File

@@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS `npm_module_maintainer` (
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='npm original module maintainers';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('NpmModuleMaintainer', {
@@ -34,7 +35,7 @@ module.exports = function (sequelize, DataTypes) {
comment: 'user name'
},
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
}

View File

@@ -7,22 +7,23 @@ CREATE TABLE IF NOT EXISTS `package_readme` (
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`readme` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT 'the latest version readme',
`version` varchar(30) NOT NULL COMMENT 'module version',
`version` varchar(70) NOT NULL COMMENT 'module version',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`),
KEY `idx_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='package latest readme';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('PackageReadme', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name'
},
version: {
type: DataTypes.STRING(30),
type: DataTypes.STRING(config.versionLen),
allowNull: false,
comment: 'module latest version'
},

View File

@@ -20,29 +20,30 @@ CREATE TABLE IF NOT EXISTS `tag` (
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'module name',
`tag` varchar(30) NOT NULL COMMENT 'tag name',
`version` varchar(30) NOT NULL COMMENT 'module version',
`tag` varchar(70) NOT NULL COMMENT 'tag name',
`version` varchar(70) NOT NULL COMMENT 'module version',
`module_id` bigint(20) unsigned NOT NULL COMMENT 'module id',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`, `tag`),
KEY `idx_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='module tag';
*/
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('Tag', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
allowNull: false,
comment: 'module name',
},
tag: {
type: DataTypes.STRING(30),
type: DataTypes.STRING(config.tagLen),
allowNull: false,
comment: 'tag name',
},
version: {
type: DataTypes.STRING(30),
type: DataTypes.STRING(config.versionLen),
allowNull: false,
comment: 'module version',
},

View File

@@ -31,11 +31,12 @@
// -- init `total` count
// INSERT INTO total(name, gmt_modified) VALUES('total', now())
// ON DUPLICATE KEY UPDATE gmt_modified=now();
const config = require('../config');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('Total', {
name: {
type: DataTypes.STRING(214),
type: DataTypes.STRING(config.nameLen),
primaryKey: true,
comment: 'total name'
},

View File

@@ -1,5 +1,6 @@
'use strict';
var os = require('os');
var semver = require('semver');
var models = require('../models');
var common = require('./common');
@@ -542,6 +543,23 @@ exports.removeModulesByNameAndVersions = function* (name, versions) {
// tags
exports.addModuleTag = function* (name, tag, version) {
if (name.length > config.nameLen
|| tag.length > config.tagLen
|| version.length > config.versionLen) {
let errorMsg = 'addModuleTag Error:';
if (name.length > config.nameLen) {
errorMsg = `${errorMsg} length of name(${name}) > ${config.nameLen}${os.EOL}`;
}
if (tag.length > config.tagLen) {
errorMsg = `${errorMsg} length of tag(${tag}) > ${config.tagLen}${os.EOL}`;
}
if (version.length > config.versionLen) {
errorMsg = `${errorMsg} length of version(${version}) > ${config.versionLen}${os.EOL}`;
}
console.info('errorMsg: ', errorMsg);
throw new Error(errorMsg);
}
var mod = yield exports.getModule(name, version);
if (!mod) {
return null;

View File

@@ -96,6 +96,20 @@ describe('test/controllers/registry/package/dist_tag.test.js', function () {
.expect(400, done);
});
it.only('should 400 when set a version of 70+ length', function (done) {
const tag = Buffer.alloc(71, 'a').toString();
request(app)
.put(`/-/package/@cnpmtest/dist_tag_test_module_set/dist-tags/${tag}`)
.set('authorization', utils.otherUserAuth)
.set('content-type', 'application/json')
.send(JSON.stringify(`1.0.1`))
.expect({
error: '[version_error] @cnpmtest/dist_tag_test_module_set@1.0.1 not exists',
reason: '[version_error] @cnpmtest/dist_tag_test_module_set@1.0.1 not exists',
})
.expect(400, done);
});
it('should 201 set exists tag', function (done) {
request(app)
.put('/-/package/@cnpmtest/dist_tag_test_module_set/dist-tags/exists')

View File

@@ -5,6 +5,8 @@ var sleep = require('co-sleep');
var Package = require('../../services/package');
var utils = require('../utils');
var common = require('../../services/common');
const config = require('../../config');
const os = require('os');
describe('test/services/package.test.js', function () {
describe('addModuleTag()', function () {
@@ -27,6 +29,15 @@ describe('test/services/package.test.js', function () {
var tag = yield Package.addModuleTag('not-exists', 'latest', '1.0.0');
should.not.exist(tag);
});
it('should return Error when tag length out of range', function* () {
const r = yield utils.createModule('test-addModuleTag-module-name', '1.0.0');
try {
yield Package.addModuleTag(r.name, Buffer.alloc(config.tagLen + 1, 'a'), r.version);
} catch (error) {
error.message.should.equal(`addModuleTag Error: length of tag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 70${os.EOL}`);
}
});
});
describe('getModuleByTag()', function () {