Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an option to set Form based auth encrypted cookie as HttpOnly #27853

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public WebAuthnAuthenticationMechanism get() {
WebAuthnRunTimeConfig config = WebAuthnRecorder.this.config.getValue();
PersistentLoginManager loginManager = new PersistentLoginManager(key, config.cookieName,
config.sessionTimeout.toMillis(),
config.newCookieInterval.toMillis());
config.newCookieInterval.toMillis(), false);
String loginPage = config.loginPage.startsWith("/") ? config.loginPage : "/" + config.loginPage;
return new WebAuthnAuthenticationMechanism(loginManager, loginPage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.quarkus.test.common.http.TestHTTPResource;
import io.restassured.RestAssured;
import io.restassured.filter.cookie.CookieFilter;
import io.restassured.matcher.RestAssuredMatchers;

public class FormAuthCookiesTestCase {

Expand All @@ -57,6 +58,7 @@ public class FormAuthCookiesTestCase {
"quarkus.http.auth.form.timeout=PT2S\n" +
"quarkus.http.auth.form.new-cookie-interval=PT1S\n" +
"quarkus.http.auth.form.cookie-name=laitnederc-sukrauq\n" +
"quarkus.http.auth.form.http-only-cookie=true\n" +
"quarkus.http.auth.session.encryption-key=CHANGEIT-CHANGEIT-CHANGEIT-CHANGEIT-CHANGEIT\n";

@RegisterExtension
Expand Down Expand Up @@ -104,7 +106,7 @@ public void testFormBasedAuthSuccess() {
.assertThat()
.statusCode(302)
.header("location", containsString("/admin%E2%9D%A4"))
.cookie("laitnederc-sukrauq", notNullValue());
.cookie("laitnederc-sukrauq", RestAssuredMatchers.detailedCookie().value(notNullValue()).httpOnly(true));

RestAssured
.given()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ public class FormAuthConfig {
*/
@ConfigItem(defaultValue = "quarkus-credential")
public String cookieName;

/**
* Set the HttpOnly attribute to prevent access to the cookie via JavaScript.
*/
@ConfigItem(defaultValue = "false")
public boolean httpOnlyCookie;
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public FormAuthenticationMechanism get() {
}
FormAuthConfig form = buildTimeConfig.auth.form;
PersistentLoginManager loginManager = new PersistentLoginManager(key, form.cookieName, form.timeout.toMillis(),
form.newCookieInterval.toMillis());
form.newCookieInterval.toMillis(), form.httpOnlyCookie);
String loginPage = form.loginPage.startsWith("/") ? form.loginPage : "/" + form.loginPage;
String errorPage = form.errorPage.startsWith("/") ? form.errorPage : "/" + form.errorPage;
String landingPage = form.landingPage.startsWith("/") ? form.landingPage : "/" + form.landingPage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ public class PersistentLoginManager {
private final long timeoutMillis;
private final SecureRandom secureRandom = new SecureRandom();
private final long newCookieIntervalMillis;
private final boolean httpOnlyCookie;

public PersistentLoginManager(String encryptionKey, String cookieName, long timeoutMillis, long newCookieIntervalMillis) {
public PersistentLoginManager(String encryptionKey, String cookieName, long timeoutMillis, long newCookieIntervalMillis,
boolean httpOnlyCookie) {
this.cookieName = cookieName;
this.newCookieIntervalMillis = newCookieIntervalMillis;
this.timeoutMillis = timeoutMillis;
this.httpOnlyCookie = httpOnlyCookie;
try {
if (encryptionKey == null) {
this.secretKey = KeyGenerator.getInstance("AES").generateKey();
Expand Down Expand Up @@ -131,7 +134,8 @@ public void save(String value, RoutingContext context, String cookieName, Restor
message.put(iv);
message.put(encrypted);
String cookieValue = Base64.getEncoder().encodeToString(message.array());
context.addCookie(Cookie.cookie(cookieName, cookieValue).setPath("/").setSecure(secureCookie));
context.addCookie(
Cookie.cookie(cookieName, cookieValue).setPath("/").setSecure(secureCookie).setHttpOnly(httpOnlyCookie));
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down