Hash the web password and revoke sessions server-side
Store a scrypt hash (migrating a plaintext password without changing it), sign sessions with a generation that ./password and --revoke-all rotate, make logout a same-origin POST that revokes the token, add sign out everywhere, and close proxied websockets whose session ends. Enforce a strength rule for new passwords, back off logins per client and server-wide, check Origin on login, tighten next, add security headers and a __Host- cookie over https.
This commit is contained in:
+27
-11
@@ -44,6 +44,7 @@ lan = module("lan_address", "lan-address.py")
|
||||
release_lookup = module("resolve_releases", "resolve-releases.py")
|
||||
tokens = module("save_ntfy_token", "save-ntfy-token.py")
|
||||
app_build = module("app_build", "app_build.py")
|
||||
web_password = module("pipelines_password", "password.py")
|
||||
|
||||
DEFAULT_PREFIX = Path.home() / ".local/share/pipelines"
|
||||
MARKER = "pipelines-install.json"
|
||||
@@ -348,16 +349,17 @@ class Setup:
|
||||
or Path.home() / ".config/pipelines/web-auth.json").expanduser().resolve()
|
||||
if auth.exists():
|
||||
data = json.loads(auth.read_text())
|
||||
if (not isinstance(data, dict) or not isinstance(data.get("password"), str)
|
||||
or len(data["password"]) < 8 or not isinstance(data.get("secret"), str)
|
||||
or len(data["secret"]) < 32):
|
||||
if not web_password.valid_auth(data):
|
||||
raise ValueError(f"Invalid existing web auth file; retained unchanged: {auth}")
|
||||
if auth.stat().st_mode & 0o077:
|
||||
raise ValueError(f"Web auth file must be private (chmod 600): {auth}")
|
||||
else:
|
||||
password = self.choose_password()
|
||||
self.chosen_web_password = password
|
||||
auth.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
self._write_atomic(auth, (json.dumps({"password": password,
|
||||
# Only a scrypt hash is stored (see scripts/password.py).
|
||||
self._write_atomic(auth, (json.dumps({"password_hash": web_password.hash_password(password),
|
||||
"password_weak": False, "generation": 0,
|
||||
"secret": secrets.token_urlsafe(48)}) + "\n").encode(), 0o600)
|
||||
target = prefix / "lib/web"
|
||||
shutil.copytree(Path(__file__).resolve().parents[1] / "web", target, dirs_exist_ok=True,
|
||||
@@ -374,17 +376,20 @@ class Setup:
|
||||
PIPELINES_WEB_PASSWORD answers it without a terminal (automation)."""
|
||||
given = self.environ.get("PIPELINES_WEB_PASSWORD")
|
||||
if given is not None:
|
||||
if len(given) < 8:
|
||||
raise ValueError("PIPELINES_WEB_PASSWORD must be at least 8 characters")
|
||||
problem = web_password.password_problem(given)
|
||||
if problem:
|
||||
raise ValueError(f"PIPELINES_WEB_PASSWORD is too weak. {problem}")
|
||||
return given
|
||||
if not sys.stdin.isatty():
|
||||
raise ValueError("Choose the web sign-in password: rerun ./install in a terminal, "
|
||||
"or set PIPELINES_WEB_PASSWORD (at least 8 characters)")
|
||||
"or set PIPELINES_WEB_PASSWORD. " + web_password.PASSWORD_RULE)
|
||||
print("This computer has no web sign-in yet. Choose the password for the web app and phone.")
|
||||
print(web_password.PASSWORD_RULE)
|
||||
while True:
|
||||
first = getpass.getpass("Web password (at least 8 characters): ")
|
||||
if len(first) < 8:
|
||||
print("Too short; use at least 8 characters.")
|
||||
first = getpass.getpass("Web password: ")
|
||||
problem = web_password.password_problem(first)
|
||||
if problem:
|
||||
print(problem)
|
||||
continue
|
||||
if getpass.getpass("Repeat the password: ") != first:
|
||||
print("The passwords did not match; try again.")
|
||||
@@ -673,6 +678,17 @@ class Setup:
|
||||
process.kill()
|
||||
process.wait()
|
||||
|
||||
def reader_password(self, web):
|
||||
"""The phone's ntfy password, which matches the web password: the one
|
||||
chosen during this install, else a not yet migrated plaintext file. A
|
||||
hashed file has no plaintext; an existing ntfy account keeps its own
|
||||
password and a new one is asked for on the terminal."""
|
||||
chosen = getattr(self, "chosen_web_password", None)
|
||||
if chosen is not None:
|
||||
return chosen
|
||||
legacy = json.loads(Path(web["auth_file"]).read_text()).get("password")
|
||||
return legacy if isinstance(legacy, str) else None
|
||||
|
||||
def bootstrap_ntfy(self, prefix, config_path, reader_password=None):
|
||||
def run_ntfy(args, capture=False, interactive=False, password=None):
|
||||
return self.ntfy(prefix, config_path, list(args), capture=capture, password=password)
|
||||
@@ -856,7 +872,7 @@ class Setup:
|
||||
else:
|
||||
self.migrate_docker(prefix, args.replace_data)
|
||||
self.bootstrap_ntfy(prefix, config_path,
|
||||
reader_password=json.loads(Path(web["auth_file"]).read_text())["password"])
|
||||
reader_password=self.reader_password(web))
|
||||
timezone = args.timezone
|
||||
if not timezone:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user