Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicholasLeader/3fccbfee88ae0d3ff747ca1a8f6bd8a7 to your computer and use it in GitHub Desktop.
Save NicholasLeader/3fccbfee88ae0d3ff747ca1a8f6bd8a7 to your computer and use it in GitHub Desktop.
This Python AWS Lamda function, returns True / False if a given AWS API query parameter (URL) has an HTTP security CSP header present
### Nicholas Leader
### Python PoC given AWS API query parameter of URL, return if HTTP security header CSP policy is present
### Written as AWS Lamda (serverless) function
### 8.14.2019
###
import json
def lambda_handler(event, context):
import urllib.request
input = event["queryStringParameters"]['URL']
req = urllib.request.Request(input, #test URLs 'https://securityheaders.com'#securityHeaders has the CSP, twitter doesn't
data=None,
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'} )
f = urllib.request.urlopen(req)
Headerdict = dict(f.info()) # add the header meta data to a dictionary
# print( Headerdict ['Content-Security-Policy']) # print contents of hashtable - dictionary entry
try: # needed to validate if statement, otherwise will exit with error
if Headerdict['Content-Security-Policy']:
#print ("CSP present")
Headerpresent = "True"
except:
# print('CSP not present')
Headerpresent = "False"
return {
'statusCode': 200,
'body': json.dumps(Headerpresent) # if you want to return the whole header: Headerdict['Content-Security-Policy'])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment