Repository navigation
fix: validate the env variables on startup - #140
Conversation
Reviewer's GuideImplementationImplement startup validation for required settings and provider entries, report warnings without secrets, and expose only valid compute-access URLs.
Run settings enforcement during Node.js startup so fatal configuration errors prevent the server from starting.
Use the validated compute-access URL in the user-info response instead of returning the environment value directly.
ConfigurationSupply minimal valid settings to the image smoke-test container so startup validation passes.
DocumentationDocument startup validation requirements, fatal errors, warnings, and the registry URL constraint for operators.
Explain how invalid login-provider entries are reported and when they prevent startup.
TestsTest settings validation, warning and fatal behavior, secret-safe reporting, startup enforcement, and omission of invalid access-request URLs.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #140 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 28 +1
Lines 765 856 +91
Branches 207 233 +26
=========================================
+ Hits 765 856 +91 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Linked issuesChecked at commit 670bb7b #103 fix: check the settings when Krini starts · 5 of 6 met
|
There was a problem hiding this comment.
Hey - I've found 9 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/lib/settings.ts" line_range="189-191" />
<code_context>
+ it("warns on an invalid COMPUTE_ACCESS_REQUEST_URL", () => {
+ setUp({ COMPUTE_ACCESS_REQUEST_URL: "not a url" });
+ expect(validateSettings().warnings).toContainEqual({
+ setting: "COMPUTE_ACCESS_REQUEST_URL",
+ problem: "is not a valid http(s) URL; the link is ignored",
+ });
+ });
+
</code_context>
<issue_to_address>
**Invalid links remain clickable**
When `COMPUTE_ACCESS_REQUEST_URL` is set to a non-HTTP(S) value, `validateSettings()` logs a warning, but `/auth/api/user-info` still returns the raw value and the UI renders it as an `href`; users see a link the warning says is ignored, and clicking a `javascript:` value executes script in their browser.
Suppress invalid `COMPUTE_ACCESS_REQUEST_URL` values in `/auth/api/user-info` so the UI does not render them as links.
</issue_to_address>
### Comment 2
<location path="src/lib/settings.ts" line_range="106" />
<code_context>
+ if (typeof value !== "string" || value === "") {
+ problems.push({
+ setting: "LOGIN_PROVIDERS",
+ problem: `${where} has no ${field}`,
+ });
+ }
</code_context>
<issue_to_address>
**Usable providers block startup**
When `LOGIN_PROVIDERS` contains a usable provider plus an incomplete or duplicate entry, `loginProviderProblems()` adds the incomplete or duplicate entry to `fatal`, so `enforceSettings()` exits even though `getLoginProviders()` keeps a usable provider; the deployment refuses to start.
Make incomplete and duplicate entries non-fatal when `getLoginProviders()` has a usable provider.
Also at `src/lib/settings.ts:113-117`, `src/lib/settings.ts:221-226`.
</issue_to_address>
### Comment 3
<location path="src/lib/settings.ts" line_range="103" />
<code_context>
+ const record = entry as Record<string, unknown>;
+ for (const field of REQUIRED_PROVIDER_FIELDS) {
+ const value = record[field];
+ if (typeof value !== "string" || value === "") {
+ problems.push({
+ setting: "LOGIN_PROVIDERS",
</code_context>
<issue_to_address>
**Malformed issuer delays login failure**
When a provider has a non-empty but malformed `issuer` value, `loginProviderProblems()` accepts the issuer, so startup proceeds; `getEndpoints()` then fails to fetch its discovery document when the user signs in, and login fails without a startup diagnostic.
Validate that each provider’s `issuer` is a usable HTTP(S) URL before accepting the provider.
</issue_to_address>
### Comment 4
<location path="src/lib/settings.ts" line_range="168-170" />
<code_context>
+ const callback = new URL(redirectUri);
+ const app = appOrigin ? new URL(appOrigin) : null;
+ if (
+ (callback.protocol !== "http:" &&
+ callback.protocol !== "https:") ||
+ (app && callback.origin !== app.origin)
+ ) {
+ fatal.push({
</code_context>
<issue_to_address>
**Wrong callback path breaks login**
When `OIDC_REDIRECT_URI` is an HTTP(S) URL on the `APP_ORIGIN` origin but its path is not `/auth/api/callback`, `validateSettings()` checks the scheme and origin but not the path, so the URI passes validation and is sent to the identity provider; the provider returns users to a route without the callback handler, and login fails.
Reject `OIDC_REDIRECT_URI` values whose path is not `/auth/api/callback`.
Also at `src/lib/settings.ts:172-176`.
</issue_to_address>
### Comment 5
<location path="src/lib/settings.ts" line_range="143" />
<code_context>
+ if (!appOrigin) {
+ fatal.push({ setting: "APP_ORIGIN", problem: "is not set" });
+ } else if (!isHttpUrl(appOrigin)) {
+ fatal.push({
+ setting: "APP_ORIGIN",
+ problem: "is not a valid http(s) URL",
</code_context>
<issue_to_address>
**HTTP production origin passes validation**
When `NODE_ENV` is production and `APP_ORIGIN` uses HTTP for a non-local host, `validateSettings` accepts a non-local HTTP `APP_ORIGIN` in production, so the server starts with an origin where browsers will not send its Secure session cookies and login sessions fail.
Reject non-HTTPS production origins except localhost during startup.
</issue_to_address>
### Comment 6
<location path="src/lib/settings.ts" line_range="28-31" />
<code_context>
+ "clientSecret",
+] as const;
+
+function isHttpUrl(value: string): boolean {
+ try {
+ const url = new URL(value);
+ return url.protocol === "http:" || url.protocol === "https:";
+ } catch {
+ return false;
</code_context>
<issue_to_address>
**Configured origin path is discarded**
When `APP_ORIGIN` is an HTTP(S) URL with a non-root path or query, `isHttpUrl()` accepts the value, so startup proceeds; `getAppOrigin()` reduces it to the URL origin and drops the configured path, leaving redirects and callback URLs without the configured prefix.
Validate that `APP_ORIGIN` is an origin with no path or query, rather than accepting any HTTP(S) URL.
Also at `src/lib/settings.ts:142-147`.
</issue_to_address>
### Comment 7
<location path="src/lib/settings.ts" line_range="156" />
<code_context>
+ if (!appOrigin) {
+ fatal.push({ setting: "APP_ORIGIN", problem: "is not set" });
+ } else if (!isHttpUrl(appOrigin)) {
+ fatal.push({
+ setting: "APP_ORIGIN",
+ problem: "is not a valid http(s) URL",
</code_context>
<issue_to_address>
**Registry requests target wrong endpoint**
When `CLOUD_REGISTRY_URL` includes a query or fragment, `isHttpUrl()` accepts the value, but `load` appends `/services` to the raw string, so the request targets the wrong endpoint and registry loading fails after startup.
Reject `CLOUD_REGISTRY_URL` values with a query or fragment before `load` appends `/services`.
</issue_to_address>
### Comment 8
<location path="docs/operator-guide/configuration.md" line_range="37" />
<code_context>
-through [Checking a deployment](deployment.md#checking-a-deployment).
+Krini checks its settings when it starts (see `src/lib/settings.ts`):
+a missing or invalid required setting (`LOGIN_PROVIDERS`, `APP_ORIGIN`,
+`CLOUD_REGISTRY_URL`, `OIDC_REDIRECT_URI`) is logged with the reason and
+Krini refuses to start. Warnings — an invalid `COMPUTE_ACCESS_REQUEST_URL`,
+or no provider with `computeGroups`/`adminGroups` — are logged but don't
</code_context>
<issue_to_address>
**Guide marks optional setting required**
When an operator relies on the startup-validation paragraph to determine whether `OIDC_REDIRECT_URI` must be set, the guide says a missing `OIDC_REDIRECT_URI` prevents startup, but validation skips the unset value and `getRedirectUri()` supplies a default from `APP_ORIGIN`; operators are told startup will fail when it succeeds.
Update the guide to explain that `OIDC_REDIRECT_URI` is optional and defaults from `APP_ORIGIN`.
</issue_to_address>
### Comment 9
<location path="src/lib/settings.test.ts" line_range="41-42" />
<code_context>
+ expect(validateSettings().fatal.length).toBeGreaterThan(0);
+ });
+
+ it("names each invalid provider entry and its missing field", () => {
+ setUp({
+ LOGIN_PROVIDERS: JSON.stringify([
+ { id: "broken", issuer: "https://i.example.org" },
</code_context>
<issue_to_address>
**Test missing issuer**
The issue asks for tests covering each check, but the provider tests do not cover an entry missing issuer. Add a case that checks the reported problem for that missing field.
</issue_to_address>Sourcery assessment
Approval pending. 7 findings to address first.
Blocking findings: src/lib/settings.ts:191, src/lib/settings.ts:106, src/lib/settings.ts:103, src/lib/settings.ts:170, src/lib/settings.ts:143, and 2 more
closes #103
Summary by Sourcery
Validate Krini configuration during startup so deployment errors are reported immediately and invalid settings cannot cause misleading runtime behavior.
Bug Fixes:
Enhancements:
Documentation:
Tests: