Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Home Assistant Version Control provides complete version history for your setup.

v1.7 adds API authentication for standalone Docker and cuts idle CPU.

* **API Authentication for Docker:** The standalone container now asks for HTTP Basic auth on `/api/*`. It is on by default there. The username is `havc`, and the password is generated on first start and saved to `/data/api-password`. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to choose your own, or `HAVC_API_AUTH=off` behind an authenticating reverse proxy. The add-on behind Ingress is unchanged.
* **API Authentication for Docker:** The standalone container now asks for a username and password in the app. It is on by default there. The username is `havc`, and the password is generated on first start and saved to `/data/api-password`. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to choose your own, or `HAVC_API_AUTH=off` behind an authenticating reverse proxy. The add-on behind Ingress is unchanged.
* **Lower Idle CPU:** The watcher now ignores files HAVC can never commit, so polling no longer stats dependency folders, model caches, and other large directories.
* **Faster Startup Recovery:** A stale `.git/index.lock` is cleared after ten minutes, and startup and watcher retries now share one initialization run instead of racing for the git lock.

Expand Down Expand Up @@ -161,7 +161,7 @@ There are two ways to install Home Assistant Version Control: as a Home Assistan
### 2. Standalone Docker Installation

> [!WARNING]
> The standalone container requires API authentication by default. Requests to `/api/*` need HTTP Basic auth, while static files and the `/` healthcheck stay open. If you don't set `HAVC_PASSWORD`, a password is generated on first start, printed in the container logs as `[auth] API password: ...`, and saved to `/data/api-password` inside the container. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to use your own credentials. Set `HAVC_API_AUTH=off` only when an authenticating reverse proxy already sits in front of the container. For internet exposure, keep an authenticating reverse proxy in front and leave the port closed on untrusted networks.
> The standalone container requires API authentication by default. The app asks for a username and password when API auth is enabled. The login form works in the Home Assistant Companion app as well as a normal browser. The `/healthz` endpoint stays open for the container healthcheck. If you don't set `HAVC_PASSWORD`, a password is generated on first start, printed in the container logs as `[auth] API password: ...`, and saved to `/data/api-password` inside the container. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to use your own credentials. Set `HAVC_API_AUTH=off` only when an authenticating reverse proxy already sits in front of the container. For internet exposure, keep an authenticating reverse proxy in front and leave the port closed on untrusted networks.

<details>
<summary>Click to expand Docker deployment options</summary>
Expand Down
5 changes: 5 additions & 0 deletions homeassistant-version-control/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

## [1.7.1]

### Fixed & Improved
- **In-App Login for API Auth:** When API auth is enabled, the app now shows its own username and password form instead of relying on the browser's HTTP Basic dialog. This works in the Home Assistant Companion app and other embedded webviews, where the native dialog is unavailable. The container healthcheck moved to the unauthenticated `/healthz` endpoint.

## [1.7.0]

### Security Hardening
Expand Down
4 changes: 2 additions & 2 deletions homeassistant-version-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Home Assistant Version Control provides complete version history for your setup.

v1.7 adds API authentication for standalone Docker and cuts idle CPU.

* **API Authentication for Docker:** The standalone container now asks for HTTP Basic auth on `/api/*`. It is on by default there. The username is `havc`, and the password is generated on first start and saved to `/data/api-password`. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to choose your own, or `HAVC_API_AUTH=off` behind an authenticating reverse proxy. The add-on behind Ingress is unchanged.
* **API Authentication for Docker:** The standalone container now asks for a username and password in the app. It is on by default there. The username is `havc`, and the password is generated on first start and saved to `/data/api-password`. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to choose your own, or `HAVC_API_AUTH=off` behind an authenticating reverse proxy. The add-on behind Ingress is unchanged.
* **Lower Idle CPU:** The watcher now ignores files HAVC can never commit, so polling no longer stats dependency folders, model caches, and other large directories.
* **Faster Startup Recovery:** A stale `.git/index.lock` is cleared after ten minutes, and startup and watcher retries now share one initialization run instead of racing for the git lock.

Expand Down Expand Up @@ -162,7 +162,7 @@ There are two ways to install Home Assistant Version Control: as a Home Assistan
### 2. Standalone Docker Installation

> [!WARNING]
> The standalone container requires API authentication by default. Requests to `/api/*` need HTTP Basic auth, while static files and the `/` healthcheck stay open. If you don't set `HAVC_PASSWORD`, a password is generated on first start, printed in the container logs as `[auth] API password: ...`, and saved to `/data/api-password` inside the container. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to use your own credentials. Set `HAVC_API_AUTH=off` only when an authenticating reverse proxy already sits in front of the container. For internet exposure, keep an authenticating reverse proxy in front and leave the port closed on untrusted networks.
> The standalone container requires API authentication by default. The app asks for a username and password when API auth is enabled. The login form works in the Home Assistant Companion app as well as a normal browser. The `/healthz` endpoint stays open for the container healthcheck. If you don't set `HAVC_PASSWORD`, a password is generated on first start, printed in the container logs as `[auth] API password: ...`, and saved to `/data/api-password` inside the container. Set `HAVC_USERNAME` and `HAVC_PASSWORD` to use your own credentials. Set `HAVC_API_AUTH=off` only when an authenticating reverse proxy already sits in front of the container. For internet exposure, keep an authenticating reverse proxy in front and leave the port closed on untrusted networks.

