@adam_englander
Practical API Security
Adam Englander, Software Architect
iovation
@adam_englander
Let's set some expectations...
@adam_englander
What are we protecting against?
@adam_englander
@adam_englander
1: Injection
@adam_englander
2: Broken Authentication
@adam_englander
3: Sensitive Data Exposure
@adam_englander
4: XML External Entities (XXE)
@adam_englander
libxml_disable_entity_loader(true);
@adam_englander
Use defusedxml and
defusedexpat to protect lib.xml
@adam_englander
5: Broken Access Control
@adam_englander
6: Security Misconfiguration
@adam_englander
7: Cross-Site Scripting XSS
@adam_englander
8: Insecure Deserialization
@adam_englander
9: Using Components with Known
Vulnerabilities
@adam_englander
PHP Resources
• https://security.sensiolabs.org/
• roave/security-advisories package
• https://github.com/FriendsOfPHP/security-
advisories
@adam_englander
10: Insufficient Logging &
Monitoring
@adam_englander
How do we provide that
protection?
@adam_englander
@adam_englander
Defense in Depth
Transport Layer Security
Rate Limiting/Replay Prevention
Authentication
Data Validation
Data Encryption
Logging
Access Control
@adam_englander
Defense in Depth
Transport Layer Security
Rate Limiting/Replay Prevention
Authentication
Data Validation
Data Encryption
Logging
Access Control
@adam_englander
Defense in Depth
Transport Layer Security
Rate Limiting/Replay Prevention
Authentication
Data Validation
Data Encryption
Logging
Access Control
@adam_englander
@adam_englander
@adam_englander
Defense in Depth
Transport Layer Security
Rate Limiting/Replay Prevention
Authentication
Data Validation
Data Encryption
Logging
Access Control
@adam_englander
Replay prevention requires
unique requests
@adam_englander
Determine Uniqueness of Request
GET / HTTP/1.1
Accept: application/json
@adam_englander
Determine Uniqueness of Request
GET / HTTP/1.1
Accept: application/json
X-Nonce: 5ed518e8c5c51a64638b2b50c192242d
@adam_englander
Store that unique value in a
datastore so you can verify you
don't see it again
@adam_englander
Use the add function on the
cache to prevent race conditions
@adam_englander
Cache Example
if ($token === null) {
throw new AuthorizationRequiredException();
} elseif (!$this->cache->add(hash('sha512', $token), 1, 10)) {
throw new InvalidRequestException();
}
@adam_englander
Use insert on unique index for
RDBMS to prevent race
conditions
@adam_englander
Rate limiting requires unique
identification for restrictions
@adam_englander
api-user-id|create-widget|20:01
ebf4e1d4bb33e5f6028e8443d6a1d6aa
@adam_englander
Use the add and increment
functions of the cache to
prevent race conditions
@adam_englander
Cache Example
$key = sprintf("%s|root-post|%s", $userId, $timeSlice);
$this->cache->add($key, 0, 1);
$total = $this->cache->increment($key);
@adam_englander
Use insert with unique index
and update returning in RDBMS
to prevent race conditions
@adam_englander
Data stores can be done in
three ways.
@adam_englander
In Memory Datastore
@adam_englander
Local Datastore
@adam_englander
Global Datastore
@adam_englander
Defense in Depth
Transport Layer Security
Rate Limiting/Replay Prevention
Authentication
Data Validation
Data Encryption
Logging
Access Control
@adam_englander
Do not make authentication part
of the body
@adam_englander
Use the Authorization header
@adam_englander
HTTP Basic Authentication
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
@adam_englander
HTTP Digest Authentication
DO NOT USE!
@adam_englander
HTTP Bearer Authentication
Authorization: Bearer mF_9.B5f-4.1JqM
@adam_englander
Roll Your Own
@adam_englander
Many APIs do this
@adam_englander
What about never rolling your
own crypto?
@adam_englander
Single Use JWT
@adam_englander
No auth service required
@adam_englander
Can use existing JWT libraries
to create and validate
@adam_englander
Can be extended beyond auth
to provide data validation and
MITM protection
@adam_englander
Defense in Depth
Transport Layer Security
Rate Limiting/Replay Prevention
Authentication
Data Validation
Data Encryption
Logging
Access Control
@adam_englander
Message Validation
@adam_englander
Request Validation
@adam_englander
Method Validation
GET /user/abc HTTP/1.1
Accept: application/json
@adam_englander
Method Validation
DELETE /user/abc HTTP/1.1
Accept: application/json
@adam_englander
Path Validation
GET /user/abc HTTP/1.1
Accept: application/json
@adam_englander
Path Validation
GET /user/def HTTP/1.1
Accept: application/json
@adam_englander
Body Validation
PATCH /user/abc HTTP/1.1
{"email": "valid@user.com"}
@adam_englander
Body Validation
PATCH /user/abc HTTP/1.1
{"email": "pwned@hkr.com"}
@adam_englander
Response Validation
@adam_englander
Status Code Validation
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 21
{"expected": "value"}
@adam_englander
Status Code Validation
HTTP/1.1 400 Invalid Request
Content-Type: application/json; charset=UTF-8
Content-Length: 21
{"expected": "value"}
@adam_englander
Status Code Validation
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 21
{"expected": "value"}
@adam_englander
Status Code Validation
HTTP/1.1 301 Moved
Content-Type: application/json; charset=UTF-8
Content-Length: 21
Location: https://bad.actor.com
{"expected": "value"}
@adam_englander
Header Validation
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 21
Cache-Control: no-cache
{"expected": "value"}
@adam_englander
Header Validation
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 21
Cache-Control: max-age=99999999
{"expected": "value"}
@adam_englander
Data Validation
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 21
{"active": false}
@adam_englander
Data Validation
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 21
{"active": true}
@adam_englander
Validation of request data
@adam_englander
Defense in Depth
Transport Layer Security
Rate Limiting/Replay Prevention
Authentication
Data Validation
Data Encryption
Logging
Access Control
@adam_englander
Encrypt Data at Rest
@adam_englander
Use a structure format that
allows for in-place key rotation
and nonce storage
@adam_englander
COSE
CBOR Object Signing and Encryption (COSE)
Concise Binary Object Representation (CBOR)
@adam_englander
Roll Your Own
keyid|nonce|encrypted-data
@adam_englander
Encrypt Data in Transit
@adam_englander
WW?D
@adam_englander
JSON Web Encryption
@adam_englander
Log Everything
@adam_englander
Log in a structured format for
easier parsing
@adam_englander
Log all pertinent actions
@adam_englander
Include all data regarding state.
Anonymize sensitive data.
@adam_englander
Include origin data to identify
bad actors.
@adam_englander
Utilize tools like ELK or Greylog
to aggregate logs
@adam_englander
Determine anomalous conditions
and alert on those conditions.
@adam_englander
And now a demonstration...
@adam_englander
https://joind.in/talk/9895f

Practical API Security - Midwest PHP 2018