Skip to content

refactor: reject JWT expiration value at startup if less than 1 sec - #1172

Open
ShradhaGupta31 wants to merge 1 commit into
mainfrom
fix-CM-334-jwt-expiration
Open

refactor: reject JWT expiration value at startup if less than 1 sec #1172
ShradhaGupta31 wants to merge 1 commit into
mainfrom
fix-CM-334-jwt-expiration

Conversation

@ShradhaGupta31

@ShradhaGupta31 ShradhaGupta31 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Changes Done:

  • Modified config.go to validate jwtExpiration while console startup
  • Reject non-positive values of jwtExpiration

Description:
config.yml accepts jwtExpiration: 0s which is a syntactically valid zero duration that the YAML parser accepts without error. At runtime, every issued JWT has exp = time.Now(), so all tokens expire at the moment of issuance and every subsequent API call is rejected.

Before fix : Server starts silently with jwtExpiration 0s, login returns an already-expired token:

$ curl -s -X POST http://localhost:8181/api/v1/authorize -H 'Content-Type: application/json' -d '{"username":<USER_NAME>,"password":<PASSWORD>}'
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."} 

$ curl -s http://localhost:8181/api/v1/devices -H "Authorization: Bearer <token>"
{"error":"invalid access token"} # immediately rejected

After fix : Server would fail to start if jwtExpiration is set to 0s

$ GIN_MODE=debug go run ./cmd/app --config config.yml
Config error: config: auth.jwtExpiration must be positive (e.g. 24h) — zero causes tokens to expire on issuance
exit status 1

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 58.33333% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.16%. Comparing base (7ccfa79) to head (8526529).

Files with missing lines Patch % Lines
config/config.go 58.33% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1172      +/-   ##
==========================================
- Coverage   50.16%   50.16%   -0.01%     
==========================================
  Files         147      147              
  Lines       13574    13586      +12     
==========================================
+ Hits         6810     6815       +5     
- Misses       6172     6178       +6     
- Partials      592      593       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents a misconfigured auth.jwtExpiration / auth.redirectionJWTExpiration (zero or negative durations) from allowing the server to start and then immediately issuing already-expired JWTs, which effectively denies access to legitimate users.

Changes:

  • Adds startup-time config validation for JWT expiration durations.
  • Introduces sentinel errors for invalid JWT expiration settings.
  • Adds unit tests covering zero/negative durations and valid defaults.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
config/config.go Adds Config.validate() and calls it from NewConfig() to fail fast on non-positive JWT expirations.
config/config_test.go Adds tests ensuring validation rejects zero/negative expirations and accepts defaults.
Suppressed comments (3)

config/config_test.go:133

  • The test asserts via substring matching on the error string. Since validate() returns a sentinel error, use require.ErrorIs so the test remains stable if the message wording changes.
	err := cfg.validate()
	require.Error(t, err)
	assert.Contains(t, err.Error(), "auth.jwtExpiration")
}

config/config_test.go:144

  • The test asserts via substring matching on the error string. Since validate() returns a sentinel error, use require.ErrorIs so the test remains stable if the message wording changes.
	err := cfg.validate()
	require.Error(t, err)
	assert.Contains(t, err.Error(), "auth.redirectionJWTExpiration")
}

config/config_test.go:155

  • The test asserts via substring matching on the error string. Since validate() returns a sentinel error, use require.ErrorIs so the test remains stable if the message wording changes.
	err := cfg.validate()
	require.Error(t, err)
	assert.Contains(t, err.Error(), "auth.redirectionJWTExpiration")
}

Comment thread config/config_test.go
Comment thread config/config.go
@ShradhaGupta31
ShradhaGupta31 force-pushed the fix-CM-334-jwt-expiration branch 2 times, most recently from b8d9299 to e990ba4 Compare August 4, 2026 15:42
Comment thread config/config.go Outdated

@sudhir-intc sudhir-intc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, check if wee need to keep a minimum value of atleast 1m

sudhir-intc
sudhir-intc previously approved these changes Aug 5, 2026
@ShradhaGupta31
ShradhaGupta31 requested a review from rsdmike August 6, 2026 15:03
@madhavilosetty-intel

madhavilosetty-intel commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

@ShradhaGupta31 The current validation rejects anything under 1 minute, but the commit says it’s rejecting zero/negative expirations. Please change it to <= 0.

ConsoleConfig is set to nil config.go line:353 only on validation failure, while earlier config errors leave it populated. Please keep the behavior consistent across all error paths.

@ShradhaGupta31 ShradhaGupta31 changed the title fix: reject zero/negative JWT expiration at startup fix: reject JWT expiration value at startup if less than 1 sec Aug 17, 2026
@ShradhaGupta31 ShradhaGupta31 changed the title fix: reject JWT expiration value at startup if less than 1 sec refactor: reject JWT expiration value at startup if less than 1 sec Aug 17, 2026
@ShradhaGupta31
ShradhaGupta31 force-pushed the fix-CM-334-jwt-expiration branch from f6ab15d to 365da3f Compare August 17, 2026 03:52
@ShradhaGupta31
ShradhaGupta31 force-pushed the fix-CM-334-jwt-expiration branch 2 times, most recently from ff50cb2 to df465df Compare August 17, 2026 05:21
@ShradhaGupta31

Copy link
Copy Markdown
Contributor Author

@ShradhaGupta31 The current validation rejects anything under 1 minute, but the commit says it’s rejecting zero/negative expirations. Please change it to <= 0.

ConsoleConfig is set to nil config.go line:353 only on validation failure, while earlier config errors leave it populated. Please keep the behavior consistent across all error paths.

@madhavilosetty-intel - Updated title & config.go as well.

- Modified config.go to validate jwtExpiration while console startup
- Reject non-positive values of jwtExpiration

Signed-off-by: ShradhaGupta31 <shradha.gupta@intel.com>
@ShradhaGupta31
ShradhaGupta31 force-pushed the fix-CM-334-jwt-expiration branch from df465df to 8526529 Compare August 18, 2026 05:39

@sudhir-intc sudhir-intc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Please ensure the CI passes before the merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants