chiark / gitweb /
Keep track of whether a CGI request is carried over SSL.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 9 Mar 2013 05:29:10 +0000 (05:29 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 9 Mar 2013 05:35:11 +0000 (05:35 +0000)
If it is, then tie the cookie so that it's only returned to us over
SSL-encrypted links.

cgi.py
chpwd
httpauth.py

diff --git a/cgi.py b/cgi.py
index 05c6c9e..0bd66cc 100644 (file)
--- a/cgi.py
+++ b/cgi.py
@@ -291,6 +291,7 @@ SPECIAL = {}
 PARAM = []
 PARAMDICT = {}
 PATH = []
+SSLP = False
 
 ## Regular expressions for splitting apart query and cookie strings.
 R_QSPLIT = RX.compile('[;&]')
@@ -346,8 +347,13 @@ def cgiparse():
   `PATH'
         The trailing `PATH_INFO' path, split at `/' markers, with any
         trailing empty component removed.
+
+  `SSLP'
+        True if the client connection is carried over SSL or TLS.
   """
 
+  global SSLP
+
   def getenv(var):
     try: return ENV[var]
     except KeyError: raise U.ExpectedError, (500, "No `%s' supplied" % var)
@@ -402,6 +408,10 @@ def cgiparse():
     if pp and not pp[-1]: pp.pop()
     PATH[:] = pp
 
+  ## Check the crypto for the connection.
+  if ENV.get('SSL_PROTOCOL'):
+    SSLP = True
+
 ###--------------------------------------------------------------------------
 ### CGI subcommands.
 
diff --git a/chpwd b/chpwd
index 5517274..1e4248b 100755 (executable)
--- a/chpwd
+++ b/chpwd
@@ -73,6 +73,9 @@ for short, long, props in [
     'metavar': 'FILE', 'dest': 'config',
     'default': OS.path.join(HOME, 'chpwd.conf'),
     'help': 'read configuration from FILE.' }),
+  ('-s', '--ssl', {
+    'dest': 'sslp', 'action': 'store_true',
+    'help': 'pretend CGI connection is carried over SSL/TLS' }),
   ('-u', '--user', {
     'metavar': 'USER', 'dest': 'user', 'default': None,
     'help': "impersonate USER, and default context to `userv'." })]:
@@ -259,6 +262,7 @@ if __name__ == '__main__':
     with cli_errors():
       OPTS, args = OPTPARSE.parse_args()
       CONF.loadconfig(OPTS.config)
+      CGI.SSLP = OPTS.sslp
       ctx = OPTS.context
       if OPTS.user:
         CU.set_user(OPTS.user)
index 22648dd..e29686c 100644 (file)
@@ -257,9 +257,10 @@ def cmd_auth(u, pw):
     CGI.redirect(CGI.action('login', why = 'AUTHFAIL'))
   else:
     t = mint_token(u)
-    CGI.redirect(CGI.action('list'),
+    CGI.redirect(CGI.action('list', u),
                  set_cookie = CGI.cookie('chpwd-token', t,
                                          httponly = True,
+                                         secure = CGI.SSLP,
                                          path = CFG.SCRIPT_NAME,
                                          max_age = (CFG.SECRETLIFE -
                                                     CFG.SECRETFRESH)))