Reference Manual | Prev: Chapter 7. HTML Generation | Next: Chapter 9. Data Caching and Performance
CL-HTTP provides a comprehensive, object-oriented authentication and access control system that governs who can access which URLs and what HTTP methods they may invoke. The system is built around five core concepts:
http:realm.
http:user.
http:group.
http:access-control.
(METHOD &rest GROUPS-AND-USERS),
where METHOD is an HTTP method keyword (:GET,
:HEAD, :POST, :PUT,
:DELETE, :OPTIONS, :TRACE) or
:DEFAULT. Groups are specified by keyword, users by string.
CL-HTTP provides two independent mechanisms for controlling access:
:secure-subnets, :read-subnets, and
:write-subnets arguments to
http:export-url.
:authentication-realm and
:capabilities arguments to http:export-url.
Both mechanisms can be combined for defense in depth. For example, digest authentication can be restricted to clients on trusted subnets, providing both identity verification and network-level filtering.
The authentication scheme is determined by the realm type. CL-HTTP supports the following schemes:
| Scheme Keyword | Realm Class | Security Level |
|---|---|---|
:basic |
basic-realm |
Weak — Base64 encoding only. Useful because it is universally deployed in clients, but provides no cryptographic security. |
:digest |
digest-realm |
Moderate — MD5 hash. Cracked and deemed insecure since 2004.
Use :digest-sha-256 instead. |
:digest-sha |
digest-sha-realm |
Moderate — SHA-1 160-bit hash. NIST recommended phase-out in 2010. |
:digest-sha-256 |
digest-sha-256-realm |
Strong — SHA-256. Recommended for all new digest authentication. |
:digest-sha-384 |
digest-sha-384-realm |
Strong — SHA-384. |
:digest-sha-512 |
digest-sha-512-realm |
Strong — SHA-512. |
:certificate |
certificate-realm |
Strong — X.509 client certificates via SSL/TLS. Requires
:cl-http-ssl-client feature. |
The scheme-to-class mapping is maintained in
http:*realm-scheme-class-alist* and can be extended via
http:add-realm-scheme to register application-specific
realm classes.
Each authentication scheme defines a complete set of CLOS classes for its realm, users, groups, and access controls. The class hierarchy uses mixins to separate mechanism from policy:
realm
standard-realm
basic-realm ; :basic scheme
digest-realm ; :digest (MD5) scheme
digest-sha-realm ; :digest-sha (SHA-1) scheme
digest-sha-256-realm ; :digest-sha-256 scheme
digest-sha-384-realm ; :digest-sha-384 scheme
digest-sha-512-realm ; :digest-sha-512 scheme
certificate-realm ; :certificate scheme
user
standard-user
basic-user
digest-user
certificate-user
group
standard-group
basic-group
digest-group
certificate-group
access-control
standard-access-control
basic-access-control
digest-access-control
certificate-access-control
standard-url-access-control
basic-url-access-control
digest-url-access-control
certificate-url-access-control
When a realm is created, it automatically uses the correct user, group,
and access control classes for its scheme. For example, a
basic-realm creates basic-user,
basic-group, and basic-access-control objects.
The class selection is controlled by the class-allocated slots
realm-user-class, realm-group-class, and
realm-access-control-class.
Subnet-based access control restricts URL access based on the client's IP address. It operates independently of user authentication and is evaluated first — a client must pass subnet restrictions before any password challenge is issued.
The variable http:*secure-subnets* holds a list of trusted
subnet specifications. When set, it provides a global default for all URLs
that do not specify their own subnet restrictions.
;; Trust only the local class C subnet
(setq http:*secure-subnets* '("192.168.1.0"))
;; Trust multiple subnets
(setq http:*secure-subnets* '("192.168.1.0" "10.0.0.0"))
In subnet specifications, a zero component acts as a wildcard. For
example, "192.168.0.0" matches any address starting with
192.168.
The :secure-subnets keyword on http:export-url
restricts read access (GET, HEAD) to clients on specified subnets:
;; Only allow access from the local network
(http:export-url #u"/cl-http/sources/server/"
:directory
:pathname "http:server;"
:recursive-p t
:secure-subnets '("192.168.1.0")
:keywords '(:cl-http :documentation))
For finer control over read vs. write access, use
:read-subnets and :write-subnets separately:
;; Allow anyone to read, but only the local host can write
(http:export-url #u"/cl-http/users/"
:directory
:pathname "http:www;cl-http;users;"
:recursive-p t
:read-subnets nil ; no read restriction
:write-subnets (list (http:local-host-ip-address))
:keywords '(:cl-http :documentation))
A common pattern restricts access to the machine running the server:
;; Only accessible from localhost
(http:export-url #u"/cl-http/maintenance/configure-server.html"
:computed
:response-function #'write-configure-server-form
:secure-subnets (list (http:local-host-ip-address))
:private t
:proxy-revalidate t)
The predicate http:ip-subnet-match-p tests whether a
host address falls within a given subnet list. It replaces the older
name ip-host-trusted-p; the meaning is unchanged. A NIL
subnet list now signals an error rather than returning silently —
callers must pass an explicit list of allowed subnets (or use
http:*secure-subnets* as a fallback).
http:trusted-host-p consults the global secure-subnets
list and respects this stricter contract.
;; Permit one /24 plus the local host
(http:ip-subnet-match-p (http:client-host *client*)
(list "192.168.1.0/24"
(http:local-host-ip-address)))
Basic authentication (RFC 2617) is the simplest HTTP authentication scheme. The client sends the username and password encoded in Base64. Because Base64 is trivially reversible, basic authentication provides no cryptographic security. It is useful only because it is universally supported by web clients.
Security note: Basic authentication should be combined with subnet security or used only over SSL/TLS connections. On its own, it merely keeps honest people honest.
The setup sequence is: create a realm, create groups, add users to groups, define access controls, then export URLs with authentication.
Step 1: Create a realm.
;; Create a basic authentication realm (http:add-realm :my-realm :basic)
Step 2: Create groups within the realm. Groups must exist before users are assigned to them.
;; Create multiple groups at once (http:add-groups :my-realm :users :administrators) ;; Or one at a time (http:add-group :my-realm :users)
Step 3: Add users with passwords and group memberships.
(http:add-user "alice" :my-realm
:password "alice-secret"
:groups '(:users)
:personal-name "Alice Johnson"
:email-address "alice@example.com")
(http:add-user "bob" :my-realm
:password "bob-secret"
:groups '(:users :administrators)
:personal-name "Bob Smith"
:email-address "bob@example.com")
Step 4: Define access control groups (capabilities).
;; Give :administrators full access, :users read access
(http:add-access-control-group :admin-access
:my-realm
:capabilities '((:get :users)
(:head :users)
(:default :administrators)))
Step 5: Export URLs with authentication.
;; Any user in the realm can access this URL
(http:export-url #u"/protected/public.html"
:computed
:response-function #'write-protected-page
:authentication-realm :my-realm
:capabilities nil ; nil = any user in the realm
:private t
:proxy-revalidate t)
;; Only users matching :admin-access capabilities
(http:export-url #u"/protected/admin.html"
:computed
:response-function #'write-admin-page
:authentication-realm :my-realm
:capabilities :admin-access
:private t
:proxy-revalidate t)
Instead of pre-defining an access control group with
add-access-control-group, you can pass a capability alist
directly to :capabilities:
;; Inline capability alist — system-hackers do everything
(http:export-url #u"/admin/system.html"
:computed
:response-function #'write-system-page
:authentication-realm :system-admin
:capabilities '((:default :system-hackers))
:private t
:proxy-revalidate t)
Digest authentication (RFC 2069, RFC 2617) is significantly stronger than basic authentication. Instead of sending the password, the client sends a one-way hash (digest) of the username, password, realm, nonce, and request URI. The server verifies the digest against its stored password digest. Replay attacks are prevented for different URLs and different IP addresses.
Important: MD5 and SHA-1 are considered insecure. All new
applications should use SHA-256 as the digest algorithm by specifying
:digest-sha-256 as the realm scheme.
;; Create a digest realm using SHA-256
(http:add-realm :secure-realm :digest-sha-256)
;; Create groups
(http:add-groups :secure-realm :members :elite-members)
;; Add users
(http:add-user "mike" :secure-realm
:password "mike-secret"
:groups '(:members)
:personal-name "Mike Smith"
:email-address "mike@example.com")
(http:add-user "joe" :secure-realm
:password "joe-secret"
:groups '(:members :elite-members)
:personal-name "Joe Doe"
:email-address "joe@example.com")
;; Define capabilities for elite members
(http:add-access-control-group :elite-access
:secure-realm
:capabilities '((:get :elite-members)
(:head :elite-members)))
;; Export URL accessible to any realm member
(http:export-url #u"/secure/members.html"
:computed
:response-function #'write-members-page
:authentication-realm :secure-realm
:capabilities nil
:private t
:proxy-revalidate t)
;; Export URL accessible only to elite members
(http:export-url #u"/secure/elite.html"
:computed
:response-function #'write-elite-page
:authentication-realm :secure-realm
:capabilities :elite-access
:private t
:proxy-revalidate t)
Digest authentication uses nonces (numbers used once) to prevent
replay attacks. The nonce encodes a time window during which it remains
valid. The variable
http:*digest-authentication-nonce-life-time* controls this
window in seconds. A shorter window reduces the replay attack window but
causes more frequent re-authentication challenges.
Combining digest authentication with subnet security and a short nonce lifetime provides reasonable access control for sensitive resources.
When first setting up a server with digest authentication, perform these one-time initializations:
(http:digest-authentication-random-seed t) sixteen times
manually. The time variability of manual evaluation increases randomness
of the seed, making digest authentication passwords more secure.
:server
realm (digest authentication) has a Webmaster account for
web-based server maintenance:
(http:save-authentication-object
(http:intern-user :server "Webmaster"
:personal-name "Common Lisp Wizard"
:password "your-secret-password"
:email-address "admin@your-host.example.com"))
Security note: Never set this password over the network. The password travels in clear text during assignment. Set passwords for digest-authenticated realms only from the server console.
HTTP:PW; stores serialized authentication data. For maximum
security, this directory should reside on the same machine as the server
and be protected by file system permissions.
The digest algorithm is selected by the realm's scheme keyword:
| Scheme | Algorithm | Digest Size | Status |
|---|---|---|---|
:digest / :md5 | MD5 | 128 bits | Insecure since 2004 |
:digest-sha | SHA-1 | 160 bits | Phase-out recommended |
:digest-sha-256 | SHA-256 | 256 bits | Recommended |
:digest-sha-384 | SHA-384 | 384 bits | Secure |
:digest-sha-512 | SHA-512 | 512 bits | Secure |
Certificate authentication uses X.509 client certificates presented
during SSL/TLS handshake. This is the strongest authentication method
available, as it relies on public key cryptography rather than passwords.
Certificate authentication requires the :cl-http-ssl-client
feature to be active.
Certificate realms are defined with http:define-certificate-realm,
which specifies the acceptable certificate authorities, key sizes,
cryptographic algorithms, cipher suites, and SSL versions:
(http:define-certificate-realm :client-cert-realm (:key-size 2048 :algorithms '(:rsa :dsa) :verify-modes '(:always)) :verisign :thawte :digicert)
The parameters control which client certificates are acceptable:
:key-size
:algorithms
:rsa and :dsa.
:verify-modes
:always verifies on
every request; :once verifies only on first connection.
:ssl-versions
:cyphers
The positional arguments after the keyword parameters are keywords denoting acceptable certificate authorities (the issuers of client certificates).
Once a certificate realm is defined, it is used with
http:export-url in the same way as basic or digest realms.
Users, groups, and access controls work identically — the only
difference is the authentication mechanism used to verify identity.
;; Export a URL requiring client certificate authentication
(http:export-url #u"/secure/cert-protected.html"
:computed
:response-function #'write-cert-page
:authentication-realm :client-cert-realm
:capabilities nil
:private t
:proxy-revalidate t)
The primary interface for user management consists of these functions:
(http:add-user username realm &key password groups personal-name email-address)
(http:intern-user realm username &key if-does-not-exist password groups personal-name email-address)
:if-does-not-exist
can be :create, :error, or :soft
(returns nil). add-user calls this with
:if-does-not-exist :create.
(http:unintern-user realm user)
(http:set-user-password user password)
(http:update-user user &key personal-name email-address password groups)
(http:add-group realm group)
group is a keyword.
(http:add-groups realm &rest groups)
(http:intern-group realm group &key if-does-not-exist)
(http:group-add-user group user)
(http:group-remove-user group user)
(http:add-access-control-group name realm &key capabilities)
name is a keyword used to reference this access control when
exporting URLs.
(http:intern-access-control realm name &key if-does-not-exist capability-alist)
(http:unintern-access-control realm access-control)
The capability alist passed to :capabilities on
add-access-control-group or export-url is a
list of entries, each of the form:
(METHOD &rest GROUPS-AND-USERS)
Where:
METHOD is an HTTP method keyword (:GET,
:HEAD, :POST, :PUT,
:DELETE, :OPTIONS, :TRACE) or
:DEFAULT.
GROUPS-AND-USERS is a sequence of group keywords or
user name strings that are authorized for that method.
nil GROUPS-AND-USERS for a method means no access
is allowed for that method.
:DEFAULT method applies to all HTTP methods not
otherwise listed in the alist.
Examples:
;; :users can GET and HEAD; :admins can do everything '((:get :users) (:head :users) (:default :admins)) ;; "frank" can do everything except DELETE '((:delete) (:default "frank")) ;; Any user in the realm can do everything '((:default :any-user-in-realm)) ;; :developers can GET, HEAD, and POST '((:get :developers) (:head :developers) (:post :developers))
(http:add-realm realm-name scheme)
intern-realm.
(http:intern-realm realm &key if-does-not-exist scheme)
scheme is one of :basic,
:digest, :md5, :digest-sha,
:digest-sha-256, :digest-sha-384,
:digest-sha-512, or :certificate.
(http:unintern-realm realm)
(http:map-realms function)
Authentication data (realms, users, groups, access controls) can be saved to and restored from disk. Passwords are stored as digests, never in clear text. However, access to the authentication data file compromises security, so the file must be protected.
(http:save-authentication-data &key pathname if-exists)
http:*authentication-data-pathname*.
(http:restore-authentication-data &key pathname if-does-not-exist clear-stale-data)
clear-stale-data is non-null, all existing authentication
data in memory is purged before loading.
Security note: For production sites, use these functions to manage password data rather than embedding clear-text passwords in source files. The example code in this chapter uses clear-text passwords for pedagogical purposes only.
CL-HTTP provides a web-based interface for managing authentication
realms, groups, users, and access controls. The interface is exported
by calling http:export-access-control-interface:
(http:export-access-control-interface :port 8443 :protocol :https :authentication-realm :server :capabilities :webmasters :secure-subnets (list (http:local-host-ip-address)))
This exports the following administration URLs:
| URL | Function |
|---|---|
/cl-http/describe-realm? |
Browse and describe authentication realms |
/cl-http/describe-group? |
Describe groups within a realm (syntax: realm|group) |
/cl-http/describe-access-control? |
Describe access controls within a realm |
/cl-http/describe-user? |
Describe a user's capabilities |
/cl-http/edit-user? |
Edit a user's access capabilities |
/cl-http/edit-user.html |
Form for editing user access control |
/cl-http/create-user.html |
Form for creating user accounts with privileges |
The administration interface should always be protected by both
authentication and subnet restrictions. For maximum security, restrict
access to the local host and the :server realm's
Webmaster account.
HTTP methods such as PUT and DELETE change server state. The variable
http:*accept-write-methods* controls the global security
policy for these side-effecting methods. It can be set via the
server configuration
interface or programmatically.
Available policies:
| Policy Keyword | Requirements |
|---|---|
:access-controlled |
URLs must restrict users via either user authentication or subnet security. |
:authenticated-users |
URLs must restrict users via user authentication only. |
:authenticated-users-on-write-subnets |
URLs must restrict users via both user authentication and write subnet security. |
:local-host |
Users must be on the local host running the server. |
:none |
No users can invoke side-effecting methods. |
:remote-host |
Requires URLs to control access, but respects any global or URL-level controls. |
:write-subnets |
URLs must restrict access to hosts trusted for write operations. |
After a user has been authenticated, the server instance (bound to
http:*server*) contains the user object and authentication
method. Response functions can access these via:
(http:server-user-object server)
(http:server-authentication-method server)
:basic,
:digest).
(http:allow-user-access-p url-or-access-control user http-method)
Authenticated users are recorded in the Common Log File by qualified
name (e.g., "realm|username"). Service providers should
strictly respect people's privacy when using user properties and
logging.
This section provides complete, working examples for each
authentication method. The examples use clear-text passwords for
clarity; production sites should use
http:save-authentication-data and
http:restore-authentication-data instead.
This example sets up two realms with basic authentication, multiple users and groups, and several access-controlled URLs demonstrating different capability patterns.
(in-package :http)
;;; --- Realm Setup ---
;; Create two basic authentication realms
(add-realm :minimum-security :basic)
(add-realm :system-admin :basic)
;; Create groups before assigning users to them
(add-groups :minimum-security :users :developers)
(add-group :system-admin :system-hackers)
;;; --- User Setup ---
(add-user "ernest" :minimum-security
:password "ernest-foo"
:groups '(:users)
:personal-name "Ernest Jones"
:email-address "ernest@foo.com")
(add-user "jim" :minimum-security
:password "jim-foo"
:groups '(:developers :users)
:personal-name "Jim Thompson"
:email-address "jim@foo.com")
(add-user "frank" :minimum-security
:password "frank-foo"
:groups '(:developers :users)
:personal-name "Frank Smith"
:email-address "frank@foo.com")
(add-user "luke" :system-admin
:password "luke-foo"
:groups '(:system-hackers)
:personal-name "Luke Miller"
:email-address "luke@foo.com")
;;; --- Access Control Groups ---
;; Developers can GET, HEAD, and POST
(add-access-control-group :minimum-security-developers
:minimum-security
:capabilities '((:get :developers)
(:head :developers)
(:post :developers)))
;; Frank can do everything except DELETE
(add-access-control-group :minimum-security-frank-capabilities
:minimum-security
:capabilities '((:delete)
(:default "frank")))
;;; --- URL Exports ---
;; Any user in :minimum-security realm can access
(export-url #u"/cl-http/authentication/minimum.html"
:computed
:response-function #'display-url-authentication-status
:authentication-realm :minimum-security
:capabilities nil
:private t
:proxy-revalidate t
:language :en)
;; Only :developers group
(export-url #u"/cl-http/authentication/minimum-developers.html"
:computed
:response-function #'display-url-authentication-status
:authentication-realm :minimum-security
:capabilities :minimum-security-developers
:private t
:proxy-revalidate t
:language :en)
;; Only user "frank"
(export-url #u"/cl-http/authentication/minimum-frank.html"
:computed
:response-function #'display-url-authentication-status
:authentication-realm :minimum-security
:capabilities :minimum-security-frank-capabilities
:private t
:proxy-revalidate t
:language :en)
;; Inline capabilities: :system-hackers do everything
(export-url #u"/cl-http/authentication/system-hackers.html"
:computed
:response-function #'display-url-authentication-status
:authentication-realm :system-admin
:capabilities '((:default :system-hackers))
:private t
:proxy-revalidate t
:language :en)
This example uses the recommended SHA-256 digest authentication scheme.
(in-package :http)
;; Create a digest realm using SHA-256
(add-realm :digest-realm :digest-sha-256)
;; Create groups before user assignment
(add-groups :digest-realm :members :elite-members)
;; Add users
(add-user "mike" :digest-realm
:password "mike-foo"
:groups '(:members)
:personal-name "Mike Smith"
:email-address "mike@foo.com")
(add-user "joe" :digest-realm
:password "joe-foo"
:groups '(:members :elite-members)
:personal-name "Joe Doe"
:email-address "joe@foo.com")
;; Define capabilities for elite members
(add-access-control-group :elite-members-access
:digest-realm
:capabilities '((:get :elite-members)
(:head :elite-members)))
;; Any realm member can access
(export-url #u"/cl-http/authentication/members.html"
:computed
:response-function #'display-url-authentication-status
:authentication-realm :digest-realm
:capabilities nil
:private t
:proxy-revalidate t
:language :en)
;; Only elite members
(export-url #u"/cl-http/authentication/elite-members.html"
:computed
:response-function #'display-url-authentication-status
:authentication-realm :digest-realm
:capabilities :elite-members-access
:private t
:proxy-revalidate t
:language :en)
This example combines subnet restrictions with digest authentication for defense in depth. The log files are accessible only to the Webmaster from the local machine.
(in-package :http)
;; Export log files with both subnet and password protection
(export-url #u"/cl-http/logs/"
:directory
:pathname (http:log-directory)
:recursive-p t
:authentication-realm :server
:capabilities :webmasters
:secure-subnets (list (local-host-ip-address))
:private t
:proxy-revalidate t
:language :en)
;; Export administration interface on HTTPS with restrictions
(export-access-control-interface
:port 8443
:protocol :https
:authentication-realm :server
:capabilities :webmasters
:secure-subnets (list (local-host-ip-address)))
This example sets up X.509 certificate authentication. It requires
the :cl-http-ssl-client feature and a properly configured
SSL server with client certificate verification enabled.
(in-package :http)
#+cl-http-ssl-client
(progn
;; Define a certificate realm requiring 2048-bit RSA keys
(define-certificate-realm :cert-realm
(:key-size 2048
:algorithms '(:rsa)
:verify-modes '(:always))
:digicert :lets-encrypt)
;; Create groups and users within the certificate realm
(add-groups :cert-realm :operators :viewers)
;; Export a URL requiring certificate authentication
(export-url #u"/secure/cert-only.html"
:computed
:response-function #'write-cert-page
:authentication-realm :cert-realm
:capabilities '((:default :operators))
:private t
:proxy-revalidate t))
Applications can define custom realm classes and register them with
the scheme-class mapping using http:add-realm-scheme:
(in-package :http)
;; Define a custom realm class with additional slots
(defclass my-organization-realm (digest-sha-256-realm)
((organization :initarg :organization
:accessor realm-organization)
(department :initarg :department
:accessor realm-department))
(:documentation "A digest realm that tracks organizational affiliation."))
;; Register the custom realm class
(add-realm-scheme :organization-digest 'my-organization-realm)
;; Now use it
(intern-realm :engineering
:if-does-not-exist :create
:scheme :organization-digest)
Reference Manual | Prev: Chapter 7. HTML Generation | Next: Chapter 9. Data Caching and Performance