Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This document contains guidelines that will help you create and consume APIs in a secure way.

The guidelines are inspired by the document "OWASP cheat for REST Security" which you can find here: https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html.

General guidelines for securing REST based APIs

Info

Secure all endpoints using HTTPS

Among other things, this will protect Access Tokens that are transferred between HelseID and the API-klient, as well as between API-clients and API endpoints.

Info

Perform access control on every endpoint

Even if the API is hidden behind an API Gateway.

Info

Require JWTs as security tokens

Info

Restrict which HTTP methods that are used

Reject every HTTP methods that is not in use by responding with the HTTP status code 405 - "method not allowed"

Info

Validate every input parameter

  • Never trust input parameters, take a look at the OWASP Input Validation cheat sheet for detailed explanations

  • Validate the lenght of the input-value, valid ranges and values, format and type

  • All input parameters should be strongly typed

  • Do not accept unexpected or unknown content

  • Use libraries or framworks for validating and sanitizing input values

  • Define limits for data size in requests and reject requests that are too big by responding with the HTTP status code 413 - "Request Entity Too Large"

  • Log errors that occur during input validation, consider implementing rules that temporarily ban API-clients that often fail.

Info

Validate "content types"

If you don’t validate "content type" you open up for injecting and execution of code

  • Validate "content types" for incoming requests to your API

    • Reject requests that lack "content type", or contain unexpected "content type" values.
      Respond to such requests with HTTP status codes:

      • 406 - "Unacceptable"

      • 415 - "Unsupported Media Type"

    • Avoid exposing content types not in use by defining explicit content types.
      By doing this you can avoid XXE (XML External Entity) attacks.

  • Use safe content types in responses

    • DON'T copy the "Accept" header to the "Content-type" headeren of the response

    • Do not accept the request if the "Accept" header doesn’t contain type that is not allowed.
      Reply with HTTP status code 406 - "Not Acceptable" if the type stated in the "Accept" header is not allowed.

    • Make sure "content type" headers in your responses stemmer overens  med innholdet i body.- F.eks application/json og ikke application/javascript

Info

Security headers

  • Send "Content-Type" headeren med riktig content type og charset.

  • Send sikkerhetsheaderen "X-Content-Type-Options: nosniff" for å sikre at nettleseren ikke forsøker å sette en annen Content-Type enn det som faktisk ble sendt (dette kan føre til XSS).

  • Send sikkerhetsheaderen "X-Frame-Options: deny" for å beskytte mot "drag'n drop clickjacking" angrep i eldre nettlesere

Info

Riktig bruk av CORS.
Ved å levere passende CORS headere signaliserer REST APIet ditt hvilke domener (origins) som har lov til å gjøre JavaScript kall til REST-tjenesten

  • Deaktiver CORS headere hvis kall på tvers av domener ikke støttes eller forventes

  • Vær så spesifikk som mulig, og så generell som nødvendig når du definerer hvilke origins som er gyldige for kall på tvers av domener

Info

Make sure your APIs always sends correct HTTP response codes

Note

Avoid the exposure of enpoints for administration on the internet

...

Be careful when handling errors

...

Reply with generic error messages - avoid exposing details about the error if its not neccessary

...

page has been moved to this location (Norwegian), and this location (English).