@yeyelailou/co 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -5
- package/bin//345/205/245/345/217/243.js +48 -0
- package/index.js +30 -2
- package/package.json +17 -6
- package//345/267/245/345/205/267//345/217/221/345/270/203/co-language.json +34 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/identify.js +74 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/linguist/languages.yml +40 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/lint.js +18 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/parse.js +9 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/server.js +117 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//345/271/263/345/217/260/345/214/205/linux-arm64/package.json +29 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//345/271/263/345/217/260/345/214/205/linux-x64/package.json +28 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//346/236/204/345/273/272/345/271/263/345/217/260/345/214/205.js +183 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//350/207/252/345/212/250/345/217/221/345/270/203.js +120 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//350/257/206/345/210/253/346/265/213/350/257/225.js +30 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//350/257/255/350/250/200/350/257/206/345/210/253.js +11 -0
- package//345/267/245/345/205/267//347/224/237/346/200/201/cli.js +142 -44
- package//345/267/245/345/205/267//347/224/237/346/200/201//345/214/205/345/256/211/345/205/250.js +156 -0
- package//345/267/245/345/205/267//347/224/237/346/200/201//346/263/250/345/206/214/350/241/250/346/234/215/345/212/241.js +82 -15
- package//345/267/245/345/205/267//347/224/237/346/200/201//350/247/204/350/214/203.js +20 -3
- package//345/267/245/345/205/267//347/224/237/346/200/201//351/252/214/350/257/201/344/270/211/351/200/232/351/201/223.js +9 -0
- package//346/265/213/350/257/225//345/267/245/345/205/267/351/223/276/345/256/211/345/205/250/346/265/213/350/257/225.js +214 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/binary.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/bubble.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/calc.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/factorial.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/fib.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/hanoi.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/multable.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/prime.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/reverse.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/wordcount.elf +0 -0
package//345/267/245/345/205/267//347/224/237/346/200/201//345/214/205/345/256/211/345/205/250.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 生态包的路径与压缩包安全边界。
|
|
4
|
+
*
|
|
5
|
+
* 注册表和 CLI 都把包名/版本用于拼文件路径,而 tar 归档来自网络。
|
|
6
|
+
* 这里集中校验,避免某处漏掉后出现目录穿越、越权删除或解压写出目标目录。
|
|
7
|
+
*/
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const crypto = require('crypto');
|
|
11
|
+
const { execFileSync } = require('child_process');
|
|
12
|
+
|
|
13
|
+
const 包名正则 = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
14
|
+
const 版本正则 = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
15
|
+
const 最大包体积 = 128 * 1024 * 1024;
|
|
16
|
+
const 最大成员数 = 10000;
|
|
17
|
+
|
|
18
|
+
function 校验包名(名) {
|
|
19
|
+
if (typeof 名 !== 'string' || !包名正则.test(名)) {
|
|
20
|
+
const 显示 = typeof 名 === 'string' ? JSON.stringify(名) : String(名);
|
|
21
|
+
throw new Error('非法包名(须为 1-64 位字母/数字/_/-,且以字母数字开头):' + 显示);
|
|
22
|
+
}
|
|
23
|
+
return 名;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function 校验版本(版本) {
|
|
27
|
+
if (typeof 版本 !== 'string' || 版本.length > 64 || !版本正则.test(版本)) {
|
|
28
|
+
throw new Error('非法版本号(须为 semver,如 1.2.3):' + JSON.stringify(版本));
|
|
29
|
+
}
|
|
30
|
+
return 版本;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 返回 根/包名,并保证结果仍是根的直接子目录。 */
|
|
34
|
+
function 包目录(根, 名) {
|
|
35
|
+
校验包名(名);
|
|
36
|
+
const 根绝对 = path.resolve(根);
|
|
37
|
+
const 目标 = path.resolve(根绝对, 名);
|
|
38
|
+
if (path.dirname(目标) !== 根绝对) throw new Error('包名越过缓存目录:' + 名);
|
|
39
|
+
return 目标;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 校验 tar 成员名:绝对路径、盘符、.. 段和控制字符一律拒绝。 */
|
|
43
|
+
function 校验成员名(名) {
|
|
44
|
+
if (typeof 名 !== 'string' || !名 || /[\x00-\x1f\x7f]/.test(名)) {
|
|
45
|
+
throw new Error('压缩包含非法文件名');
|
|
46
|
+
}
|
|
47
|
+
// 反斜杠在 Unix 上不是分隔符,但在 Windows/人工审计中容易产生歧义;
|
|
48
|
+
// tar 的转义输出(如 \n)也统一拒绝,防止控制字符藏住后续 .. 路径段。
|
|
49
|
+
if (名.includes('\\')) {
|
|
50
|
+
throw new Error('压缩包含反斜杠或转义文件名:' + JSON.stringify(名));
|
|
51
|
+
}
|
|
52
|
+
const 统一 = 名.replace(/\\/g, '/');
|
|
53
|
+
if (统一.startsWith('/') || /^[A-Za-z]:/.test(统一)) {
|
|
54
|
+
throw new Error('压缩包含绝对路径:' + 名);
|
|
55
|
+
}
|
|
56
|
+
const 段 = 统一.split('/');
|
|
57
|
+
for (const 项 of 段) {
|
|
58
|
+
if (项 === '..') throw new Error('压缩包含目录穿越成员:' + 名);
|
|
59
|
+
}
|
|
60
|
+
return 统一;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function tar环境() {
|
|
64
|
+
// C locale 下 GNU tar 默认把非 ASCII 文件名转义为八进制;
|
|
65
|
+
// 用 literal quoting 保留中文原始名称,避免误伤正常的中文 .co 文件。
|
|
66
|
+
return Object.assign({}, process.env, { LC_ALL: 'C', TAR_OPTIONS: '' });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const tar名称参数 = ['--quoting-style=literal'];
|
|
70
|
+
|
|
71
|
+
function 列出Tar(文件, 详细) {
|
|
72
|
+
const 标志 = 详细 ? '-tvf' : '-tf';
|
|
73
|
+
const 选项 = {
|
|
74
|
+
encoding: 'utf8',
|
|
75
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
76
|
+
timeout: 30000,
|
|
77
|
+
env: tar环境(),
|
|
78
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
79
|
+
};
|
|
80
|
+
try {
|
|
81
|
+
return execFileSync('tar', [...tar名称参数, 标志, 文件], 选项);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
const 输出 = String(e.stderr || e.message || '');
|
|
84
|
+
// BSD tar / toybox tar 可能没有 GNU 的 quoting-style;退回普通列表,
|
|
85
|
+
// 真正损坏的归档仍会在第二次执行或解压时失败。
|
|
86
|
+
if (!/quoting-style|unrecognized option|unknown option|illegal option/i.test(输出)) throw e;
|
|
87
|
+
return execFileSync('tar', [标志, 文件], 选项);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 在解压前检查归档。`-tvf` 的类型列只允许普通文件(-)和目录(d),
|
|
93
|
+
* 因此符号链接、硬链接、设备和 FIFO 都不会进入缓存目录。
|
|
94
|
+
*/
|
|
95
|
+
function 校验Tar归档(文件) {
|
|
96
|
+
const st = fs.lstatSync(文件);
|
|
97
|
+
if (!st.isFile()) throw new Error('压缩包不是普通文件');
|
|
98
|
+
if (st.size > 最大包体积) throw new Error('压缩包超过 128MB 上限');
|
|
99
|
+
|
|
100
|
+
const 名称输出 = 列出Tar(文件, false);
|
|
101
|
+
const 名称 = 名称输出.split('\n').filter((x) => x !== '');
|
|
102
|
+
if (名称.length > 最大成员数) throw new Error('压缩包成员超过 10000 个上限');
|
|
103
|
+
for (const 名 of 名称) 校验成员名(名);
|
|
104
|
+
|
|
105
|
+
const 详细输出 = 列出Tar(文件, true);
|
|
106
|
+
const 行 = 详细输出.split('\n').filter((x) => x.trim() !== '');
|
|
107
|
+
for (const 行文本 of 行) {
|
|
108
|
+
const 类型 = 行文本[0];
|
|
109
|
+
if (类型 !== '-' && 类型 !== 'd') {
|
|
110
|
+
throw new Error('压缩包含非普通文件成员(类型 ' + 类型 + '),已拒绝解压');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** 校验并解压到一个新建/已清空的目标目录;调用方负责目标目录本身的安全。 */
|
|
117
|
+
function 安全解压Tar(文件, 目标目录) {
|
|
118
|
+
校验Tar归档(文件);
|
|
119
|
+
fs.mkdirSync(目标目录, { recursive: true });
|
|
120
|
+
try {
|
|
121
|
+
execFileSync('tar', ['xzf', 文件, '-C', 目标目录, '--no-same-owner'], {
|
|
122
|
+
timeout: 120000,
|
|
123
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
124
|
+
env: tar环境(),
|
|
125
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
126
|
+
});
|
|
127
|
+
} catch (e) {
|
|
128
|
+
// 兼容不支持 --no-same-owner 的 tar:成员类型与路径已在上面校验,
|
|
129
|
+
// 退回默认属主行为不会重新引入路径穿越。
|
|
130
|
+
if (!/no-same-owner|unrecognized option|unknown option/i.test(String(e.stderr || e.message))) throw e;
|
|
131
|
+
execFileSync('tar', ['xzf', 文件, '-C', 目标目录], {
|
|
132
|
+
timeout: 120000,
|
|
133
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
134
|
+
env: tar环境(),
|
|
135
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function 文件Sha256(文件) {
|
|
142
|
+
return crypto.createHash('sha256').update(fs.readFileSync(文件)).digest('hex');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
module.exports = {
|
|
146
|
+
包名正则,
|
|
147
|
+
版本正则,
|
|
148
|
+
校验包名,
|
|
149
|
+
校验版本,
|
|
150
|
+
包目录,
|
|
151
|
+
校验成员名,
|
|
152
|
+
校验Tar归档,
|
|
153
|
+
安全解压Tar,
|
|
154
|
+
文件Sha256,
|
|
155
|
+
最大包体积
|
|
156
|
+
};
|
|
@@ -13,26 +13,37 @@ const fs = require('fs');
|
|
|
13
13
|
const path = require('path');
|
|
14
14
|
const os = require('os');
|
|
15
15
|
const crypto = require('crypto');
|
|
16
|
+
const 包安全 = require('./包安全.js');
|
|
16
17
|
|
|
17
|
-
const 存储根 = path.join(os.tmpdir(), 'cocn-registry');
|
|
18
|
+
const 存储根 = path.resolve(process.env.COCR_STORAGE || path.join(os.tmpdir(), 'cocn-registry'));
|
|
18
19
|
fs.mkdirSync(存储根, { recursive: true });
|
|
19
20
|
const 端口 = process.env.COCR_PORT ? parseInt(process.env.COCR_PORT, 10) : 7878;
|
|
21
|
+
// 默认只监听本机;确需内网共享时显式设置 COCR_HOST=0.0.0.0。
|
|
22
|
+
// 注册表目前没有发布鉴权,直接暴露到公网等于允许任何人覆盖/上传包。
|
|
23
|
+
const 监听地址 = process.env.COCR_HOST || '127.0.0.1';
|
|
24
|
+
const 最大请求体 = 128 * 1024 * 1024;
|
|
20
25
|
|
|
21
|
-
function 包目录(名) { return
|
|
26
|
+
function 包目录(名) { return 包安全.包目录(存储根, 名); }
|
|
22
27
|
function 索引文件(名) { return path.join(包目录(名), 'index.json'); }
|
|
23
|
-
function tarball文件(名, 版本) {
|
|
28
|
+
function tarball文件(名, 版本) {
|
|
29
|
+
包安全.校验版本(版本);
|
|
30
|
+
return path.join(包目录(名), 版本 + '.tar.gz');
|
|
31
|
+
}
|
|
24
32
|
|
|
25
33
|
function 读索引(名) {
|
|
26
34
|
try { return JSON.parse(fs.readFileSync(索引文件(名), 'utf8')); } catch { return null; }
|
|
27
35
|
}
|
|
28
36
|
function 写索引(名, 数据) {
|
|
29
|
-
|
|
30
|
-
fs.
|
|
37
|
+
const 文件 = 索引文件(名);
|
|
38
|
+
fs.mkdirSync(path.dirname(文件), { recursive: true });
|
|
39
|
+
const 临时 = 文件 + '.tmp-' + process.pid;
|
|
40
|
+
fs.writeFileSync(临时, JSON.stringify(数据, null, 2));
|
|
41
|
+
fs.renameSync(临时, 文件);
|
|
31
42
|
}
|
|
32
43
|
function 列出所有() {
|
|
33
44
|
if (!fs.existsSync(存储根)) return [];
|
|
34
45
|
return fs.readdirSync(存储根)
|
|
35
|
-
.filter(n => fs.existsSync(索引文件(n)))
|
|
46
|
+
.filter(n => 包安全.包名正则.test(n) && fs.existsSync(索引文件(n)))
|
|
36
47
|
.map(n => 读索引(n))
|
|
37
48
|
.filter(Boolean)
|
|
38
49
|
.sort((a, b) => (b.最新发布时间 || '').localeCompare(a.最新发布时间 || ''));
|
|
@@ -43,7 +54,26 @@ function 发送(res, 码, obj) {
|
|
|
43
54
|
res.end(JSON.stringify(obj));
|
|
44
55
|
}
|
|
45
56
|
function 解析Body(req) {
|
|
46
|
-
return new Promise((resolve
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
const 块 = [];
|
|
59
|
+
let 大小 = 0;
|
|
60
|
+
let 结束 = false;
|
|
61
|
+
req.on('data', c => {
|
|
62
|
+
if (结束) return;
|
|
63
|
+
大小 += c.length;
|
|
64
|
+
if (大小 > 最大请求体) {
|
|
65
|
+
结束 = true;
|
|
66
|
+
const error = new Error('请求体超过 128MB 上限');
|
|
67
|
+
error.statusCode = 413;
|
|
68
|
+
reject(error);
|
|
69
|
+
req.resume();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
块.push(c);
|
|
73
|
+
});
|
|
74
|
+
req.on('end', () => { if (!结束) resolve(Buffer.concat(块).toString('utf8')); });
|
|
75
|
+
req.on('error', e => { if (!结束) { 结束 = true; reject(e); } });
|
|
76
|
+
});
|
|
47
77
|
}
|
|
48
78
|
function 转义(s) {
|
|
49
79
|
return String(s == null ? '' : s)
|
|
@@ -51,6 +81,14 @@ function 转义(s) {
|
|
|
51
81
|
.replace(/"/g, '"').replace(/'/g, ''');
|
|
52
82
|
}
|
|
53
83
|
function 转义文本(s) { return 转义(s).replace(/\n/g, '<br>'); }
|
|
84
|
+
/** repository 是用户可发布字段:只允许 http(s) 外链,阻止 javascript: 等协议。 */
|
|
85
|
+
function 安全外链(s) {
|
|
86
|
+
try {
|
|
87
|
+
const u = new URL(String(s || ''));
|
|
88
|
+
if (u.protocol !== 'https:' && u.protocol !== 'http:') return '';
|
|
89
|
+
return 转义(u.href);
|
|
90
|
+
} catch { return ''; }
|
|
91
|
+
}
|
|
54
92
|
|
|
55
93
|
const server = http.createServer(async (req, res) => {
|
|
56
94
|
const u = new URL(req.url, 'http://localhost');
|
|
@@ -91,7 +129,7 @@ const server = http.createServer(async (req, res) => {
|
|
|
91
129
|
+ '<h1>' + 转义(c.显示名 || c.name) + ' <span class="muted">' + 转义(c.name) + '</span></h1>'
|
|
92
130
|
+ '<p>' + 转义文本(c.description || '') + '</p>'
|
|
93
131
|
+ '<p class="muted">版本 ' + 转义(c.最新版本) + ' · license ' + 转义(c.license || '—')
|
|
94
|
-
+ (c.repository ? ' · <a href="' +
|
|
132
|
+
+ (安全外链(c.repository) ? ' · <a href="' + 安全外链(c.repository) + '" rel="noopener noreferrer">仓库</a>' : '')
|
|
95
133
|
+ (c.authors ? ' · 作者 ' + 转义(c.authors) : '') + '</p>';
|
|
96
134
|
if (c.keywords && c.keywords.length) html += '<p class="muted">关键词:' + c.keywords.map(t => '<code>' + 转义(t) + '</code>').join(' ') + '</p>';
|
|
97
135
|
if (c.categories && c.categories.length) html += '<p class="muted">分类:' + c.categories.map(t => '<code>' + 转义(t) + '</code>').join(' ') + '</p>';
|
|
@@ -140,19 +178,41 @@ const server = http.createServer(async (req, res) => {
|
|
|
140
178
|
let pkg; try { pkg = JSON.parse(body); } catch { return 发送(res, 400, { error: 'JSON 无效' }); }
|
|
141
179
|
const { name, version, tarball_base64 } = pkg;
|
|
142
180
|
if (!name || !version || !tarball_base64) return 发送(res, 400, { error: '缺少 name/version/tarball_base64' });
|
|
143
|
-
|
|
181
|
+
包安全.校验包名(name);
|
|
182
|
+
包安全.校验版本(version);
|
|
183
|
+
if (typeof tarball_base64 !== 'string' || tarball_base64.length > 最大请求体 * 2) {
|
|
184
|
+
return 发送(res, 400, { error: 'tarball_base64 无效或超过大小上限' });
|
|
185
|
+
}
|
|
144
186
|
const buf = Buffer.from(tarball_base64, 'base64');
|
|
187
|
+
if (!buf.length || buf.length > 包安全.最大包体积) {
|
|
188
|
+
return 发送(res, 400, { error: '压缩包为空或超过 128MB 上限' });
|
|
189
|
+
}
|
|
145
190
|
const sha = crypto.createHash('sha256').update(buf).digest('hex');
|
|
146
|
-
|
|
147
|
-
|
|
191
|
+
if (pkg.checksum && pkg.checksum !== sha) return 发送(res, 400, { error: 'checksum 与实际压缩包不一致' });
|
|
192
|
+
// 版本不可变:必须在写 tarball 之前检查,否则重复发布的 409 会先覆盖原包。
|
|
193
|
+
const 已有索引 = 读索引(name);
|
|
194
|
+
if (已有索引 && (已有索引.versions || []).some(v => v.version === version)) {
|
|
195
|
+
return 发送(res, 409, { error: '版本已存在' });
|
|
196
|
+
}
|
|
197
|
+
const 目录 = 包目录(name);
|
|
198
|
+
const 目标包 = tarball文件(name, version);
|
|
199
|
+
const 临时包 = 目标包 + '.tmp-' + process.pid;
|
|
200
|
+
fs.mkdirSync(目录, { recursive: true });
|
|
201
|
+
fs.writeFileSync(临时包, buf);
|
|
202
|
+
try {
|
|
203
|
+
包安全.校验Tar归档(临时包);
|
|
204
|
+
fs.renameSync(临时包, 目标包);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
try { fs.unlinkSync(临时包); } catch {}
|
|
207
|
+
return 发送(res, 400, { error: '压缩包不安全或无效:' + e.message });
|
|
208
|
+
}
|
|
148
209
|
const 现在 = new Date().toISOString();
|
|
149
|
-
let i =
|
|
210
|
+
let i = 已有索引 || {
|
|
150
211
|
name, 显示名: pkg.显示名 || name, description: pkg.description || '',
|
|
151
212
|
keywords: pkg.keywords || [], categories: pkg.categories || [],
|
|
152
213
|
license: pkg.license || '', repository: pkg.repository || '',
|
|
153
214
|
authors: pkg.authors || '', versions: []
|
|
154
215
|
};
|
|
155
|
-
if (i.versions.find(v => v.version === version)) return 发送(res, 409, { error: '版本已存在' });
|
|
156
216
|
i.显示名 = pkg.显示名 || i.显示名;
|
|
157
217
|
i.description = pkg.description || i.description;
|
|
158
218
|
i.keywords = pkg.keywords || i.keywords;
|
|
@@ -173,6 +233,7 @@ const server = http.createServer(async (req, res) => {
|
|
|
173
233
|
if (req.method === 'POST' && m) {
|
|
174
234
|
const i = 读索引(decodeURIComponent(m[1]));
|
|
175
235
|
const v = decodeURIComponent(m[2]);
|
|
236
|
+
包安全.校验版本(v);
|
|
176
237
|
if (!i) return 发送(res, 404, { error: '未找到包' });
|
|
177
238
|
const 目标 = i.versions.find(x => x.version === v);
|
|
178
239
|
if (!目标) return 发送(res, 404, { error: '未找到版本 ' + v });
|
|
@@ -183,7 +244,9 @@ const server = http.createServer(async (req, res) => {
|
|
|
183
244
|
}
|
|
184
245
|
return 发送(res, 404, { error: '未找到路由' });
|
|
185
246
|
} catch (e) {
|
|
186
|
-
|
|
247
|
+
const 是输入错误 = /^非法(包名|版本)|URI malformed|请求体超过/.test(e.message || '');
|
|
248
|
+
const 状态 = e.statusCode || (是输入错误 ? 400 : 500);
|
|
249
|
+
return 发送(res, 状态, { error: e.message });
|
|
187
250
|
}
|
|
188
251
|
});
|
|
189
252
|
|
|
@@ -197,4 +260,8 @@ function 转义Text块(text) {
|
|
|
197
260
|
.replace(/\n/g, '<br>');
|
|
198
261
|
}
|
|
199
262
|
|
|
200
|
-
|
|
263
|
+
if (require.main === module) {
|
|
264
|
+
server.listen(端口, 监听地址, () => console.log('[coCN 注册表] 监听 http://' + 监听地址 + ':' + 端口 + ' (存储: ' + 存储根 + ')'));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
module.exports = { server, 存储根, 包目录, tarball文件 };
|
|
@@ -32,21 +32,29 @@ function 解析值(v) {
|
|
|
32
32
|
return v.replace(/^["']|["']$/g, '');
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
/** 解析 TOML
|
|
35
|
+
/** 解析 TOML 文本为对象。
|
|
36
|
+
* 所有表使用无原型对象:不可信清单里的 __proto__ / constructor 等键
|
|
37
|
+
* 不会改写 Object.prototype(原型污染)。 */
|
|
36
38
|
function 解析TOML(文本) {
|
|
37
|
-
const 根 =
|
|
39
|
+
const 根 = Object.create(null);
|
|
38
40
|
let 当前 = 根;
|
|
39
41
|
for (const raw of String(文本).split(/\r?\n/)) {
|
|
40
42
|
const line = raw.trim();
|
|
41
43
|
if (!line || line.startsWith('#')) continue;
|
|
42
44
|
if (line.startsWith('[') && line.includes(']')) {
|
|
43
45
|
const sec = line.slice(1, line.lastIndexOf(']')).trim();
|
|
44
|
-
当前 = sec.split('.').reduce((o, k) =>
|
|
46
|
+
当前 = sec.split('.').reduce((o, k) => {
|
|
47
|
+
if (!Object.prototype.hasOwnProperty.call(o, k) || !o[k] || typeof o[k] !== 'object') {
|
|
48
|
+
o[k] = Object.create(null);
|
|
49
|
+
}
|
|
50
|
+
return o[k];
|
|
51
|
+
}, 根);
|
|
45
52
|
continue;
|
|
46
53
|
}
|
|
47
54
|
const i = line.indexOf('=');
|
|
48
55
|
if (i < 0) continue;
|
|
49
56
|
const key = line.slice(0, i).trim().replace(/^["']|["']$/g, '');
|
|
57
|
+
if (!key) continue;
|
|
50
58
|
当前[key] = 解析值(line.slice(i + 1));
|
|
51
59
|
}
|
|
52
60
|
return 根;
|
|
@@ -65,6 +73,15 @@ function 校验(c) {
|
|
|
65
73
|
}
|
|
66
74
|
if (c.dependencies != null && typeof c.dependencies !== 'object') {
|
|
67
75
|
错误.push('dependencies 须为 [dependencies] 表');
|
|
76
|
+
} else if (c.dependencies) {
|
|
77
|
+
for (const 依赖名 of Object.keys(c.dependencies)) {
|
|
78
|
+
if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(依赖名)) {
|
|
79
|
+
错误.push('依赖包名非法:' + JSON.stringify(依赖名));
|
|
80
|
+
}
|
|
81
|
+
if (typeof c.dependencies[依赖名] !== 'string') {
|
|
82
|
+
错误.push('依赖 ' + 依赖名 + ' 的版本范围须为字符串');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
68
85
|
}
|
|
69
86
|
if (错误.length) throw new Error('co.toml 校验失败:' + 错误.join(';'));
|
|
70
87
|
return c;
|
|
@@ -39,11 +39,20 @@ function findBootstrap() {
|
|
|
39
39
|
目录 = path.dirname(目录);
|
|
40
40
|
if (目录 === path.dirname(目录)) break;
|
|
41
41
|
}
|
|
42
|
+
// 全新克隆未运行 install.js 时的仓库内兜底,与 CLI/API 保持一致。
|
|
43
|
+
const 仓库内 = path.join(ROOT, '工具', '发布', '平台包', suffix, 'bin', '引导器');
|
|
44
|
+
if (fs.existsSync(仓库内)) return 仓库内;
|
|
45
|
+
const 仓库内Exe = 仓库内 + '.exe';
|
|
46
|
+
if (fs.existsSync(仓库内Exe)) return 仓库内Exe;
|
|
42
47
|
}
|
|
43
48
|
return mainBoot;
|
|
44
49
|
}
|
|
45
50
|
|
|
46
51
|
const BOOT = process.env.COCN_BOOTSTRAP || findBootstrap();
|
|
52
|
+
if (!fs.existsSync(BOOT)) {
|
|
53
|
+
console.error('找不到引导器:' + BOOT + '\n可运行 node install.js,或设置 COCN_BOOTSTRAP 指向本机架构引导器。');
|
|
54
|
+
process.exit(2);
|
|
55
|
+
}
|
|
47
56
|
// 单通道超时:默认 60s;慢速宿主(如 Termux/aarch64 自举解释器)可调大,
|
|
48
57
|
// 例如 COCN_VERIFY_TIMEOUT=180000 node 工具/生态/验证三通道.js
|
|
49
58
|
const 通道超时 = process.env.COCN_VERIFY_TIMEOUT
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 工具链安全回归:包名/版本路径、tar 解压、注册表不可变发布、HTTP API 边界。
|
|
4
|
+
* 只在系统临时目录创建数据,不访问真实 ~/.cocn,也不需要外部网络。
|
|
5
|
+
*/
|
|
6
|
+
const assert = require('assert');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const os = require('os');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const http = require('http');
|
|
11
|
+
const { execFileSync } = require('child_process');
|
|
12
|
+
|
|
13
|
+
const 临时 = fs.mkdtempSync(path.join(os.tmpdir(), 'co-security-test-'));
|
|
14
|
+
const 旧HOME = process.env.HOME;
|
|
15
|
+
const 旧注册表 = process.env.COCN_REGISTRY;
|
|
16
|
+
process.env.HOME = path.join(临时, 'home');
|
|
17
|
+
process.env.COCR_STORAGE = path.join(临时, 'registry');
|
|
18
|
+
fs.mkdirSync(process.env.HOME, { recursive: true });
|
|
19
|
+
|
|
20
|
+
const 包安全 = require('../工具/生态/包安全.js');
|
|
21
|
+
const cli = require('../工具/生态/cli.js');
|
|
22
|
+
|
|
23
|
+
function 请求(端口, 方法, 路径, 对象) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
const data = 对象 === undefined ? null : Buffer.from(JSON.stringify(对象));
|
|
26
|
+
const req = http.request({
|
|
27
|
+
host: '127.0.0.1', port: 端口, method: 方法, path: 路径,
|
|
28
|
+
headers: data ? { 'Content-Type': 'application/json', 'Content-Length': data.length } : {}
|
|
29
|
+
}, (res) => {
|
|
30
|
+
const chunks = [];
|
|
31
|
+
res.on('data', c => chunks.push(c));
|
|
32
|
+
res.on('end', () => {
|
|
33
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
34
|
+
let body = null;
|
|
35
|
+
try { body = text ? JSON.parse(text) : null; } catch {}
|
|
36
|
+
resolve({ status: res.statusCode, body, text });
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
req.on('error', reject);
|
|
40
|
+
if (data) req.write(data);
|
|
41
|
+
req.end();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function 监听(server) {
|
|
46
|
+
return new Promise(resolve => server.listen(0, '127.0.0.1', () => resolve(server.address().port)));
|
|
47
|
+
}
|
|
48
|
+
function 关闭(server) {
|
|
49
|
+
return new Promise(resolve => server.close(resolve));
|
|
50
|
+
}
|
|
51
|
+
function 制包(目录, 名称) {
|
|
52
|
+
fs.mkdirSync(目录, { recursive: true });
|
|
53
|
+
fs.writeFileSync(path.join(目录, 'co.toml'), 'name = "' + 名称 + '"\nversion = "1.0.0"\nlib = "库.co"\n');
|
|
54
|
+
fs.writeFileSync(path.join(目录, '库.co'), '函数 问候()\n 返回 "安全"\n结束\n');
|
|
55
|
+
const 包 = path.join(临时, 名称 + '.tar.gz');
|
|
56
|
+
execFileSync('tar', ['czf', 包, '-C', 目录, '.']);
|
|
57
|
+
return 包;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function 主() {
|
|
61
|
+
// 1) 包名/版本不能跳出目录
|
|
62
|
+
for (const 坏名 of ['../../victim', 'a/b', '..', '.', '', 'a\\b', '-bad']) {
|
|
63
|
+
assert.throws(() => 包安全.校验包名(坏名), /非法包名/, '应拒绝包名 ' + JSON.stringify(坏名));
|
|
64
|
+
}
|
|
65
|
+
for (const 坏版本 of ['../../x', '1.0.0/../../x', '1.0', 'v1.0.0', '1.0.0\n']) {
|
|
66
|
+
assert.throws(() => 包安全.校验版本(坏版本), /非法版本号/, '应拒绝版本 ' + JSON.stringify(坏版本));
|
|
67
|
+
}
|
|
68
|
+
const 缓存 = path.join(临时, 'cache');
|
|
69
|
+
assert.strictEqual(包安全.包目录(缓存, 'abc_1'), path.resolve(缓存, 'abc_1'));
|
|
70
|
+
|
|
71
|
+
// 2) semver caret 的 0.x 边界
|
|
72
|
+
assert.strictEqual(cli.满足('^0.0.9', '0.0.9'), true);
|
|
73
|
+
assert.strictEqual(cli.满足('^0.0.9', '0.0.10'), false);
|
|
74
|
+
assert.strictEqual(cli.满足('^0.0.9', '0.1.0'), false);
|
|
75
|
+
assert.strictEqual(cli.满足('^0.2.3', '0.2.9'), true);
|
|
76
|
+
assert.strictEqual(cli.满足('^0.2.3', '0.3.0'), false);
|
|
77
|
+
assert.strictEqual(cli.满足('^1.2.3', '1.9.0'), true);
|
|
78
|
+
assert.strictEqual(cli.满足('^1.2.3', '2.0.0'), false);
|
|
79
|
+
assert.strictEqual(cli.满足('~1.2.3', '1.2.9'), true);
|
|
80
|
+
assert.strictEqual(cli.满足('~1.2.3', '1.3.0'), false);
|
|
81
|
+
|
|
82
|
+
// 2.1) TOML 清单原型污染与非法依赖名
|
|
83
|
+
const 规范 = require('../工具/生态/规范.js');
|
|
84
|
+
const 清单 = 规范.解析TOML('name = "safe"\nversion = "1.0.0"\n[dependencies]\n__proto__ = "x"\nconstructor = "y"\n');
|
|
85
|
+
assert.strictEqual({}.polluted, undefined);
|
|
86
|
+
assert.strictEqual(Object.prototype.polluted, undefined);
|
|
87
|
+
assert.strictEqual(清单.dependencies.__proto__, 'x');
|
|
88
|
+
assert.strictEqual(Object.getPrototypeOf(清单.dependencies), null);
|
|
89
|
+
assert.throws(() => 规范.校验({ name: 'safe', version: '1.0.0', dependencies: { '../x': '^1.0.0' } }), /依赖包名非法/);
|
|
90
|
+
|
|
91
|
+
// 3) tar 路径与特殊成员
|
|
92
|
+
assert.throws(() => 包安全.校验成员名('../escape.co'), /目录穿越/);
|
|
93
|
+
assert.throws(() => 包安全.校验成员名('/etc/passwd'), /绝对路径/);
|
|
94
|
+
assert.throws(() => 包安全.校验成员名('C:\\Windows\\x'), /反斜杠|绝对路径/);
|
|
95
|
+
const 安全目录 = path.join(临时, 'safe-src');
|
|
96
|
+
const 安全包 = 制包(安全目录, 'safe');
|
|
97
|
+
assert.strictEqual(包安全.校验Tar归档(安全包), true);
|
|
98
|
+
const 解压目标 = path.join(临时, 'safe-out');
|
|
99
|
+
包安全.安全解压Tar(安全包, 解压目标);
|
|
100
|
+
assert.ok(fs.existsSync(path.join(解压目标, '库.co')));
|
|
101
|
+
|
|
102
|
+
const 链接目录 = path.join(临时, 'link-src');
|
|
103
|
+
fs.mkdirSync(链接目录, { recursive: true });
|
|
104
|
+
fs.writeFileSync(path.join(链接目录, '目标.txt'), 'x');
|
|
105
|
+
fs.symlinkSync('目标.txt', path.join(链接目录, '链接'));
|
|
106
|
+
const 链接包 = path.join(临时, 'link.tar.gz');
|
|
107
|
+
execFileSync('tar', ['czf', 链接包, '-C', 链接目录, '.']);
|
|
108
|
+
assert.throws(() => 包安全.校验Tar归档(链接包), /非普通文件/, '符号链接归档必须拒绝');
|
|
109
|
+
|
|
110
|
+
// 4) remove 不得删除缓存外目录
|
|
111
|
+
const 受害者 = path.join(临时, 'victim');
|
|
112
|
+
fs.mkdirSync(受害者, { recursive: true });
|
|
113
|
+
fs.writeFileSync(path.join(受害者, 'keep.txt'), 'keep');
|
|
114
|
+
await assert.rejects(() => cli.运行('remove', ['../../victim']), /非法包名/);
|
|
115
|
+
assert.ok(fs.existsSync(path.join(受害者, 'keep.txt')), '越界删除不得发生');
|
|
116
|
+
|
|
117
|
+
// 5) 注册表:非法名、重复版本不可覆盖、checksum 核验
|
|
118
|
+
const 注册表 = require('../工具/生态/注册表服务.js');
|
|
119
|
+
const 注册端口 = await 监听(注册表.server);
|
|
120
|
+
process.env.COCN_REGISTRY = 'http://127.0.0.1:' + 注册端口;
|
|
121
|
+
// cli.js 的注册表地址在加载时固定;此用例用独立子进程验证完整安装链路。
|
|
122
|
+
const 包 = 制包(安全目录, 'safe');
|
|
123
|
+
const 原始字节 = fs.readFileSync(包);
|
|
124
|
+
const 发布体 = {
|
|
125
|
+
name: 'safe', version: '1.0.0', tarball_base64: 原始字节.toString('base64'),
|
|
126
|
+
checksum: 包安全.文件Sha256(包)
|
|
127
|
+
};
|
|
128
|
+
let r = await 请求(注册端口, 'POST', '/api/v1/crates/new', 发布体);
|
|
129
|
+
assert.strictEqual(r.status, 200, r.text);
|
|
130
|
+
r = await 请求(注册端口, 'POST', '/api/v1/crates/new', {
|
|
131
|
+
name: '../../escape', version: '1.0.0', tarball_base64: 原始字节.toString('base64')
|
|
132
|
+
});
|
|
133
|
+
assert.strictEqual(r.status, 400);
|
|
134
|
+
assert.ok(!fs.existsSync(path.join(临时, 'escape')), '非法包名不得建目录');
|
|
135
|
+
|
|
136
|
+
const 冒名包 = 制包(path.join(临时, 'other-src'), 'other');
|
|
137
|
+
r = await 请求(注册端口, 'POST', '/api/v1/crates/new', {
|
|
138
|
+
name: 'safe', version: '1.0.0', tarball_base64: fs.readFileSync(冒名包).toString('base64')
|
|
139
|
+
});
|
|
140
|
+
assert.strictEqual(r.status, 409);
|
|
141
|
+
const 已存包 = path.join(process.env.COCR_STORAGE, 'safe', '1.0.0.tar.gz');
|
|
142
|
+
assert.ok(原始字节.equals(fs.readFileSync(已存包)), '409 后原 tarball 必须保持不变');
|
|
143
|
+
|
|
144
|
+
// 6) 完整 CLI 安装:错误 checksum 时不得解压;恢复正确值后可安装。
|
|
145
|
+
const 索引文件 = path.join(process.env.COCR_STORAGE, 'safe', 'index.json');
|
|
146
|
+
const 原索引 = JSON.parse(fs.readFileSync(索引文件, 'utf8'));
|
|
147
|
+
const 正确校验和 = 原索引.versions[0].checksum;
|
|
148
|
+
原索引.versions[0].checksum = '0'.repeat(64);
|
|
149
|
+
fs.writeFileSync(索引文件, JSON.stringify(原索引));
|
|
150
|
+
const 消费目录 = path.join(临时, 'consumer');
|
|
151
|
+
fs.mkdirSync(消费目录, { recursive: true });
|
|
152
|
+
const cli路径 = path.join(__dirname, '..', '工具', '生态', 'cli.js');
|
|
153
|
+
const 运行CLI = () => new Promise((resolve, reject) => {
|
|
154
|
+
const child = require('child_process').spawn(process.execPath, ['-e',
|
|
155
|
+
'process.chdir(process.argv[1]); require(process.argv[2]).运行("install", ["safe@1.0.0"]).then(()=>process.exit(0)).catch(e=>{console.error(e.message);process.exit(1)})',
|
|
156
|
+
消费目录, cli路径], {
|
|
157
|
+
env: Object.assign({}, process.env, { HOME: process.env.HOME, COCN_REGISTRY: process.env.COCN_REGISTRY })
|
|
158
|
+
});
|
|
159
|
+
let stdout = '', stderr = '';
|
|
160
|
+
const timer = setTimeout(() => child.kill('SIGTERM'), 60000);
|
|
161
|
+
child.stdout.on('data', c => { stdout += c; });
|
|
162
|
+
child.stderr.on('data', c => { stderr += c; });
|
|
163
|
+
child.on('error', reject);
|
|
164
|
+
child.on('exit', (status, signal) => {
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
resolve({ status, signal, stdout, stderr });
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
let 子 = await 运行CLI();
|
|
170
|
+
assert.notStrictEqual(子.status, 0, '错误 checksum 必须安装失败:' + JSON.stringify({ status: 子.status, stdout: 子.stdout, stderr: 子.stderr, error: 子.error && 子.error.message }));
|
|
171
|
+
assert.match(子.stderr + 子.stdout, /checksum 不匹配/, JSON.stringify(子));
|
|
172
|
+
const 安装目录 = path.join(process.env.HOME, '.cocn', 'registry', 'safe', '1.0.0');
|
|
173
|
+
assert.ok(!fs.existsSync(path.join(安装目录, '库.co')), '校验失败后不得解压出库文件');
|
|
174
|
+
|
|
175
|
+
原索引.versions[0].checksum = 正确校验和;
|
|
176
|
+
fs.writeFileSync(索引文件, JSON.stringify(原索引));
|
|
177
|
+
子 = await 运行CLI();
|
|
178
|
+
assert.strictEqual(子.status, 0, 子.stderr || 子.stdout);
|
|
179
|
+
assert.ok(fs.existsSync(path.join(安装目录, '库.co')));
|
|
180
|
+
assert.ok(fs.existsSync(path.join(process.env.HOME, '.cocn', 'registry', 'safe', '当前', 'safe.co')));
|
|
181
|
+
await 关闭(注册表.server);
|
|
182
|
+
|
|
183
|
+
// 7) 语言 API:不能借 filename 读本地文件;坏 JSON/坏源码为 400。
|
|
184
|
+
const 语言服务 = require('../工具/发布/server.js');
|
|
185
|
+
const 语言端口 = await 监听(语言服务.server);
|
|
186
|
+
const 诱饵 = path.join(临时, 'secret.co');
|
|
187
|
+
fs.writeFileSync(诱饵, '函数 返回 如果 导入 断言相等 测试用例\n');
|
|
188
|
+
r = await 请求(语言端口, 'POST', '/identify', { filename: 诱饵 });
|
|
189
|
+
assert.strictEqual(r.status, 200);
|
|
190
|
+
assert.deepStrictEqual(r.body.keywordHits, [], 'HTTP identify 不得读取服务器文件');
|
|
191
|
+
assert.strictEqual(r.body.matched, 'extension');
|
|
192
|
+
r = await 请求(语言端口, 'POST', '/identify', { source: '函数 返回 如果' });
|
|
193
|
+
assert.strictEqual(r.body.matched, 'keywords');
|
|
194
|
+
r = await 请求(语言端口, 'POST', '/parse', { source: '输出("未闭合字符串' });
|
|
195
|
+
assert.strictEqual(r.status, 400, r.text);
|
|
196
|
+
r = await 请求(语言端口, 'POST', '/lint', { source: 123 });
|
|
197
|
+
assert.strictEqual(r.status, 400);
|
|
198
|
+
r = await 请求(语言端口, 'POST', '/lint', { source: 'x'.repeat(2100000) });
|
|
199
|
+
assert.strictEqual(r.status, 413, '超限请求应收到明确 413,而不是连接重置:' + r.text);
|
|
200
|
+
await 关闭(语言服务.server);
|
|
201
|
+
|
|
202
|
+
console.log('工具链安全测试通过:路径边界 / semver / tar / 发布不可变 / checksum / HTTP 输入');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
主().catch((e) => {
|
|
206
|
+
console.error(e && e.stack || e);
|
|
207
|
+
process.exitCode = 1;
|
|
208
|
+
}).finally(() => {
|
|
209
|
+
try { fs.rmSync(临时, { recursive: true, force: true }); } catch {}
|
|
210
|
+
if (旧HOME === undefined) delete process.env.HOME; else process.env.HOME = 旧HOME;
|
|
211
|
+
if (旧注册表 === undefined) delete process.env.COCN_REGISTRY; else process.env.COCN_REGISTRY = 旧注册表;
|
|
212
|
+
// 断言失败时 HTTP server 可能仍持有事件循环;测试结束应显式退出。
|
|
213
|
+
process.exit(process.exitCode || 0);
|
|
214
|
+
});
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|