bcoe / sandcastle

A simple and powerful sandbox for running untrusted JavaScript.
MIT License
220 stars 48 forks source link

Issues with Windows #44

Open Oceanswave opened 9 years ago

Oceanswave commented 9 years ago

Unfortunately, I'm seeing that seeing that SandCastle doesn't seem to execute scripts under windows.

var SandCastle = require('sandcastle').SandCastle;

var sandcastle = new SandCastle();

var script = sandcastle.createScript("\
  exports.main = function() {\
    exit('Hey ' + name + ' Hello World!');\
  }\
");

script.on('exit', function(err, output) {
    console.log(output); // Hello World!
});

script.on('timeout', function() {
    console.log('I timed out, oh what a silly script I am!');
});

script.run({name: 'Ben'});

results in a timeout

I timed out, oh what a silly script I am!

When using the Pool nothing seems to happen... no timeout, nothing.

var Pool = require('sandcastle').Pool;

var poolOfSandcastles = new Pool( { numberOfInstances: 3 }, { timeout: 6000 } );

var script = poolOfSandcastles.createScript("\
  exports.main = function() {\
    exit('Hello World!');\
  }\
");

script.on('exit', function(err, output) {
    console.log(output);
});

script.on('timeout', function(err, output) {
    console.log('I timed out, oh what a silly script I am!');
});

script.run({name: 'Ben'});
seubert commented 9 years ago

I believe this is related to #41.

Oceanswave commented 9 years ago

Making the changes in the referenced pull doesn't help any.

Seems like this never gets triggered.. this.sandbox.stdout.on('data', function(data) { _this.waitingOnHeartbeat = false; // Used to keep only one heartbeat on the wire at a time. _this.sandboxInitialized = true; });

bcoe commented 9 years ago

I don't have a Windows VM up and running at the moment, the submission of a patch that gets tests running on Windows would gain my eternal gratitude :)

jsas commented 9 years ago

I'm finally getting to this. Windows doesn't like Unix sockets. @Oceanswave, I passed a free tcp port into the Sandcastle ops for "socket" and got it to work.

bcoe commented 9 years ago

@jsas that's awesome that a tcp connection did the trick, I wonder if we should make this the default approach; about to respond to your email.

yoavprat commented 8 years ago

@jsas , How do I open tcp ports in Windows?

bcoe commented 8 years ago

@shai067, @yoavapi adding you to this long-standing thread on Windows support.

Curious if anyone has had a chance to dig into this further?

jsas commented 8 years ago

Sorry folks, I stopped working with sandcastle a long time ago and also moved to osx.

yoavprat commented 8 years ago

@jsas I can understand why... It works beautifully on OSX / Linux

marstein commented 7 years ago

In my team people did this to src/metadata/constraints/expressions.js:

import os from 'os';
import { Pool, SandCastle } from 'sandcastle';

function createPool() {
  /*
   * Horrendous hack to get sandcastle working on windows. This should only be important for running tests
   * locally on a windows machine - Lambda always runs on Linux so this code should never be executed in production.
   * SandCastle unfortunately assumes it can communicate with child processes via local sockets with names like
   * '/tmp/sandcastle_xx.sock' where xx is the instance number. But windows insists on names like '\\.\pipe\...'.
   * There's no way of configuring this in options to Pool creation so instead we hack the SandCastle prototype
   * to wrap the 'socket' property and transform it to a windows friendly format.
   */
  if (os.platform() === 'win32') {
    const socketPattern = /_(\d+)\./;
    console.log('Hacking socket property for Windows');
    Object.defineProperty(SandCastle.prototype, 'socket', {
      get: function () { // eslint-disable-line object-shorthand,func-names
        return this.realSocket;
      },
      set: function (value) { // eslint-disable-line object-shorthand,func-names
        let newValue = value;
        const found = value && value.match(socketPattern);
        if (found) {
          newValue = `\\\\.\\pipe\\sandcastle${found[1]}`;
        }
        this.realSocket = newValue;
      }
    });
  }
  return new Pool({ numberOfInstances: 3 }, { timeout: 6000 });
}

const sandcastle = createPool();
yoavprat commented 7 years ago

@marstein - Thanks! we will give it a go.

trevor-morris commented 7 years ago

I added a pull request that attempts to fix things for Windows - #71

Works ok on my Windows 7 machine and on some co-workers' Macs. If anyone else wants to try it and make sure it runs ok (pull down the branch then run "npm install" and "npm run test") then feel free.

Unfortunately "npm install" can run into problems installing node-gyp on Windows. node-gyp generally seems somewhat problematic on Windows, depending on exactly what you've got installed. I got it working after Googling around a while, but another of my co-workers just tried "npm install" and got an entirely different error on their Windows box...