host-files.test.mjs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {test} from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import {mkdtemp,mkdir,writeFile,symlink,rm} from 'node:fs/promises'
  4. import {tmpdir} from 'node:os'
  5. import {join} from 'node:path'
  6. import {listHostFiles,readHostFile,writeHostFile,execHostShell,resolveWithin} from './host-files.mjs'
  7. test('lists, reads and writes files inside the allowed root',async()=>{
  8. const root=await mkdtemp(join(tmpdir(),'yaoyao-host-files-'))
  9. try{
  10. await mkdir(join(root,'docs'))
  11. await writeFile(join(root,'docs','note.txt'),'你好,世界','utf8')
  12. const listing=await listHostFiles(root,'docs')
  13. assert.equal(listing.entries.length,1)
  14. assert.deepEqual(listing.entries[0],{name:'note.txt',type:'file',size:Buffer.byteLength('你好,世界'),mtime:listing.entries[0].mtime})
  15. const file=await readHostFile(root,'docs/note.txt')
  16. assert.equal(Buffer.from(file.data,'base64').toString('utf8'),'你好,世界')
  17. const written=await writeHostFile(root,{path:'docs/out/new.bin',data:Buffer.from([1,2,3]).toString('base64')})
  18. assert.equal(written.size,3)
  19. assert.deepEqual(Buffer.from(await readHostFile(root,'docs/out/new.bin').then(value=>Buffer.from(value.data,'base64'))),Buffer.from([1,2,3]))
  20. await assert.rejects(()=>listHostFiles(root,'missing'),/不存在/)
  21. await assert.rejects(()=>readHostFile(root,'docs'),/不是文件/)
  22. await assert.rejects(()=>writeHostFile(root,{path:'x.txt',data:'not base64!!'}),/base64/)
  23. }finally{await rm(root,{recursive:true,force:true})}
  24. })
  25. test('path escapes are refused for every operation',async()=>{
  26. const root=await mkdtemp(join(tmpdir(),'yaoyao-host-files-'))
  27. const outside=await mkdtemp(join(tmpdir(),'yaoyao-host-files-out-'))
  28. try{
  29. await writeFile(join(outside,'secret.txt'),'secret','utf8')
  30. await symlink(outside,join(root,'escape'))
  31. await writeFile(join(root,'keep.txt'),'keep','utf8')
  32. await assert.rejects(()=>resolveWithin(root,'../x'),/超出允许范围/)
  33. await assert.rejects(()=>resolveWithin(root,'/etc/passwd'),/超出允许范围/)
  34. await assert.rejects(()=>readHostFile(root,'escape/secret.txt'),/超出允许范围/)
  35. await assert.rejects(()=>listHostFiles(root,'escape'),/超出允许范围/)
  36. await assert.rejects(()=>writeHostFile(root,{path:'escape/evil.txt',data:Buffer.from('x').toString('base64')}),/超出允许范围/)
  37. await assert.rejects(()=>readHostFile(root,'keep.txt\u0000'),/路径无效/)
  38. assert.equal((await readHostFile(root,'keep.txt')).size,4)
  39. }finally{
  40. await rm(root,{recursive:true,force:true});await rm(outside,{recursive:true,force:true})
  41. }
  42. })
  43. test('shell execution reports stdout, exit codes, timeouts and cwd stays in root',async()=>{
  44. const root=await mkdtemp(join(tmpdir(),'yaoyao-host-files-'))
  45. try{
  46. await mkdir(join(root,'work'))
  47. const ok=await execHostShell(root,{command:'pwd',cwd:'work'})
  48. assert.equal(ok.exitCode,0)
  49. assert.ok(ok.stdout.includes(`${root}/work`),ok.stdout)
  50. const failing=await execHostShell(root,{command:'echo boom >&2; exit 7'})
  51. assert.equal(failing.exitCode,7);assert.equal(failing.stderr.trim(),'boom')
  52. const timed=await execHostShell(root,{command:'sleep 5',timeoutMs:1000})
  53. assert.equal(timed.timedOut,true);assert.notEqual(timed.exitCode,0)
  54. const escaped=await execHostShell(root,{command:'cd / && pwd'})
  55. assert.equal(escaped.exitCode,0)
  56. await assert.rejects(()=>execHostShell(root,{command:' '}),/命令为空/)
  57. }finally{await rm(root,{recursive:true,force:true})}
  58. })