views:

94

answers:

5

Is there any web language that allows the client itself to create HTTP posts to external sites.

I know that JavaScript does this with XMLHttpRequest, but it does not allow cross-domain posting, unless the recipient domain wants to allow the sending domain.

I want to post data to an external site (that I don't control) and have the request be authenticated with what the client's browser already has (cookies, etc).

Is this possible? I tried cURL but it seems to make a server HTTP post, not a client HTTP post.


Edit:

A bit more insight of what I am trying to do:

I am trying to POST JSON to the website using the user's session (I said cookies but I believe they are PHP sessions, which I guess I still consider cookies).

The website does NOT check the referral (poor security #1) I can execute javascript and html on the webpage using my personal homepage (poor security #2) The JSON code will still work even if the content-type is form (poor security #3) There is no security checking at all, just PHP session checking.

The form idea is wonderful and it works. The probably again is that its JSON. So having sent postdata as foo={"test":"123", "test2":"456"} the whole foo= part messes it up. Plus forms seem to turn JSON into form encoding, so its sending:

foo=%7B%22 test%22%3A+%22 123%22%2C+%22 test2%22%3A+%22 456%22%7D

when i need it to send;

{"test":"123", "test2":"456"}

So with everything known, is there a better chance of sending JSON or not?

+2  A: 

I don't think so: You won't get hold of the user's auth cookies on the third party site from server side (because of the Single Origin Policy) and you can't make Ajax requests to the third party site.

The best you can do is probably create a <form> (maybe in an <iframe>), point it to the third party site, populate it with data, and have the user submit it (or auto-submit it). You will not be able to get hold of the request results programmatically (again because of the Single Origin Policy), but maybe it'll do - you can still show the request results to the user.

Pekka
@Pekka you bring up an interesting thought right now that I have never stopped to think about before. So I can't behind the scenes submit a form and get back results on behalf of the user, say uysing ajax. But...if I had a hidden iframe in my site that read cookies from the machine, posted a form to a remote site, scraped the screen with javascript for some juicy information, then posted another hidden form to my site that was recording that information that would be ok. Am I missing something in security that I am just overlooking? It can't be that easy can it?
spinon
@spinon You can't do it for any properly programmed web site (what you want to do is a cross site request forgery).
Artefacto
@spinon no, that's not possible because you can't access any part of the document using Javascript. You can't even fetch single pixels from it: http://stackoverflow.com/questions/1936021/javascript-eyedropper-tell-colour-of-pixel-under-mouse-cursor
Pekka
@Artefacto My interest has been peaked. What do you mean by properly programmed website? I still am not seeing what would prevent me from doing something like this? I am sure there must be something I am missing?
spinon
@Pekka oh ok that is right. Now I remember that. I knew I was missing something. Thanks for cleaning up my brain fart. Duh!!
spinon
@spinon A form submission made from another site should not be possible in a site that generates a token per form display (or even per session) and then validates that token on form submission. Google for CSRF. I don't know much about Javascript, but additionally, according to what Pekka claims, even if the form submission succeed, you wouldn't be able to read the result back.
Artefacto
@Artefacto you are right: Using a form this way *is* a form of CSRF - albeit a legitimate one.
Pekka
@Pekka yeah I know about that approach but in practice I don't think a lot of smaller sites are using it.
spinon
@Pekka, @Artefacto thanks for the JS Security 101 lesson. :P
spinon
A: 

I think for obvious reasons this is not allowed. If this was allowed what would stop a malicious person from posting form data from a person's browser to any number of sites in some hidden iframe or popup window.

If this is a design of your application you need to rethink what you are trying to accomplish.

EDIT: As @Pekka was pointing out I know you can submit a form to a remote site using typical form submits. I was referring to using some client side ajax solution. Sorry for the confusion.

spinon
It's no problem to post form data from a person's browser to an external site. It's just impossible to make a full Ajax request to one, and get the response back programmatically.
Pekka
@Pekka yeah that is what I was trying to say just not as clearly. Read the comment I left on yours though about something I never thought about before.
spinon
A: 

You should follow the way OpenID and other single-sign-on system works. How openID works is your website POSTs some token to openID service and in return gets authentication result. Refer How Does it Work? section here

Ankit Jain
Will probably not work because he doesn't control the external site he needs to make the request to.
Pekka
A: 

The client cannot post to an external site directly; it's a breach of basic cross-domain security models. The exception is accessing javascript with JSONP. What you describe would require access to a user's cookies for another website, which is impossible as the browser only allows cookie access within the same domain/path.

You would need to use a server-side proxy to make cross-domain requests, but you still cannot access external cookies: http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html

Ian Wetherbee
The server-side proxy won't really help either, because there are authentication cookies in the client's browser he seems to need to use for the operation.
Pekka
I said access to cookies is impossible, which includes client+server side methods.
Ian Wetherbee
A: 

Yes, you can use a special flash library that supports cross-domain calls: YUI connection manager

Added: not sure about the cookie authentication issue though...

Larry K
This sounds good, but I think Flash doesn't use the browser's cookies when making the request. It might be possible to add them manually, though.
Pekka
The key thing here are the cookies. If they weren't required he could as well setup a reverse proxy in his domain.
Artefacto