You can use a masking pass to make this happen. I believe it's the right way to approach the problem. Your original code is going to need some additions to make this possible.
First you'll need to create a render target that has a stencil buffer enabled to pass in to the effect composer to use. The information in the stencil buffer allows for masking to take place.
renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat, stencilBuffer: true };
renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, renderTargetParameters );
composer = new THREE.EffectComposer( renderer, renderTarget );
Next, I'd recommend having your background and foreground passes both be in the composer. It makes the rendering stack cleaner. When you render the foreground scene make sure its clear property is set to false so that the stencil buffer data is preserved for the masking pass.
Then a mask pass, the ssao pass, a clear mask path, and a final shader pass to render it all to the screen. You could use a simple pass through shader or fxaa here. There may be another way to render to screen as well that I'm not aware of, but you can not assign the ssao pass to render to screen and have it work when the clear mask pass follows it.
here's a link to a fiddle with this approach implemented http://jsfiddle.net/xqLdz/3/