PythonIntermediate · Windows, macOS, or Linux; Python 3.12+

Review cookie attributes without values

Inspect Set-Cookie attribute presence from one HEAD response while omitting cookie values.

Before you begin

  • Python 3.12 or later.
  • A reachable endpoint you administer that supports HEAD.
What this script changesOne HEAD request with no authentication and no stored cookie jar. Cookie values are not printed, persisted, or resent. Redirects can contact redirected hosts.

Set up your example

  1. Save as security-cookie-attributes.py.
  2. Open a terminal in its folder.
  3. Run python "security-cookie-attributes.py" --help and prepare the listed input files.

This recipe uses your own local input or creates its own example output. Follow the setup steps below.

Download the ZIP, extract it, and open a terminal inside the extracted folder. The source file is security-cookie-attributes.py.

Run the script

Run command
python "security-cookie-attributes.py" "http://127.0.0.1:8000/"

If your system uses python3 instead of python, use that command for Python examples. Keep paths containing spaces in quotes. Required inputs and optional settings are described in the code and setup steps.

What to expect

A session cookie can show secure=true and httponly=true; its value is never printed.

The complete script

security-cookie-attributes.py
#!/usr/bin/env python3
"""Inspect Set-Cookie attribute presence from one HEAD response while omitting cookie values."""
import argparse
import sys
import json
from pathlib import Path
from urllib.request import Request,urlopen
from urllib.parse import urlsplit
from urllib.error import HTTPError

def head(url,extra=None):
    p=urlsplit(url)
    if p.scheme not in {'http','https'} or not p.hostname or p.username is not None or p.password is not None:raise ValueError('Use an HTTP(S) URL without credentials')
    request=Request(url,method='HEAD',headers={'User-Agent':'Scriptage-local-check/1.0',**(extra or {})})
    try:return urlopen(request,timeout=10)
    except HTTPError as response:return response

from http.cookies import SimpleCookie,CookieError

def main():
    parser = argparse.ArgumentParser(description='Inspect Set-Cookie attribute presence from one HEAD response while omitting cookie values.')
    parser.add_argument('url')
    args = parser.parse_args()
    with head(args.url) as response:
        rows=[]
        for raw in response.headers.get_all('Set-Cookie',[]):
            cookie=SimpleCookie()
            try:cookie.load(raw)
            except CookieError:rows.append({'parse_error':True});continue
            for name,morsel in cookie.items():rows.append({'name':name,'secure':bool(morsel['secure']),'httponly':bool(morsel['httponly']),'samesite':morsel['samesite'] or None})
        print(json.dumps(rows,indent=2))

if __name__ == '__main__':
    try:
        main()
    except (OSError, ValueError, RuntimeError) as exc:
        print(f'Error: {exc}', file=sys.stderr)
        sys.exit(1)

Open source as plain text · Download source and sample files

How it works

  1. Inspect Set-Cookie attribute presence from one HEAD response while omitting cookie values.
  2. Arguments are explicit and expected input errors produce a message with a nonzero exit code.
  3. Results reflect your selected local data or the specific endpoint you choose; the example illustrates the result shape.

Make it your own

Adapt the task

Adjust the attribute keys in rows for your review. Some servers set cookies only on GET or authenticated responses, which this check does not request.

Adjust presentation

Edit final print statements or json.dumps indentation to adjust the report layout. Keep input validation and exclusive-create file modes intact.

Change one setting at a time, run it again on sample data, and compare the output. The customization guide walks through this process.

If something goes wrong

An input or dependency is unavailable.

A reachable endpoint you administer that supports HEAD.

Actual results differ from the example.

Inputs and system state determine values. Review each finding in context; a report is not proof of system health or a complete security assessment.

Reference & next steps

These official references explain the underlying tools. The Scriptage example and guide are original instructional content. See verification notes for what has been checked and the limits of those checks.

Copies a template for your own notes. It does not send anything.