nadesiko3 3.3.74 → 3.3.76

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/src/cnako3.mts CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env node
2
-
3
2
  /**
4
3
  * コマンドライン版のなでしこ3
5
4
  */
@@ -1,8 +1,8 @@
1
1
  // 実際のバージョン定義 (自動生成されるので以下を編集しない)
2
2
  const nakoVersion = {
3
- version: '3.3.74',
3
+ version: '3.3.76',
4
4
  major: 3,
5
5
  minor: 3,
6
- patch: 74
6
+ patch: 76
7
7
  };
8
8
  export default nakoVersion;
@@ -11,9 +11,9 @@ export interface NakoVersion {
11
11
  }
12
12
  // 実際のバージョン定義 (自動生成されるので以下を編集しない)
13
13
  const nakoVersion: NakoVersion = {
14
- version: '3.3.74',
14
+ version: '3.3.76',
15
15
  major: 3,
16
16
  minor: 3,
17
- patch: 74
17
+ patch: 76
18
18
  }
19
19
  export default nakoVersion
@@ -14,6 +14,7 @@
14
14
  #-----------
15
15
  APPKEY=""
16
16
  HREF=WINDOW["location"]["href"]
17
+ HREFを表示
17
18
  P=HREFをURLパラメータ解析
18
19
  APPKEY=P["appkey"]
19
20
  Pを表示。
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /** nako3edit用の超簡易サーバ(生のnodeだけで簡単HTTPサーバ) */
3
3
  import path from 'path'
4
- import fs, { existsSync } from 'fs'
4
+ import fs from 'fs'
5
5
  import { execSync } from 'child_process'
6
6
  import opener from 'opener'
7
7
  import http from 'http'
@@ -20,7 +20,11 @@ const homeDir = process.env[isWin ? 'USERPROFILE' : 'HOME']
20
20
  const userDir = path.join(homeDir, 'nadesiko3_user')
21
21
  const CNAKO3 = path.resolve(path.join(__dirname, '../../src/cnako3.mjs'))
22
22
  const NODE = process.argv[0]
23
- const appkey = 'K' + Math.floor(Math.random() * 10000000).toString(16)
23
+ const appkey = 'k' +
24
+ Math.floor(Math.random() * 0xFFFFFFFF).toString(16) +
25
+ Math.floor(Math.random() * 0xFFFFFFFF).toString(16) +
26
+ Math.floor(Math.random() * 0xFFFFFFFF).toString(16) +
27
+ Math.floor(Math.random() * 0xFFFFFFFF).toString(16)
24
28
 
25
29
  // ユーザーフォルダを作成
26
30
  if (!fs.existsSync(userDir)) { fs.mkdirSync(userDir) }
@@ -44,7 +48,11 @@ const server = http.createServer(function (req, res) {
44
48
  const q = String(a[1]).split('&')
45
49
  for (const kv of q) {
46
50
  const qq = kv.split('=')
47
- params[qq[0]] = decodeURIComponent(qq[1])
51
+ try {
52
+ params[qq[0]] = decodeURIComponent(qq[1])
53
+ } catch (e) {
54
+ console.error(e)
55
+ }
48
56
  }
49
57
  }
50
58
  // サニタイズ
@@ -155,7 +163,7 @@ function apiFiles (res) {
155
163
  res.end(JSON.stringify(files))
156
164
  }
157
165
  function apiLoad (res, params) {
158
- const fname = params.file
166
+ const fname = removePathFlag(params.file)
159
167
  const fullpath = path.join(userDir, fname)
160
168
  console.log('load=', fullpath)
161
169
  let text = '# 新規ファイル\n「こんにちは」と表示。'
@@ -172,7 +180,7 @@ function apiSave (res, params) {
172
180
  res.end('[ERROR] キーが違います')
173
181
  return
174
182
  }
175
- const fname = params.file
183
+ const fname = removePathFlag(params.file)
176
184
  const body = params.body
177
185
  const fullpath = path.join(userDir, fname)
178
186
  try {
@@ -186,6 +194,13 @@ function apiSave (res, params) {
186
194
  }
187
195
  }
188
196
 
197
+ function removePathFlag (s) {
198
+ // ファイル名をサニタイズ
199
+ s = s.replace(/['"`\\?/<>*]/g, '_')
200
+ s = s.replace(/_{2,}/g, '') // '__'を削除
201
+ return s
202
+ }
203
+
189
204
  function apiRun (res, params) {
190
205
  res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' })
191
206
  const appkeyUser = params.appkey
@@ -193,7 +208,7 @@ function apiRun (res, params) {
193
208
  res.end('[ERROR] キーが違います')
194
209
  return
195
210
  }
196
- const fname = params.file
211
+ const fname = removePathFlag(params.file)
197
212
  const body = params.body
198
213
  const fullpath = path.join(userDir, fname)
199
214
  try {
@@ -205,6 +220,8 @@ function apiRun (res, params) {
205
220
  result = String(result)
206
221
  } catch (err) {
207
222
  console.error(err)
223
+ res.end('[ERROR]実行に失敗しました。' + err.toString())
224
+ return
208
225
  }
209
226
  console.log('[run] file=', fname)
210
227
  console.log('--------------------------------')
@@ -212,6 +229,7 @@ function apiRun (res, params) {
212
229
  console.log('--------------------------------')
213
230
  res.end(result)
214
231
  } catch (err) {
232
+ console.error(err)
215
233
  res.end('[ERROR] 実行に失敗しました。')
216
234
  }
217
235
  }
@@ -220,27 +238,31 @@ function apiDelete (res, params) {
220
238
  res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' })
221
239
  const appkeyUser = params.appkey
222
240
  if (appkey !== appkeyUser) {
223
- res.end('[ERROR] キーが違います')
241
+ res.end('"[ERROR] キーが違います"')
224
242
  return
225
243
  }
226
- const fname = params.file
227
- const body = params.body
244
+ const fname = removePathFlag(params.file)
228
245
  const fullpath = path.join(userDir, fname)
229
246
  try {
230
- fs.unlinkSync(fullpath)
231
- res.end('"ok"')
247
+ if (fs.existsSync(fullpath)) {
248
+ fs.unlinkSync(fullpath)
249
+ res.end('"ok"')
250
+ } else {
251
+ res.end('"[ERROR] ファイルが見つかりません。"')
252
+ }
232
253
  return
233
254
  } catch (err) {
255
+ console.error(err)
234
256
  res.end('error:' + err.message)
235
257
  }
236
258
  }
237
259
 
238
260
  function apiGetNewFilename (res) {
239
261
  let fname = 'newfile.nako3'
240
- for (let i = 1; i <= 999; i++) {
241
- fname = `newfile${i}.nako3`
262
+ for (let i = 1; i <= 9999; i++) {
263
+ fname = `file${i}.nako3`
242
264
  const full = path.join(userDir, fname)
243
- if (fs.existsSync(fname)) { continue }
265
+ if (fs.existsSync(full)) { continue }
244
266
  break
245
267
  }
246
268
  res.writeHead(200, { 'Content-Type': 'text/plaing; charset=utf-8' })