Exceptions caused by Python3 MongoDB objectId passed as bytes

I have encountered an error that happened after long time when Pritunl ran without restart: OpenVPN did not start and there were many errors in /var/log/pritunl.log:

[pritunl.<domain.tld-removed>][2022-05-11 08:56:32,820][ERROR] Exception on /event/627b4c26962f9e57323bd325 [GET]
Traceback (most recent call last):
  File "/usr/lib/pritunl/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/pritunl/lib/python3.8/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/pritunl/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/pritunl/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/usr/lib/pritunl/lib/python3.8/site-packages/pritunl/auth/app.py", line 7, in _wrapped
    if not check_session(True):
  File "/usr/lib/pritunl/lib/python3.8/site-packages/pritunl/auth/administrator.py", line 449, in check_session
    admin_id = database.ParseObjectId(admin_id)
  File "/usr/lib/pritunl/lib/python3.8/site-packages/pritunl/database/utils.py", line 30, in ParseObjectId
    return bson.ObjectId(str(oid))
  File "/usr/lib/pritunl/lib/python3.8/site-packages/bson/objectid.py", line 110, in __init__
  File "/usr/lib/pritunl/lib/python3.8/site-packages/bson/objectid.py", line 213, in __validate
  File "/usr/lib/pritunl/lib/python3.8/site-packages/bson/objectid.py", line 38, in _raise_invalid_id
    raise InvalidId(
bson.errors.InvalidId: "b'6078da2dd3d212408eeb9d32'" is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string

I don’t know the entire history of the server but now it is Ubuntu Focal that runs unattended updates and uses the recommended repos:

$ cat /etc/apt/sources.list.d/*
deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse
deb http://repo.pritunl.com/stable/apt focal main

And I tried to run apt-get update && apt-get dist-upgrade and reboot the server, but it didn’t make any difference.

The exception repeated every few seconds, the OpenVPN server was down and it was not caused by a web interface, but I tried to clear cookies in web browser as suggested here Cannot access web UI .

I also tried running pritunl repair-database but it didn’t help either.

In the end I hacked the pritunl/utils/filter.py to tranlate the byte array to string:

--- /usr/lib/pritunl/lib/python3.8/site-packages/pritunl/utils/filter.py-orig	2022-05-11 09:23:02.596792240 +0000
+++ /usr/lib/pritunl/lib/python3.8/site-packages/pritunl/utils/filter.py	2022-05-11 09:26:32.662389167 +0000
@@ -57,4 +57,7 @@
 def session_opt_str(key):
     val = flask.session.get(key)
+    if isinstance(val, bytes):
+        print(f"WARN: session_opt_str fixup hit for key {key}")
+        return val.decode()
     return None if val is None else str(val)

and the error disappeared. In fact the fixup was needed only once, after restarting Pritunl and restoring the original file it does not happen anymore.

Btw. the ID was the only one document in mongo.administrators collection. It seems that the OpenVPN startup uses some cached API call that contained bytes instead of string?

This is caused by a incorrect value in the session cookies from a previous release, clearing the cookies for the Pritunl server domain can fix the issue.