<p>When designing a REST API or service are there any established best practices for dealing with security (Authentication, Authorization, Identity Management) ?</p>

<p>When building a SOAP API you have WS-Security as a guide and much literature exists on the topic. I have found less information about securing REST endpoints.</p>

<p>While I understand REST intentionally does not have specifications analogous to WS-* I am hoping best practices or recommended patterns have emerged.</p>

<p>Any discussion or links to relevant documents would be very much appreciated.
If it matters, we would be using WCF with POX/JSON serialized messages for our REST API's/Services built using v3.5 of the .NET Framework.</p>

[{'answer_id': 7562, 'author': 'Mark Renouf', 'author_id': 758, 'author_profile': 'https://Stackoverflow.com/users/758', 'pm_score': 7, 'selected': False, 'text': '<p>There are no standards for REST other than HTTP. There are established REST services out there. I suggest you take a peek at them and get a feel for how they work. </p>\n\n<p>For example, we borrowed a lot of ideas from Amazon\'s S3 REST service when developing our own. But we opted not to use the more advanced security model based on request signatures. The simpler approach is HTTP Basic auth over SSL. You have to decide what works best in your situation.</p>\n\n<p>Also, I highly recommend the book <a href="http://oreilly.com/catalog/9780596529260/" rel="noreferrer">RESTful Web Services</a> from O\'reilly. It explains the core concepts and does provide some best practices. You can generally take the model they provide and map it to your own application.</p>\n'}, {'answer_id': 7618, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 9, 'selected': True, 'text': "<p>As tweakt said, Amazon S3 is a good model to work with. Their request signatures do have some features (such as incorporating a timestamp) that help guard against both accidental and malicious request replaying.</p>\n\n<p>The nice thing about HTTP Basic is that virtually all HTTP libraries support it. You will, of course, need to require SSL in this case because sending plaintext passwords over the net is almost universally a bad thing. Basic is preferable to Digest when using SSL because even if the caller already knows that credentials are required, Digest requires an extra roundtrip to exchange the nonce value. With Basic, the callers simply sends the credentials the first time.</p>\n\n<p>Once the identity of the client is established, authorization is really just an implementation problem. However, you could delegate the authorization to some other component with an existing authorization model. Again the nice thing about Basic here is your server ends up with a plaintext copy of the client's password that you can simply pass on to another component within your infrastructure as needed.</p>\n"}, {'answer_id': 87293, 'author': 'Nathan', 'author_id': 541, 'author_profile': 'https://Stackoverflow.com/users/541', 'pm_score': 4, 'selected': False, 'text': '<p>Thanks for the excellent advice. We ended up using a custom HTTP header to pass an identity token from the client to the service, in preparation for integrating our RESTful API with the the upcoming Zermatt Identity framework from Microsoft. I have described the problem <a href="http://www.thefreakparade.com/2008/09/flowing-identity-from-a-client-to-a-service-when-using-restful-wcf-part-1-the-problem/" rel="nofollow noreferrer">here</a> and our solution <a href="http://www.thefreakparade.com/2008/09/flowing-identity-from-a-client-to-a-service-when-using-restful-wcf-part-2-a-solution/" rel="nofollow noreferrer">here</a>. I also took <a href="https://stackoverflow.com/questions/7551/best-practices-for-securing-a-rest-api-web-service#7562">tweakt</a>\'s advice and bought <a href="http://oreilly.com/catalog/9780596529260/" rel="nofollow noreferrer">RESTful Web Services</a> - a very good book if you\'re building a RESTful API of any kind.</p>\n'}, {'answer_id': 89600, 'author': 'John Spurlock', 'author_id': 3054, 'author_profile': 'https://Stackoverflow.com/users/3054', 'pm_score': 6, 'selected': False, 'text': '<p>You may also want to take a look at <a href="http://oauth.net/" rel="noreferrer">OAuth</a>, an emerging open protocol for token-based authorization specifically targeting http apis.</p>\n\n<p>It is very similar to the approach taken by <a href="http://www.flickr.com/services/api/" rel="noreferrer">flickr</a> and <a href="http://www.rememberthemilk.com/services/api/" rel="noreferrer">remember the milk</a> "rest" apis (not necessarily good examples of restful apis, but good examples of the token-based approach).</p>\n'}, {'answer_id': 96833, 'author': 'degnome', 'author_id': 18218, 'author_profile': 'https://Stackoverflow.com/users/18218', 'pm_score': 4, 'selected': False, 'text': '<p>One of the best posts I\'ve ever come across regarding Security as it relates to REST is over at <a href="http://1raindrop.typepad.com/1_raindrop/2006/12/rest_security_o.html" rel="noreferrer">1 RainDrop</a>.  The MySpace API\'s use OAuth also for security and you have full access to their custom channels in the RestChess code, which I did a lot of exploration with. This was demo\'d at Mix and you can find the posting <a href="http://blogs.msdn.com/vbertocci/archive/2008/03/10/wcf-rest-at-mix08-the-tale-of-myspace-apis.aspx" rel="noreferrer">here</a>. </p>\n'}, {'answer_id': 429803, 'author': 'Rob Ottaway', 'author_id': 53517, 'author_profile': 'https://Stackoverflow.com/users/53517', 'pm_score': 5, 'selected': False, 'text': '<p>I\'ve used OAuth a few times, and also used some other methods (BASIC/DIGEST). I wholeheartedly suggest OAuth. The following link is the best tutorial I\'ve seen on using OAuth:</p>\n\n<p><a href="http://hueniverse.com/oauth/guide/" rel="noreferrer">http://hueniverse.com/oauth/guide/</a></p>\n'}, {'answer_id': 1479210, 'author': 'stinkymatt', 'author_id': 72676, 'author_profile': 'https://Stackoverflow.com/users/72676', 'pm_score': 5, 'selected': False, 'text': "<p>I'm kind of surprised SSL with client certificates hasn't been mentioned yet. Granted, this approach is only really useful if you can count on the community of users being identified by certificates.  But a number of governments/companies do issue them to their users.  The user doesn't have to worry about creating yet another username/password combination, and the identity is established on each and every connection so communication with the server can be entirely stateless, no user sessions required.  (Not to imply that any/all of the other solutions mentioned require sessions)</p>\n"}, {'answer_id': 10598925, 'author': 'Parisa Kachoui', 'author_id': 1395913, 'author_profile': 'https://Stackoverflow.com/users/1395913', 'pm_score': 3, 'selected': False, 'text': '<p>I searched a lot about restful ws security and we also ended up with using token via cookie from client to server to authenticate the requests . I  used spring security for authorization of requests in service because I had to authenticate and authorized each request based on specified security policies that has already been in DB.  </p>\n'}, {'answer_id': 12854984, 'author': 'Robert Morschel', 'author_id': 1566117, 'author_profile': 'https://Stackoverflow.com/users/1566117', 'pm_score': 2, 'selected': False, 'text': '<p>REST itself offers no security standards, but things like OAuth and SAML are rapidly becoming the standards in this space.  However, authentication and authorization are only a small part of what you need to consider.  Many of the known vulnerabilities relating to web applications apply very much to REST apis.  You have to consider input validation, session cracking, inappropriate error messages, internal employee vulnerabilities and so on.  It is a big subject.</p>\n'}, {'answer_id': 15399365, 'author': 'Abhijit Gaikwad', 'author_id': 403872, 'author_profile': 'https://Stackoverflow.com/users/403872', 'pm_score': 3, 'selected': False, 'text': '<p>I would recommend OAuth 2/3. You can find more information at <a href="http://oauth.net/2/" rel="noreferrer">http://oauth.net/2/</a></p>\n'}, {'answer_id': 18993123, 'author': 'David Brossard', 'author_id': 1021725, 'author_profile': 'https://Stackoverflow.com/users/1021725', 'pm_score': 5, 'selected': False, 'text': '<p>Everyone in these answers has overlooked true access control / authorization.</p>\n\n<p>If for instance your REST APIs / web services are about POSTing / GETing medical records, you may want to define access control policie about who can access the data and under which circumstances. For instance:</p>\n\n<ul>\n<li>doctors can GET the medical record of a patient they have a care relationship with</li>\n<li>no one can POST medical data outside practice hours (e.g. 9 to 5)</li>\n<li>end-users can GET medical records they own or medical records of patients for whom they are the guardian</li>\n<li>nurses can UPDATE the medical record of a patient that belongs to the same unit as the nurse.</li>\n</ul>\n\n<p>In order to define and implement those fine-grained authorizations, you will need to use an attribute-based access control language called XACML, the eXtensible Access Control Markup Language.</p>\n\n<p>The other standards here are for the following:</p>\n\n<ul>\n<li>OAuth: id. federation and delegation of authorization e.g. letting a service act on my behalf on another service (Facebook can post to my Twitter)</li>\n<li>SAML: identity federation / web SSO. SAML is very much about who the user is.</li>\n<li>WS-Security / WS-* standards: these focus on the communication between SOAP services. They are specific to the application-level messaging format (SOAP) and they deal with aspects of messaging e.g. reliability, security, confidentiality, integrity, atomicity, eventing... None cover access control and all are specific to SOAP.</li>\n</ul>\n\n<p>XACML is technology-agnostic. It can be applied to java apps, .NET, Python, Ruby... web services, REST APIs, and more.</p>\n\n<p>The following are interesting resources:</p>\n\n<ul>\n<li>the OASIS XACML <a href="http://oasis-open.org/committees/tc_home.php?wg_abbrev=xacml">website</a></li>\n<li>the <a href="http://csrc.nist.gov/projects/abac/">NIST ABAC standard</a></li>\n</ul>\n'}, {'answer_id': 20123366, 'author': 'kravietz', 'author_id': 1274149, 'author_profile': 'https://Stackoverflow.com/users/1274149', 'pm_score': 3, 'selected': False, 'text': '<p>The fact that the SOAP world is pretty well covered with security standards doesn\'t mean that it\'s secure by default. In the first place, the standards are <strong>very</strong> complex. Complexity is not a very good friend of security and implementation vulnerabilities such as <a href="https://ipsec.pl/node/1119" rel="noreferrer">XML signature wrapping attacks</a> are endemic here. </p>\n\n<p>As for the .NET environment I won\'t help much, but <a href="http://amzn.to/1auRwqA" rel="noreferrer">“Building web services with Java”</a> (a brick with ~10 authors) did help me <em>a lot</em> in understanding the WS-* security architecture and, especially, its quirks.</p>\n'}, {'answer_id': 21996220, 'author': 'WelsonJR', 'author_id': 3033698, 'author_profile': 'https://Stackoverflow.com/users/3033698', 'pm_score': 4, 'selected': False, 'text': '<p>OWASP(Open Web Application Security Project) has some cheat sheets covering about all aspects of Web Application development. This Project is a very valuable and reliable source of information.\nRegarding REST services you can check this: <a href="https://www.owasp.org/index.php/REST_Security_Cheat_Sheet">https://www.owasp.org/index.php/REST_Security_Cheat_Sheet</a></p>\n'}, {'answer_id': 22108726, 'author': 'Manish Jain', 'author_id': 486867, 'author_profile': 'https://Stackoverflow.com/users/486867', 'pm_score': 2, 'selected': False, 'text': '<p>I want to add(in line with stinkeymatt), simplest solution would be to add SSL certificates to your site. In other words, make sure your url is HTTPS://. That will cover your transport security (bang for the buck). With RESTful url\'s, idea is to keep it simple (unlike WS* security/SAML), you can use <a href="https://github.com/thinktecture/Thinktecture.AuthorizationServer" rel="nofollow">oAuth2/openID connect</a> or even Basic Auth (in simple cases). But you will still need SSL/HTTPS. Please check ASP.NET Web API 2 security here: <a href="http://www.asp.net/web-api/overview/security" rel="nofollow">http://www.asp.net/web-api/overview/security</a> (Articles and Videos)</p>\n'}, {'answer_id': 24807288, 'author': 'Archimedes Trajano', 'author_id': 242042, 'author_profile': 'https://Stackoverflow.com/users/242042', 'pm_score': 2, 'selected': False, 'text': "<p>As @Nathan ended up with which is a simple HTTP Header, and some had said OAuth2 and client side SSL certificates.  The gist of it is this... your REST API shouldn't have to handle security as that should really be outside the scope of the API.</p>\n\n<p>Instead a security layer should be put on top of it, whether it is an HTTP Header behind a web proxy (a common approach like SiteMinder, Zermatt or even Apache HTTPd), or as complicated as OAuth 2.</p>\n\n<p>The key thing is the requests should work without any end-user interaction.  All that is needed is to ensure that the connection to the REST API is authenticated.  In Java EE we have the notion of a <code>userPrincipal</code> that can be obtained on an <code>HttpServletRequest</code>.  It is also managed in the deployment descriptor that a URL pattern can be secure so the REST API code does not need to check anymore.</p>\n\n<p>In the WCF world, I would use <code>ServiceSecurityContext.Current</code> to get the current security context.  You need to configure you application to require authentication.</p>\n\n<p>There is one exception to the statement I had above and that's the use of a nonce to prevent replays (which can be attacks or someone just submitting the same data twice).  That part can only be handled in the application layer.</p>\n"}, {'answer_id': 26210742, 'author': 'java_geek', 'author_id': 268850, 'author_profile': 'https://Stackoverflow.com/users/268850', 'pm_score': 2, 'selected': False, 'text': '<p>For Web Application Security, you should take a look at OWASP (<a href="https://www.owasp.org/index.php/Main_Page" rel="nofollow">https://www.owasp.org/index.php/Main_Page</a>) which provides cheatsheets for various security attacks. You can incorporate as many measures as possible to secure your Application. \nWith respect to API security (authorization, authentication, identity management), there are multiple ways as already mentioned (Basic,Digest and OAuth). There are loop holes in OAuth1.0, so you can use OAuth1.0a (OAuth2.0 is not widely adopted due to concerns with the specification)</p>\n'}, {'answer_id': 45644262, 'author': 'Matt Bannert', 'author_id': 366256, 'author_profile': 'https://Stackoverflow.com/users/366256', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s been a while but the question is still relevant, though the answer might have changed a bit. </p>\n\n<p>An API Gateway would be a flexible and highly configurable solution.\nI tested and used <a href="http://getkong.org" rel="nofollow noreferrer">KONG</a> quite a bit and really liked what I saw. KONG provides an admin REST API of its own which you can use to manage users. </p>\n\n<p><a href="http://express-gateway.io" rel="nofollow noreferrer">Express-gateway.io</a> is more recent and is also an API Gateway.</p>\n'}, {'answer_id': 47189047, 'author': 'Andrejs', 'author_id': 2786733, 'author_profile': 'https://Stackoverflow.com/users/2786733', 'pm_score': 6, 'selected': False, 'text': '<p>There is a great checklist found on <a href="https://github.com/shieldfy/API-Security-Checklist" rel="noreferrer">Github</a>:</p>\n\n<p><strong>Authentication</strong></p>\n\n<ul>\n<li><p>Don\'t reinvent the wheel in Authentication, token generation, password storage. Use the standards.</p></li>\n<li><p>Use <code>Max Retry</code> and jail features in Login.</p></li>\n<li><p>Use encryption on all sensitive data.</p></li>\n</ul>\n\n<p><strong>JWT (JSON Web Token)</strong></p>\n\n<ul>\n<li><p>Use a random complicated key (JWT Secret) to make brute forcing the token very hard.</p></li>\n<li><p>Don\'t extract the algorithm from the payload. Force the algorithm in the backend (HS256 or RS256).</p></li>\n<li><p>Make token expiration (<code>TTL</code>, <code>RTTL</code>) as short as possible.</p></li>\n<li><p>Don\'t store sensitive data in the <code>JWT</code> payload, it can be decoded easily.</p></li>\n</ul>\n\n<p><strong>OAuth</strong></p>\n\n<ul>\n<li><p>Always validate <code>redirect_uri</code> server-side to allow only whitelisted URLs.</p></li>\n<li><p>Always try to exchange for code and not tokens (don\'t allow <code>response_type=token</code>).</p></li>\n<li><p>Use state parameter with a random hash to prevent <code>CSRF</code> on the <code>OAuth</code> authentication process.</p></li>\n<li><p>Define the default scope, and validate scope parameters for each application.</p></li>\n</ul>\n\n<p><strong>Access</strong></p>\n\n<ul>\n<li><p>Limit requests (Throttling) to avoid DDoS / brute-force attacks. </p></li>\n<li><p>Use HTTPS on server side to avoid MITM (Man In The Middle Attack)</p></li>\n<li><p>Use <code>HSTS</code> header with SSL to avoid SSL Strip attack.</p></li>\n</ul>\n\n<p><strong>Input</strong> </p>\n\n<ul>\n<li><p>Use the proper HTTP method according to the operation: <code>GET</code> (read), <code>POST</code> (create), <code>PUT/PATCH</code> (replace/update), and <code>DELETE</code> (to delete a record), and respond with <code>405 Method Not Allowed</code> if the requested method isn\'t appropriate for the requested resource.</p></li>\n<li><p>Validate content-type on request <code>Accept</code> header (Content Negotiation) to allow only your supported format (e.g. <code>application/xml</code>, <code>application/json</code>, etc) and respond with <code>406 Not Acceptable</code> response if not matched.</p></li>\n<li><p>Validate <code>content-type</code> of posted data as you accept (e.g. <code>application/x-www-form-urlencoded</code>, <code>multipart/form-data</code>, <code>application/json</code>, etc).</p></li>\n<li><p>Validate User input to avoid common vulnerabilities (e.g. XSS, SQL-Injection, Remote Code Execution, etc).</p></li>\n<li><p>Don\'t use any sensitive data (credentials, Passwords, security tokens, or API keys) in the URL, but use standard <code>Authorization</code> header.</p></li>\n<li><p>Use an API Gateway service to enable caching, <code>Rate Limit</code> policies (e.g. Quota, Spike Arrest, Concurrent Rate Limit) and deploy APIs resources dynamically.</p></li>\n</ul>\n\n<p><strong>Processing</strong></p>\n\n<ul>\n<li><p>Check if all the endpoints are protected behind authentication to avoid broken authentication process.</p></li>\n<li><p>User own resource ID should be avoided. Use /me/orders instead of /user/654321/orders.</p></li>\n<li><p>Don\'t auto-increment IDs. Use UUID instead.</p></li>\n<li><p>If you are parsing XML files, make sure entity parsing is not enabled to avoid XXE (XML external entity attack).</p></li>\n<li><p>If you are parsing XML files, make sure entity expansion is not enabled to avoid Billion Laughs/XML bomb via exponential entity expansion attack.</p></li>\n<li><p>Use a CDN for file uploads.</p></li>\n<li><p>If you are dealing with huge amount of data, use Workers and Queues to process as much as possible in background and return response fast to avoid HTTP Blocking.</p></li>\n<li><p>Do not forget to turn the <strong>DEBUG</strong> mode OFF.</p></li>\n</ul>\n\n<p><strong>Output</strong></p>\n\n<ul>\n<li><p>Send <code>X-Content-Type-Options: nosniff</code> header.</p></li>\n<li><p>Send <code>X-Frame-Options: deny</code> header.</p></li>\n<li><p>Send <code>Content-Security-Policy: default-src \'none\'</code> header.</p></li>\n<li><p>Remove fingerprinting headers - <code>X-Powered-By</code>, <code>Server</code>, <code>X-AspNet-Version</code> etc.</p></li>\n<li><p>Force <code>content-type</code> for your response, if you return <code>application/json</code> then your response content-type is <code>application/json</code>.</p></li>\n<li><p>Don\'t return sensitive data like credentials, Passwords, security tokens.</p></li>\n<li><p>Return the proper status code according to the operation completed. (e.g. <code>200 OK</code>, <code>400 Bad Request</code>, <code>401 Unauthorized</code>, <code>405 Method Not Allowed</code>, etc).</p></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/7551', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/541/']