execute remote SWF in AIR Security Sandbox - actionscript-3

I try to download an external SWF and run it within the AIR security sandbox.
this is the code of the AIR application:
public class Downloader extends Sprite
{
private static const REMOTE_FILE:URLRequest = new URLRequest("http://myserver.com/downloadable.swf");
private var _main:NativeWindow;
public function Downloader()
{
var loader:URLLoader = new URLLoader(REMOTE_FILE);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, downloadComplete);
}
private function downloadComplete(e:Event):void{
var ba:ByteArray = e.target.data;
var stream:FileStream = new FileStream();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
stream.open(file, FileMode.WRITE);
stream.writeBytes(ba);
stream.close();
loadAndRunSwf();
}
private function loadAndRunSwf(){
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
////obsolete?
//var context:LoaderContext = new LoaderContext();
//context.allowLoadBytesCodeExecution = true;
//context.applicationDomain = ApplicationDomain.currentDomain;
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context*/);
this._main.stage.addChild(loader);
this._main.activate();
}
}
Code of the downloadable.swf:
public class Downloadable extends Sprite
{
private var _btn:Button = new Button();
private var _baseFolder:File = new File("app-storage:/");
public function downloadable_test()
{
this.addChild(_btn);
_btn.label = "access Harddisk";
...
}
}
so now, If I run Downloader, it will download the swf and try to run it, but i'll get an exception in Downloadable on the line
private var _baseFolder:File = new File("app-storage:/");
the error:
SecurityError: file
at runtime::SecurityManager$/checkPrivilegeForCaller()
So - what Do I need to do to prevent such security errors? I want my remote SWF to be treated as native code running in the same security sandbox as the AIR code.

I'm not sure about Android, but for the regular web player you would need to specify SecurityDomain.currentDomain for the securityDomain of the Loader's context for the loaded code to be considered equal to the loader in terms of permissions. Also note that for reasons impossible to explain, if you use SecurityDomain when loading from the file system on PC Flash Player will complain.
However complicated, the Flash Player security is often a security by obscurity... So, if it doesn't work the way you coded it, try Loader.loadBytes() "workaround".

function loadAndRunSwf()
{
var context:LoaderContext=new LoaderContext(false);
context.allowCodeImport=true;
var ba:ByteArray=new ByteArray();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var fileStream:FileStream=new FileStream();
var loader:Loader = new Loader();
fileStream.open(file,"read");
fileStream.readBytes(ba);
ba.position=0;
loader.loadBytes(ba,context);
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
this._main.stage.addChild(loader);
this._main.activate();
}

Related

add `movieclip` from external swf's library

I loaded External swf file to stage :
var mcLoader = new Loader();
this.addChild(mcLoader);
mcLoader.load(new URLRequest("E:\\MySWF.swf"));
var mcLoader = new Loader();
this.addChild(mcLoader);
now I want to add a movieclip from external swf's library with "Part3" as linkage name, I did like below :
var cls:Class = mcLoader.contentLoaderInfo.applicationDomain.getDefinition("Part3") as Class;
var MCCurrentPart = new cls;
but it didn't work, After that I tried like this :
var MCCurrentPart = new mcLoader.content.Part2;
but now it gives error : Instantiation attempted on a non-constructor.
Your use of getDefinition() is fine, just you have to wait until the Event.COMPLETE event in the contentLoaderInfo of the Loader is fired.
So you can do like this :
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_content_load);
loader.load(new URLRequest('swf.swf'));
function on_content_load(e:Event): void
{
var MyClass:Class = loader.contentLoaderInfo.applicationDomain.getDefinition('MyClass') as Class;
var obj = new MyClass;
addChild(obj);
}
You can take a look in some examples from Adobe here.
Hope that can help.

AS3 URLRequest not working in browser

My URL request is working perfectly in Flash Pro, but when I test it in the Browser, the image doesn't work at all. It doesn't load. Is there a Publish Setting I need to be using or something?
Please help, it's pretty frustrating. Here's what it looks like when it's in the browser.
Relevant code:
public function startSlidingPuzzle() {
// blank spot is the bottom right
blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
// load the bitmap
loadBitmap("http://fc07.deviantart.net/fs71/f/2014/030/d/7/slidingimage_by_nooodisaster-d74et6t.jpg");
}
// get the bitmap from an external source
public function loadBitmap(bitmapFile:String) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var request:URLRequest = new URLRequest(bitmapFile);
loader.load(request);
}
// bitmap done loading, cut into pieces
public function loadingDone(event:Event):void {
// create new image to hold loaded bitmap
var image:Bitmap = Bitmap(event.target.loader.content);
pieceWidth = image.width/numPiecesHoriz;
pieceHeight = image.height/numPiecesVert;
trace(numPiecesHoriz)
// cut into puzzle pieces
makePuzzlePieces(image.bitmapData);
// shuffle them
shufflePuzzlePieces();
}
You have a security error:
SecurityError: Error #2122: Security sandbox violation: Loader.content: http://fc03.deviantart.net/fs71/f/2014/030/e/e/beyonce_slidingpuzzle___flash_diary_day_10_by_nooodisaster-d74er8h.swf cannot access http://i.stack.imgur.com/ls9QI.jpg. A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.
at flash.display::Loader/get content()
at SlidingPuzzle/loadingDone()
Try adding a security context:
public function loadBitmap(bitmapFile:String)
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
context.securityDomain = SecurityDomain.currentDomain;
context.checkPolicyFile = true;
var request:URLRequest = new URLRequest( bitmapFile );
loader.load( request, context );
}
If that doesn't fix it you'll need to add a crossdomain.xml to the server hosting the images you are requesting. Why not save the images locally and deploy them in the same scope as your build?
[1]:

