index.test.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const assert = require('node:assert/strict')
  2. const { describe, test, mock } = require('node:test')
  3. const launchEditor = require('./index.js')
  4. const UNC_ERROR = 'UNC paths are not supported on Windows to avoid security issues.'
  5. describe('launchEditor UNC path guard', () => {
  6. if (process.platform === 'win32') {
  7. test('rejects UNC paths on Windows via the error callback', () => {
  8. const onError = mock.fn()
  9. launchEditor('\\\\server\\share\\file.js', 'vim', onError)
  10. assert.equal(onError.mock.callCount(), 1)
  11. const [fileName, message] = onError.mock.calls[0].arguments
  12. assert.equal(fileName, '\\\\server\\share\\file.js')
  13. assert.ok(message.includes(UNC_ERROR))
  14. })
  15. test('strips the position suffix before reporting the rejected UNC path', () => {
  16. const onError = mock.fn()
  17. launchEditor('\\\\server\\share\\file.js:10:5', 'vim', onError)
  18. assert.equal(onError.mock.callCount(), 1)
  19. const [fileName, message] = onError.mock.calls[0].arguments
  20. assert.equal(fileName, '\\\\server\\share\\file.js')
  21. assert.ok(message.includes(UNC_ERROR))
  22. })
  23. test('does not treat a normal absolute Windows path as UNC', () => {
  24. const onError = mock.fn()
  25. // Non-existent file: without the UNC guard firing, launchEditor returns
  26. // silently at the `fs.existsSync` check without invoking the callback.
  27. launchEditor('C:\\Users\\me\\does-not-exist-xyz.js', 'vim', onError)
  28. assert.equal(onError.mock.callCount(), 0)
  29. })
  30. } else {
  31. test('does not apply the UNC guard on non-Windows platforms', () => {
  32. const onError = mock.fn()
  33. launchEditor('\\\\server\\share\\does-not-exist-xyz.js', 'vim', onError)
  34. assert.equal(onError.mock.callCount(), 0)
  35. })
  36. }
  37. })