Search code examples
phpactionscript-3securitysandbox

AS3 SecurityError #2048 Mac vs Windows


I seem to be running into an interesting problem while saving a Sprite to my server. When I run the swf (remote) on my MAC it works fine but when I run the exact same URL from my windows computer I get a #2048 SecurityError.

Here my AS3 code:

    public function saveSprite(s:Sprite):void {
        var bmpData:BitmapData = new BitmapData(s.width, s.height, true, 0xFFFFFF);
        bmpData.draw(s);
        var byteArray:ByteArray = PNGEncoder.encode(bmpData);
        var encodedFile:Base64Encoder = new Base64Encoder();
        encodedFile.encodeBytes(byteArray);

        var data:URLVariables = new URLVariables();
        data.fileData = encodedFile;
        data.fileName = "test.png";
        data.location = "temp/";

        var request:URLRequest = new URLRequest(scriptLocation);
        request.method = URLRequestMethod.POST;
        request.data = data;

        var loader:URLLoader = new URLLoader();
        loader.addEventListener(Event.COMPLETE, errorLog);
        loader.addEventListener(Event.OPEN, errorLog);
        loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, errorLog);
        loader.addEventListener(IOErrorEvent.IO_ERROR, errorLog);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorLog);
        loader.addEventListener(ProgressEvent.PROGRESS, errorLog)

        try {
            loader.load(request);
        } catch (e:*) {
            debug.updateLog(e + "\n");
        }
    }

    private function errorLog(e:*):void {
        debug.updateLog(e + "\n");
    }

The AS3 connects with the following one-line PHP-file:

<?php file_put_contents($_POST['location'] . $_POST['fileName'], base64_decode($_POST['fileData']));

When I run the SWF on my mac the output is as follows: Log Start [Event type="open" bubbles=false cancelable=false eventPhase=2] [ProgressEvent type="progress" bubbles=false cancelable=false eventPhase=2 bytesLoaded=7 bytesTotal=0] [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=200 responseURL=null] [Event type="complete" bubbles=false cancelable=false eventPhase=2]

Where on a Windows I get the following output: Log Start [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=0 responseURL=null] [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048"]

Any thoughts on this will be appreciated. My own guess would be something with file permissions but what?

Thanks in advance!

[EDIT] It gets more interesting. 1. When I move my whole project to a different server I get a #2048 on windows and mac. 2. If I change my file permissions to 777 I get a #2048 on windows and mac as well.


Solution

  • Finally! I've found it.

    Adding a crossdomain.xml to the root of my webserver solved the issue. It seems wierd to me that this solves the problem as the php file I access is on the same server. Not only that, it's in the same folder!

    It seems that under MAC OSX the flashplayer recognizes that the request is within the same domain but under windows fails to do so. That would explain why under windows the security error is trown. Which resulted in my swf working under MAC but not on a windows machine. For now I have added the following crossdomain.xml file to the root of my server:

    <?xml version="1.0" ?>
    
    <cross-domain-policy>
        <site-control permitted-cross-domain-policies="master-only"/>
        <allow-access-from domain="*.mydomain.com" to-ports="*"/>
        <allow-http-request-headers-from domain="*.mydomain.com" headers="*"/>
    </cross-domain-policy>
    

    If you want to use this solution please change the 'mydomain.com' to the specific domain you want to allow. Read everything about the crossdomain.xml and how to use it here: http://learn.adobe.com/wiki/download/attachments/64389123/CrossDomain_PolicyFile_Specification.pdf?version=1

    As I stated in the question the following error was caught: [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048"]

    Note that usually the error contains: [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048: Security Sandbox Violation : http://www.domain1.com/file.swf can not load data from http://www.domain2.com/file.*"]

    The fact that a part of the error is missing in my case could indicate that a file in the same domain is requested.

    So the crossdomain.xml did the trick! I hope this answer will be of benefit to someone.

    Happy coding!