Skip to content

Commit

Permalink
nako3editのファイル名におけるエラーを修正 #1347
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Oct 7, 2022
1 parent cc6c78d commit 4edd1ac
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@
"opener": "^1.5.2",
"shell-quote": "^1.7.3"
}
}
}
1 change: 0 additions & 1 deletion src/cnako3.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node
/**
* コマンドライン版のなでしこ3
*/
Expand Down
1 change: 0 additions & 1 deletion src/cnako3.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node

/**
* コマンドライン版のなでしこ3
*/
Expand Down
28 changes: 20 additions & 8 deletions tools/nako3edit/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/** nako3edit用の超簡易サーバ(生のnodeだけで簡単HTTPサーバ) */
import path from 'path'
import fs, { existsSync } from 'fs'
import fs from 'fs'
import { execSync } from 'child_process'
import opener from 'opener'
import http from 'http'
Expand All @@ -20,7 +20,7 @@ const homeDir = process.env[isWin ? 'USERPROFILE' : 'HOME']
const userDir = path.join(homeDir, 'nadesiko3_user')
const CNAKO3 = path.resolve(path.join(__dirname, '../../src/cnako3.mjs'))
const NODE = process.argv[0]
const appkey = 'K' + Math.floor(Math.random() * 10000000).toString(16)
const appkey = 'K' + Math.floor(Math.random() * 0xFFFFFFFF).toString(32) + Math.floor(Math.random() * 0xFFFFFFFF).toString(32)

// ユーザーフォルダを作成
if (!fs.existsSync(userDir)) { fs.mkdirSync(userDir) }
Expand All @@ -44,7 +44,11 @@ const server = http.createServer(function (req, res) {
const q = String(a[1]).split('&')
for (const kv of q) {
const qq = kv.split('=')
params[qq[0]] = decodeURIComponent(qq[1])
try {
params[qq[0]] = decodeURIComponent(qq[1])
} catch (e) {
console.error(e)
}
}
}
// サニタイズ
Expand Down Expand Up @@ -155,7 +159,7 @@ function apiFiles (res) {
res.end(JSON.stringify(files))
}
function apiLoad (res, params) {
const fname = params.file
const fname = removeFlag(params.file)
const fullpath = path.join(userDir, fname)
console.log('load=', fullpath)
let text = '# 新規ファイル\n「こんにちは」と表示。'
Expand All @@ -172,7 +176,7 @@ function apiSave (res, params) {
res.end('[ERROR] キーが違います')
return
}
const fname = params.file
const fname = removeFlag(params.file)
const body = params.body
const fullpath = path.join(userDir, fname)
try {
Expand All @@ -186,14 +190,20 @@ function apiSave (res, params) {
}
}

function removeFlag (s) {
// ファイル名をサニタイズ
s = s.replace(/['"`\\?/<>*]/g, '_')
return s
}

function apiRun (res, params) {
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' })
const appkeyUser = params.appkey
if (appkey !== appkeyUser) {
res.end('[ERROR] キーが違います')
return
}
const fname = params.file
const fname = removeFlag(params.file)
const body = params.body
const fullpath = path.join(userDir, fname)
try {
Expand All @@ -205,13 +215,16 @@ function apiRun (res, params) {
result = String(result)
} catch (err) {
console.error(err)
res.end('[ERROR]実行に失敗しました。' + err.toString())
return
}
console.log('[run] file=', fname)
console.log('--------------------------------')
console.log(result)
console.log('--------------------------------')
res.end(result)
} catch (err) {
console.error(err)
res.end('[ERROR] 実行に失敗しました。')
}
}
Expand All @@ -224,7 +237,6 @@ function apiDelete (res, params) {
return
}
const fname = params.file
const body = params.body
const fullpath = path.join(userDir, fname)
try {
fs.unlinkSync(fullpath)
Expand All @@ -240,7 +252,7 @@ function apiGetNewFilename (res) {
for (let i = 1; i <= 999; i++) {
fname = `newfile${i}.nako3`
const full = path.join(userDir, fname)
if (fs.existsSync(fname)) { continue }
if (fs.existsSync(full)) { continue }
break
}
res.writeHead(200, { 'Content-Type': 'text/plaing; charset=utf-8' })
Expand Down

0 comments on commit 4edd1ac

Please sign in to comment.