Solution: AS3 Security Error #2122 with 300 redirects

December 23rd, 2008 by Steven Sacks

The Flash player has an issue where it will not load a crossdomain.xml file on a redirect. What this means is that setting the load policy has no effect, since it will only load the crossdomain.xml from the first domain, not the redirected one.

A consequence of this is when loading images from CDNs such as Edgecast, Akamai, etc., you will get Security Error #2122 when you attempt to get the width/height of the image. Luckily, there is a simple and straightforward solution (via @jesterxl).

All you do is reload the image using the redirected url, which is available in the LoaderInfo.url in the onComplete event listener of the first load. The benefit of doing this is that the second load comes from the cache, so it's basically instantaneous (or however long it takes to load the crossdomain.xml from the redirected url). Here's some sample code on how to pull this off.

import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;

function load(url:String):void
{
        var sprite:Sprite = new Sprite();
        var loader:Loader = new Loader();
        sprite.addChild(loader);
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
        var lc:LoaderContext = new LoaderContext(true);
        lc.checkPolicyFile = true;
        loader.load(new URLRequest(url), lc);
}
function onComplete(event:Event):void
{
        // the redirected url
        var path:String = LoaderInfo(event.target).url;
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onReallyComplete);
        // swap out the old loader with the new one into the same container
        LoaderInfo(event.target).loader.parent.addChild(loader);
        LoaderInfo(event.target).loader.parent.removeChild(LoaderInfo(event.target).loader);
        var lc:LoaderContext = new LoaderContext(true);
        lc.checkPolicyFile = true;
        loader.load(new URLRequest(path), lc);
}
function onReallyComplete(event:Event):void
{
        var w:int = LoaderInfo(event.target).content.width;
        var h:int = LoaderInfo(event.target).content.height;
}

Posted in AS3, Bugs, Tips/Tricks

7 Responses

  1. Matt Przybylski

    I recently ran into this issue on a project I was working on, and I think the way I solved it was by loading the CDN's policy file first and then loading my original policy file and doing the loading that way. I am not looking at the code right now but if memory serves me correctly that seemed to work.

  2. Steven Sacks

    @matt The issue is that you have no idea where the CDN's policy file is because the CDN is doing a redirect to a completely different server. Loading the policy file on the CDN's main server has no effect because the file is coming from a different domain, which is dynamic based on where the user is geographically.

  3. Larry Reynolds

    Steve, could you show us how checkPolicyFile can be set on a page asset in Gaia? I just ran into this issue (only with Flash Player 10 though) and this is my first experience using your framework.

    thanks!

  4. localToGlobal » Blog Archive » news review -> 2nd week of 2009

    [...] > Solution: AS3 Security Error #2122 with 300 redirects | steven sacks [...]

  5. Chet

    Thank you so much! Worked like a charm for CastFire Thumbnails :)

  6. Flex and Flash Developer - Jesse Warden dot Kizz-ohm » Blog Archive » Handling Crossdomain.xml and 302 Redirects Using NetStream

    [...] had forgotten Steven Sacks and I had this exact same problem loading images from Cast Fire 3 months ago. If you load a JPEG from Cast Fire from example, [...]

  7. Xavi Colomer

    Thank you!!! You save me some precious time. Keep working on!!!!!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Steven Sacks

I am a professional Flash developer with over 13 years of programming experience. I have consulted for high-profile agencies and companies in San Francisco, Los Angeles, Atlanta and New York, and developed numerous award-winning websites and rich internet applications for clients including Adobe, Fox Sports, FX Networks, Anheuser-Busch, GE, DirecTV, ESPN, The Weather Channel, Home Depot, and Coca-Cola.

I am the author of the open-source Gaia Framework for Adobe Flash, which dramatically reduces development time and makes developing Flash sites much easier.