2011-02-14 95 views
1

在插件上下文(由另一個swf加載的swf)中,是否有任何方式限制對加載的swf在同一時間訪問文件系統網絡?安全:限制插件訪問文件系統和網絡

編譯選項「-use-network = true | false」不適合,因爲您不能限制文件/網絡。

代碼例如:

空氣應用

package 
{ 
    import flash.display.Loader; 
    import flash.display.Sprite; 
    import flash.filesystem.File; 
    import flash.net.URLRequest; 

    public class TestContentSecurity extends Sprite 
    { 
     private var l :Loader = new Loader; 
     public function TestContentSecurity() 
     { 
      addChild(l); 
      l.load(new URLRequest(File.documentsDirectory.nativePath + "/Content.swf")); 
     } 
    } 
} 

加載的SWF

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.IOErrorEvent; 
    import flash.net.URLLoader; 
    import flash.net.URLRequest; 
    import flash.system.ApplicationDomain; 
    import flash.text.TextField; 

    public class Content extends Sprite 
    { 
     private var _log : TextField = new TextField; 
     private var l: URLLoader; 
     public function Content() 
     { 
      addChild(_log) 
      _log.multiline = true; 
      _log.width = 500; 
      _log.height = 500; 
      l = new URLLoader(); 
      l.addEventListener(Event.COMPLETE, onLoad); 
      l.addEventListener(IOErrorEvent.IO_ERROR, onError); 
      l.load(new URLRequest("c:/Windows/regedit.exe")) 
     } 

     public function onLoad(e:Event) : void{ 
      _log.text += "SUCCESS\n" ; 
     } 
     public function onError(e:IOErrorEvent) : void{ 
      _log.text += "ERROR\n"; 
     } 
    } 
} 

加載的SWF是在用戶的文檔文件夾中,外部空氣的應用程序的文件夾。目前,加載的瑞士法郎能夠加載「c:/Windows/regedit.exe」,我不希望它(既不在網絡上發送信息)。

回答

1

我在AIR中找到了一個解決方案,我不喜歡它,但它工作。這個想法是有一個迷你http服務器,並從這個服務器加載內容。

我加載有針對性的文件: new URLRequest("http://localhost:1111/Content.swf")

通過這樣做,閃存將加載「Content.swf」作爲一個遠程文件,並將其放置在遠程安全沙箱中。加載的swf將不能訪問任何本地文件,也不能訪問網絡。

如果有人有一個更清潔的解決方案來獲得這個REMOTE安全沙箱,我會很高興。

/** 
* HTTP server original idea : 
* http://coenraets.org/blog/2009/12/air-2-0-web-server-using-the-new-server-socket-api/ 
*/ 
package 
{ 
    import flash.display.Loader; 
    import flash.display.Sprite; 
    import flash.filesystem.File; 
    import flash.filesystem.FileMode; 
    import flash.filesystem.FileStream; 
    import flash.net.URLRequest; 
    import flash.events.Event; 
    import flash.events.ProgressEvent; 
    import flash.events.ServerSocketConnectEvent; 
    import flash.net.ServerSocket; 
    import flash.net.Socket; 
    import flash.utils.ByteArray; 

    public class TestContentSecurity extends Sprite 
    { 
     private var l :Loader = new Loader; 
     private var serverSocket:ServerSocket; 

     public function TestContentSecurity() 
     { 
      init(); 
      l.load(new URLRequest("http://localhost:1111/Content.swf")); 
     } 


     private function init():void 
     { 
      // Initialize the web server directory (in applicationStorageDirectory) with sample files 
      listen(1111); 
     } 

     private function listen(port : uint):void 
     { 
      try 
      { 
       serverSocket = new ServerSocket(); 
       serverSocket.addEventListener(Event.CONNECT, socketConnectHandler); 
       serverSocket.bind(port, "127.0.0.1"); 
       serverSocket.listen(); 
       trace("Listening on port " + port + "...\n"); 
      } 
      catch (error:Error) 
      { 
       trace("Port " + port + 
        " may be in use. Enter another port number and try again.\n(" + 
        error.message +")", "Error"); 
      } 
     } 

     private function socketConnectHandler(event:ServerSocketConnectEvent):void 
     { 
      var socket:Socket = event.socket; 
      socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 
     } 

     private function socketDataHandler(event:ProgressEvent):void 
     { 
      try 
      { 
       var socket:Socket = event.target as Socket; 
       var bytes:ByteArray = new ByteArray(); 
       socket.readBytes(bytes); 
       var request:String = "" + bytes; 

       var filePath:String = request.substring(5, request.indexOf("HTTP/") - 1); 
       var file:File = File.applicationDirectory.resolvePath(filePath); 
       if (file.exists && !file.isDirectory) 
       { 
        var stream:FileStream = new FileStream(); 
        stream.open(file, FileMode.READ); 
        var content:ByteArray = new ByteArray(); 
        stream.readBytes(content); 
        stream.close(); 
        socket.writeUTFBytes("HTTP/1.1 200 OK\n"); 
        socket.writeUTFBytes("Content-Type: application/x-shockwave-flash\n\n"); 
        socket.writeBytes(content); 
       } 
       else 
       { 
        socket.writeUTFBytes("HTTP/1.1 404 Not Found\n"); 
        socket.writeUTFBytes("Content-Type: text/html\n\n"); 
        socket.writeUTFBytes("<html><body><h2>Page Not Found</h2></body></html>"); 
       } 
       socket.flush(); 
       socket.close(); 
      } 
      catch (error:Error) 
      { 
       trace("Error"); 
      } 
     } 
    } 
} 
0

除非您將其作爲AIR應用程序進行部署。

你可以,但是,存儲一些數據在SharedObject,甚至當你部署具有-use-network=true。這應該適用於存儲遊戲狀態等。

編輯

在AIR中,從不同的域內容之間的安全是通過使用AIR sandbox bridges調節。這應該給你所有你需要的槓桿。

+0

這個項目是一個AIR應用程序,但我沒有看到任何可以幫助我的東西。我需要禁止網絡使用和本地訪問。 – 2011-02-14 12:03:03