mock-service-cli 3.6.0 → 3.6.2

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.
@@ -48,11 +48,19 @@ function init() {
48
48
  let dirPath = req.query.path || '/';
49
49
  dirPath = decodeURIComponent(dirPath);
50
50
 
51
- const fullPath = path.resolve(explorerRoot, '.' + dirPath);
51
+ let fullPath;
52
+ if (dirPath.startsWith('/')) {
53
+ fullPath = dirPath;
54
+ } else {
55
+ fullPath = path.resolve(explorerRoot, dirPath);
56
+ }
52
57
 
53
- // 防止路径遍历攻击
54
- if (!fullPath.startsWith(path.resolve(explorerRoot))) {
55
- return res.status(403).json({ error: 'Access denied' });
58
+ // 防止路径遍历攻击 - 确保路径不会越界
59
+ if (explorerRoot !== '/') {
60
+ const resolvedRoot = path.resolve(explorerRoot);
61
+ if (!fullPath.startsWith(resolvedRoot)) {
62
+ return res.status(403).json({ error: 'Access denied' });
63
+ }
56
64
  }
57
65
 
58
66
  if (!existsSync(fullPath)) {
@@ -70,16 +78,28 @@ function init() {
70
78
 
71
79
  files.forEach(file => {
72
80
  const filePath = path.join(fullPath, file.name);
73
- const fileStats = statSync(filePath);
81
+ let fileStats;
82
+ let hasError = false;
83
+
84
+ try {
85
+ fileStats = statSync(filePath);
86
+ } catch (statError) {
87
+ hasError = true;
88
+ }
89
+
74
90
  const relativePath = path.join(dirPath, file.name);
75
91
 
92
+ // 使用 statSync 的结果,因为 readdirSync 在根目录对某些特殊目录识别不准确
93
+ const isDirectory = hasError ? file.isDirectory() : fileStats.isDirectory();
94
+
76
95
  result.push({
77
96
  name: file.name,
78
97
  path: relativePath.replace(/\\/g, '/'),
79
- isDirectory: file.isDirectory(),
80
- size: fileStats.size,
81
- mtime: fileStats.mtime,
82
- isHidden: file.name.startsWith('.')
98
+ isDirectory: isDirectory,
99
+ size: hasError ? 0 : fileStats.size,
100
+ mtime: hasError ? new Date() : fileStats.mtime,
101
+ isHidden: file.name.startsWith('.'),
102
+ error: hasError ? 'Cannot access file' : null
83
103
  });
84
104
  });
85
105
 
@@ -106,11 +126,19 @@ function init() {
106
126
  let filePath = req.query.path || '/';
107
127
  filePath = decodeURIComponent(filePath);
108
128
 
109
- const fullPath = path.resolve(explorerRoot, '.' + filePath);
129
+ let fullPath;
130
+ if (filePath.startsWith('/')) {
131
+ fullPath = filePath;
132
+ } else {
133
+ fullPath = path.resolve(explorerRoot, filePath);
134
+ }
110
135
 
111
- // 防止路径遍历攻击
112
- if (!fullPath.startsWith(path.resolve(explorerRoot))) {
113
- return res.status(403).json({ error: 'Access denied' });
136
+ // 防止路径遍历攻击 - 确保路径不会越界
137
+ if (explorerRoot !== '/') {
138
+ const resolvedRoot = path.resolve(explorerRoot);
139
+ if (!fullPath.startsWith(resolvedRoot)) {
140
+ return res.status(403).json({ error: 'Access denied' });
141
+ }
114
142
  }
115
143
 
116
144
  if (!existsSync(fullPath)) {
@@ -152,11 +180,19 @@ function init() {
152
180
  let filePath = req.body.path || '/';
153
181
  filePath = decodeURIComponent(filePath);
154
182
 
155
- const fullPath = path.resolve(explorerRoot, '.' + filePath);
183
+ let fullPath;
184
+ if (filePath.startsWith('/')) {
185
+ fullPath = filePath;
186
+ } else {
187
+ fullPath = path.resolve(explorerRoot, filePath);
188
+ }
156
189
 
157
- // 防止路径遍历攻击
158
- if (!fullPath.startsWith(path.resolve(explorerRoot))) {
159
- return res.status(403).json({ error: 'Access denied' });
190
+ // 防止路径遍历攻击 - 确保路径不会越界
191
+ if (explorerRoot !== '/') {
192
+ const resolvedRoot = path.resolve(explorerRoot);
193
+ if (!fullPath.startsWith(resolvedRoot)) {
194
+ return res.status(403).json({ error: 'Access denied' });
195
+ }
160
196
  }
161
197
 
162
198
  if (!existsSync(fullPath)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "3.6.0",
3
+ "version": "3.6.2",
4
4
  "description": "🦅 Local Mock/Static/SPA server, Http?s request proxy, API overview page, File explorer",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {
@@ -13,6 +13,7 @@
13
13
  "test-watch": "tap --reporter=spec --watch test/*.test.js",
14
14
  "fix": "eslint --ext .js,.vue,.css --fix",
15
15
  "prettier": "prettier -c --write \"**/*.{ts,js,jsx,css,less,scss,json}\"",
16
+ "coverage": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls -v",
16
17
  "release:prerelease": "standard-version --prerelease",
17
18
  "release:prefix": "standard-version --prerelease alpha",
18
19
  "release:patch": "standard-version --release-as patch && git push --follow-tags origin master",