Looking to use Firebase with Actionscript 3 / Air

I have found Firebase, and it looks excellent for javascript / HTML5 usage.
But I was wondering if there is also an Actionscript API?
E.g
var myRootRef = new Firebase('https://myprojectname.firebaseIO-demo.com/');
myRootRef.set('Hello World!');
var dataRef = new Firebase('https://SampleChat.firebaseIO-demo.com/users/fred/name/first');
dataRef.on('value', function(snapshot) {
alert('fred’s first name is ' + snapshot.val());
});
So to set data and have listeners for updated data etc.
Thanks for any help
Matt
Doesn't look like there is an AS3 api available but the good news is that they have a rest api which is cross platform that you can write an AS3 wrapper around. (https://www.firebase.com/docs/rest-api.html) That is what I plan to do. I Want to use it with GameBuilder Studio.
If you are planning on embedding your application inside a web page (that you create) you could use an external javascript interface and communicate to firebase through that.
I'm planning on doing this myself to see how it turns out.
Connecting to Firebase using ActionScript 3 only requires the use of URLRequest and URLLoader. The following examples cover the 4 basic operations (CRUD).
To read data from an specific node from your Firebase database:
private function loadNews():void
{
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_read>.json");
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, newsLoaded);
loader.load(request);
}
private function newsLoaded(event:flash.events.Event):void
{
trace(event.currentTarget.data);
var rawData:Object = JSON.parse(event.currentTarget.data);
}
To insert data to a specific node:
private function saveEntry(title:String, description:String):void
{
var myObject:Object = new Object();
myObject.title = title;
myObject.description = description;
myObject.timestamp = new Date().getTime();
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_insert>.json");
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entrySent);
loader.load(request);
}
private function entrySent(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
To delete a specific node:
private function deleteEntry():void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "DELETE");
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_delete>.json");
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryDeleted);
loader.load(request);
}
private function entryDeleted(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
To update/modify data to a specific node:
private function updateEntry(title:String, description:String):void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "PATCH");
var myObject:Object = new Object();
myObject.title = title;
myObject.description = description;
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/journal/<Node_to_modify>.json");
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryUpdated);
loader.load(request);
}
private function entryUpdated(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
If you want further information I have written detailed Firebase REST guides and examples on how to use ActionScript 3 and Firebase.

How to upload a background image using actionscript 3.0 code?

Need your help. I am trying it first time.
I have used following code.
But i get this in my console:
started loading file
SecurityError: Error #2000: No active security context.
and my image url is in same folder as my script file.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.load(new URLRequest("C:\Documents and Settings\Owner\Desktop\23Aug\demo1\mic.jpg"), context);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
trace("started loading file");
addChild(loader);
function fileLoaded(event:Event):void
{
trace("file loaded");
}
Reasons to throw securityError exception.
Invalid path,
Trying to access a URL, that not permitted by the security sandbox,
Trying a socket connection, that exceeding the port limit, and
Trying to access a device, that has been denied by the user(Ex., camera, microphone.)
try this
private var _loader:Loader = new Loader();
private var _context:LoaderContext = new LoaderContext();
private var _url:URLRequest = new URLRequest("demo1/mic.jpg");
_context.checkPolicyFile = false;
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageloaded);
//_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_loader.load(_url, _context);
private function onImageloaded(e:Event):void{
addChild(e.target.content);
}
Use slashes instead of back slashes :
loader.load(new URLRequest("C:/Documents and Settings/Owner/Desktop/23Aug/demo1\mic.jpg"), context);
But the best way is to use relative path like "./mic.jpg" and do not use absolute path.

URLLoader handler in child movie not being called

Hi I am writing a flex application that has a MainMovie that loads flex programs (ChildMovie) depending on what the user selects in the MainMovie. below is some pseudocode to help me describe my problem hopefully.
class MainMovie{
private var request:URLRequest = new URLRequest();
public function callPHPfile(param:String, loader:URLLoader,
handlerFunction:Function):void {
var parameter:URLVariables=new URLVariables();
parameter.param = param;
request.method = URLRequestMethod.POST;
request.data = parameter;
request.url = php file on server;
loader.addEventListener(Event.COMPLETE, handlerFunction);
loader.load(request);
}
}
Class ChildMovie {
private var loaderInChild:URLLoader = new URLLoader();
public function handlerInChild(e:Event):void {
process data....
loaderInChild.removeEventListerner(Event.COMPLETE, handlerInChild);
}
private function buttonClickHandler(e:Event):void{
Application.application.callPHPfile(param, loaderInChild, handlerInChild)
}
}
I can see that the callPHPfile function is being executed and received xml data from in httpFox, the problem is that the code in the handlerInChild function is not being executed. What am I doing wrong here?
It was a runtime error. I forgot that i uninstalled flash player debugger in firefox and it didn't show. in the handlerInChild function, there is a line
var data:XML = loader.data;
it should be
var data:XML = XML(loader.data);
and the code will run as expected.