News for hackers. Stuff that matters.
Re:Secure Your REST API by tptacek on Wednesday April 17, 2013 @02:40PM (#5566202)
You should probably disregard this advice. Instead:

[Late addition:

* Do not use passwords as API authentication. The user of an API is a computer program, not a human. Issue single-purpose random credentials for API access.]

* Make sure that your API is accessible only over HTTPS; test the API endpoint to ensure requests aren't honored over unencrypted HTTP.

* Use the simplest API authentication mechanism that (a) works and (b) you understand. You should probably degrade the capabilities of your API before you try to adopt an authentication system that you don't completely, fully, absolutely grok.

This means, if you don't need delegation right away (for instance, if you don't have 3rd party applications making calls to your API on behalf of your users), don't bother with anything like OAuth.

Contrary to the implications in this article, HTTP Basic Auth is not less secure than OAuth if your endpoints all require HTTPS. If you allow HTTP calls to an API, you have bigger problems than your auth token scheme.

Semantic mismatches between API endpoints and web UI endpoints (ie, semantic differences between API auth and cookie-based authentication) are a classic, pervasive, hard- to- eradicate source of serious security flaws. Don't make things any more complicated than they need to be.

[Late edit: I didn't think I needed to make the first point, but I clearly did need to. Sorry.]

Re:Secure Your REST API by MattRogish on Wednesday April 17, 2013 @07:12PM (#5567941)
So let's say I have an iOS or Android app that needs to talk to my REST API. I have my user login in the app, which does Basic Auth over HTTPS.

Once they're authenticated, I generate a token and send it to them. They store it, and use that token from here on out (HTTPS only).

Perhaps I expire it after some time (hours or days, if I don't want my user to have to login all the time).

That's it? :D Seems like I'm missing something...

Re:Secure Your REST API by krichman on Thursday April 18, 2013 @12:40AM (#5568926)
That sounds good. Think about how, e.g., Gmail lets you persist sessions on each device. They're doing a password auth over HTTPS and leaving a cookie with some session token.
Re:Secure Your REST API by chacham15 on Thursday April 18, 2013 @03:29AM (#5569322)
That sounds very close to OAuth 2.
Re:Secure Your REST API by tel on Wednesday April 17, 2013 @02:49PM (#5566267)
> Semantic mismatches between API endpoints and web UI endpoints (ie, semantic differences between API auth and cookie-based authentication) are a classic, pervasive, hard- to- eradicate source of serious security flaws. Don't make things any more complicated than they need to be.

Could you explain that one in more detail? I don't think I understand it.

Re:Secure Your REST API by tptacek on Wednesday April 17, 2013 @03:02PM (#5566351)
You have one code path that authenticates web users through forms and sets cookies.

You have another code path that authenticates API callers.

You have code scattered through your whole application that makes authorization checks based on which user you're authenticated on.

Inconsistencies between the first two code paths often break that code.

Re:Secure Your REST API by steveklabnik on Wednesday April 17, 2013 @02:44PM (#5566226)
Wow, I don't think I've ever seen tptacek gray in a thread about security. I don't know why, you are absolutely right.

One neat variant on this is how GitHub uses HTTP Basic auth to give you an OAuth token... http://developer.github.com/v3/#authentication

Re:Secure Your REST API by veesahni on Wednesday April 17, 2013 @04:48PM (#5567108)
If you don't need delegation, there are a number of simple but secure methods: HTTPS + token via Basic auth seems quite popular.

What simple but secure methods are there where delegation is required? Is there something simpler than OAuth or AWS style signed HMAC?

Re:Secure Your REST API by bradleyjg on Wednesday April 17, 2013 @04:24PM (#5566934)
The one true solution is client authenticated TLS. For the life of me I can't understand why 25 years later certificate management is still such a mess. 

We don't need some perfect universally recognized root trust system to get started. Why doesn't the sign up process for authenticated API access routinely include the issuance of a certificate signed by the API owner?

Unlike for interactive users there's no expectation that a customer will be accessing an API from some random computer where he might not have access to his certificate store. 

Re:Secure Your REST API by tptacek on Wednesday April 17, 2013 @04:28PM (#5566962)
The user interface in browsers for client certificates is appalling.

The API support in client SSL libraries for managing multiple certificates, for applications with multiple API affiliations, is shaky.

I like TLS client authentication a lot, but it's hard to make it work.

Re:Secure Your REST API by bradleyjg on Wednesday April 17, 2013 @04:55PM (#5567174)
I agree it's awful to use, but what I don't understand is why no one has bothered to improve the tools.

It's one of those areas where the underlying tool (either a library like openssl or NSS, or an OS feature like SSPI) could do the hard work in one place and make it simple for downstream libraries to wrap the functionality.

In other, somewhat analogous, domains that happened, but for whatever reason not in this case.

Re:Secure Your REST API by chacham15 on Thursday April 18, 2013 @03:34AM (#5569335)
Look at the OpenSSL libraries for php regarding certificates. Its very close to unusable, you cannot get or set some pretty basic information like SANs.
Re:Secure Your REST API by carbocation on Wednesday April 17, 2013 @11:30PM (#5568777)
Do you think there would be value in accepting HTTP requests and then automatically and immediately expiring every credential that ever gets passed over plain HTTP?
Re:Secure Your REST API by tptacek on Thursday April 18, 2013 @01:38AM (#5569064)
With an API, I think you're better off just not accepting HTTP connections to begin with. This is library design, so, principle of least astonishment. Break hard and fast.
Re:Secure Your REST API by redblacktree on Wednesday April 17, 2013 @03:20PM (#5566471)
Even if you use HTTPS, aren't you vulnerable to lazy devs who put the password in the URL?

If I use https://username:[email protected]/, doesn't that URL show up in server logs all over the internet?

Re:Secure Your REST API by tptacek on Wednesday April 17, 2013 @03:21PM (#5566478)
Don't ever use passwords for API authentication. It's an API, not a browser. The users of an API are other programs, not people. Issue single-purpose random credentials.

I didn't think this was something I had to point out about API authentication, but apparently it is.

Re:Secure Your REST API by CodeMage on Wednesday April 17, 2013 @04:52PM (#5567144)
I have a stupid question: how do you do this with HTTP Basic auth?
Re:Secure Your REST API by tptacek on Wednesday April 17, 2013 @05:01PM (#5567225)
Generate random long passwords.
Re:Secure Your REST API by redblacktree on Thursday April 18, 2013 @12:55PM (#5571669)
By "don't ever use passwords" you mean, "don't let users set their own passwords," right?

Obviously, you're still using a password if you use HTTP Basic Auth.

Re:Secure Your REST API by lhazlewood on Wednesday April 17, 2013 @03:44PM (#5566631)
You shouldn't ever use username/password pairs for API authentication. If the user ever changed their password, then their API calls would immediately fail!

This is one of many benefits of using multiple (revokable) API Keys.

Re:Secure Your REST API by jimktrains2 on Wednesday April 17, 2013 @03:34PM (#5566568)
No. The UA strips those and puts them in an Auth header.
Re:Secure Your REST API by lhazlewood on Wednesday April 17, 2013 @03:40PM (#5566603)
Depends on the UA (curl?)
Re:Secure Your REST API by jimktrains2 on Wednesday April 17, 2013 @10:57PM (#5568697)
OK, so it'll either add an auth header or not know what to do and refuse to do anything. Either way the password is not sent in the request url
Re:Secure Your REST API by jimktrains2 on Thursday April 18, 2013 @09:12AM (#5570374)
Actually, I mused have screwed up last night. The version of curl I have does support it and does put it into the Auth header.

  % curl --trace-ascii /dev/stdout http://jimktrains:[email protected]
  == Info: About to connect() to news.ycombinator.com port 80 (#0)
  == Info:   Trying 184.172.10.74... == Info: connected
  == Info: Server auth using Basic with user 'jimktrains'
  => Send header, 223 bytes (0xdf)
  0000: GET / HTTP/1.1
  0010: Authorization: Basic amlta3RyYWluczpwYXNzd29yZA==
  .......
Some UAs however, do just drop it. Like IE (http://support.microsoft.com/kb/834489)
Re:Secure Your REST API by jes5199 on Wednesday April 17, 2013 @11:45PM (#5568811)
Even curl does the right thing for basic auth
Re:Secure Your REST API by olivier1664 on Wednesday April 17, 2013 @10:22PM (#5568609)
Please correct me if I'm wrong. HTTPS will encrypt the URL, but the DNS lookup is in clear. So the "username:[email protected]" part will be sniffable/loggable.

http://stackoverflow.com/questions/499591/are-https-urls-enc...

Re:Secure Your REST API by jewel on Wednesday April 17, 2013 @11:19PM (#5568748)
That's incorrect. The URI library will parse the URL into a hostname, username, and password. Only the hostname is sent to the DNS server.

The username and password are sent in the "Authorization" HTTP header, which will be encrypted.

Re:Secure Your REST API by dllthomas on Wednesday April 17, 2013 @03:10PM (#5566407)
Shouldn't HTTP Digest still be preferred, so your server doesn't need to see the passwords?
Re:Secure Your REST API by tptacek on Wednesday April 17, 2013 @03:15PM (#5566443)
That's not how HTTP Digest auth works, and you should never, ever be using "passwords" for API authentication.
Re:Secure Your REST API by dllthomas on Wednesday April 17, 2013 @03:24PM (#5566503)
Are you saying the server sees the password in HTTP Digest authentication?
Re:Secure Your REST API by jimktrains2 on Wednesday April 17, 2013 @03:36PM (#5566584)
How else would the server know what to check your response against? If you don't want to give the server a password, try SRP.
Re:Secure Your REST API by tptacek on Wednesday April 17, 2013 @03:48PM (#5566659)
... but please don't use SRP for your API authentication.
Re:Secure Your REST API by dllthomas on Wednesday April 17, 2013 @03:55PM (#5566707)
Well, in this case (having gone back and actually checked the docs rather than relying on recollection roughly a decade old) you could store MD5(username:realm:password) rather than password, though that doesn't buy you all that much in a case like this where making the password a long, random string that's not reused is not only good practice but also easy and so likely to be general practice.
Re:Secure Your REST API by tptacek on Wednesday April 17, 2013 @03:57PM (#5566728)
Stipulating all the other points you've made, exactly what is the purpose of hiding from a server a long random string generated by that server and useful only to that server?
Re:Secure Your REST API by jimktrains2 on Wednesday April 17, 2013 @10:22PM (#5568608)
Preventing someone else from getting that and using it on the server, especially if the service can rack up charges.
Re:Secure Your REST API by dllthomas on Wednesday April 17, 2013 @11:02PM (#5568710)
If you have the hash, you can log into the site (just not using the typical libraries), so it really doesn't add a lot of security. That's why I was wrong :-P
Re:Secure Your REST API by dllthomas on Wednesday April 17, 2013 @04:14PM (#5566864)
Yeah, I got there, just was confused about the digest thing.
Re:Secure Your REST API by healsjnr1 on Wednesday April 17, 2013 @06:22PM (#5567720)
I also wouldn't put too much value in a security article served on page using mixed HTTP and HTTPS. This shows a distinct lack of regard for and understanding of security.