For Docker users who aren't using the Home Assistant app, you have three deployment options:

Expand Down
4 changes: 2 additions & 2 deletions homeassistant-version-control/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Home Assistant Version Control",
"version": "1.7.0",
"version": "1.7.1",
"slug": "home-assistant-version-control",
"description": "Git-powered version control and automated backup for your Home Assistant configuration files.",
"url": "https://github.com/saihgupr/HomeAssistantVersionControl",
Expand Down Expand Up @@ -95,7 +95,7 @@
"CMD",
"node",
"-e",
"fetch('http://localhost:54001/').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
"fetch('http://localhost:54001/healthz').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
],
"interval": "5s",
"timeout": "3s",
Expand Down
2 changes: 1 addition & 1 deletion homeassistant-version-control/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "home-assistant-version-control",
"version": "1.7.0",
"version": "1.7.1",
"description": "Home Assistant Version Control - Git-powered time travel for your HA config",
"main": "server.js",
"type": "module",
Expand Down
141 changes: 141 additions & 0 deletions homeassistant-version-control/public/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// In-app login for API auth.
//
// The browser's HTTP Basic dialog works in Safari and desktop browsers, but
// Home Assistant Ingress loads this app inside a frame in the Companion app,
// where iOS does not surface the dialog. This wrapper sends the credentials
// with every API request and shows a form when the server returns 401.
(function () {
'use strict';

const AUTH_KEY = 'havc-api-auth';
const FAILED_KEY = 'havc-api-auth-failed';
const DEFAULT_USERNAME = 'havc';

function readStorage(key) {
try {
return sessionStorage.getItem(key) || '';
} catch (error) {
return '';
}
}

function writeStorage(key, value) {
try {
sessionStorage.setItem(key, value);
} catch (error) {
// Private mode or a locked-down webview. The form still works for this page.
}
}

function removeStorage(key) {
try {
sessionStorage.removeItem(key);
} catch (error) {
// Ignore.
}
}

function getStoredAuth() {
return readStorage(AUTH_KEY);
}

function isApiRequest(url) {
try {
const parsed = new URL(url, window.location.href);
// Never attach credentials to another origin, even if its path looks
// like an API route.
if (parsed.origin !== window.location.origin) return false;
return parsed.pathname === '/api' || parsed.pathname.startsWith('/api/');
} catch (error) {
return false;
}
}

function showLogin() {
const overlay = document.getElementById('authOverlay');
if (!overlay) return;

const form = document.getElementById('authForm');
const username = document.getElementById('authUsername');
const password = document.getElementById('authPassword');
const errorBox = document.getElementById('authError');

overlay.hidden = false;
document.body.classList.add('auth-locked');

// Keep the error message across the app's several parallel 401s. Only the
// first call after a failed attempt sets it; later calls leave it alone.
const failed = readStorage(FAILED_KEY) === '1';
if (failed) {
removeStorage(FAILED_KEY);
if (errorBox) errorBox.textContent = 'Wrong username or password.';
}
if (username && !username.value) {
username.value = DEFAULT_USERNAME;
}
if (password) {
password.value = '';
}

if (form && !form.dataset.authBound) {
form.dataset.authBound = '1';
form.addEventListener('submit', function (event) {
event.preventDefault();

const user = (username && username.value ? username.value : '').trim();
const pass = password && password.value ? password.value : '';

if (!user || !pass) {
if (errorBox) errorBox.textContent = 'Enter a username and password.';
return;
}

writeStorage(AUTH_KEY, btoa(user + ':' + pass));
window.location.reload();
});
}

if (password) {
window.setTimeout(function () {
password.focus();
}, 50);
}
}

const nativeFetch = window.fetch.bind(window);

window.fetch = function (input, init) {
const options = init ? Object.assign({}, init) : {};
const url = typeof input === 'string' ? input : (input && input.url) || '';
const apiRequest = isApiRequest(url);

if (apiRequest) {
const headers = new Headers(options.headers || (input instanceof Request ? input.headers : undefined));
headers.set('X-Requested-With', 'XMLHttpRequest');

const auth = getStoredAuth();
if (auth) {
headers.set('Authorization', 'Basic ' + auth);
}
options.headers = headers;
}

return nativeFetch(input, options).then(function (response) {
if (apiRequest && response.status === 401) {
if (getStoredAuth()) {
removeStorage(AUTH_KEY);
writeStorage(FAILED_KEY, '1');
}
showLogin();
}
return response;
});
};

window.havcAuth = {
show: showLogin,
clear: function () {
removeStorage(AUTH_KEY);
}
};
})();
Loading
Loading