-
Notifications
You must be signed in to change notification settings - Fork 166
Secure initial Roller setup with one-time operator token #189
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
Open
snoopdave
wants to merge
7
commits into
apache:master
Choose a base branch
from
snoopdave:secure-initial-setup-token
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+541
−7
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ea4c70
Secure Roller initial setup with one-time token
snoopdave 69106ba
Remove unrelated Roller 7 planning document
snoopdave aa5f015
Keep installer status checks usable without request context
snoopdave 3d35e0e
Preserve first-user role assignment behavior
snoopdave 129f6f8
Secure initial Roller setup with one-time token
snoopdave 7282293
Skip empty database upgrade flow
snoopdave 20cc99c
Fix Selenium initial login flow for setup token gate
snoopdave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/main/java/org/apache/roller/weblogger/ui/core/filters/BootstrapSecurityFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package org.apache.roller.weblogger.ui.core.filters; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.*; | ||
| import javax.servlet.http.*; | ||
| import org.apache.roller.weblogger.business.WebloggerFactory; | ||
| import org.apache.roller.weblogger.ui.core.security.BootstrapSecurity; | ||
|
|
||
| /** Prevents anonymous access to installer and first-user actions. */ | ||
| public class BootstrapSecurityFilter implements Filter { | ||
| public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | ||
| HttpServletRequest r = (HttpServletRequest) req; | ||
| HttpServletResponse p = (HttpServletResponse) res; | ||
| String uri = r.getRequestURI(); | ||
| boolean tokenPage = uri != null && (uri.endsWith("/bootstrap-token.rol") | ||
| || uri.endsWith("/bootstrap-token!redeem.rol")); | ||
| boolean installer = uri != null && (tokenPage || uri.contains("/roller-ui/install/") | ||
| || uri.endsWith("/roller-ui/register.rol") | ||
| || uri.endsWith("/roller-ui/register!save.rol") | ||
| || uri.endsWith("/roller-ui/setup.rol")); | ||
| if (installer && !BootstrapSecurity.isCompleted()) { | ||
| if (tokenPage) { chain.doFilter(req, res); return; } | ||
| if (!BootstrapSecurity.isValid(r)) { p.sendRedirect(r.getContextPath() + "/roller-ui/bootstrap-token.rol"); return; } | ||
| } | ||
| chain.doFilter(req, res); | ||
| } | ||
| public void init(FilterConfig c) { } | ||
| public void destroy() { } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
app/src/main/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. For additional information regarding | ||
| * copyright in this work, please see the NOTICE file in the top level | ||
| * directory of this distribution. | ||
| */ | ||
|
|
||
| package org.apache.roller.weblogger.ui.core.security; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.MessageDigest; | ||
| import java.security.SecureRandom; | ||
| import java.util.Base64; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
|
|
||
| /** Process-scoped gate for the unauthenticated installation flow. */ | ||
| public final class BootstrapSecurity { | ||
|
|
||
| public static final String COMPLETION_PROPERTY = "bootstrap.completed"; | ||
| private static final Log LOG = LogFactory.getLog(BootstrapSecurity.class); | ||
| private static final long LIFETIME_MS = 60 * 60 * 1000L; | ||
| private static final String SESSION = BootstrapSecurity.class.getName() + ".grant"; | ||
| private static byte[] digest; | ||
| private static long expires; | ||
| private static volatile boolean completed; | ||
| private static final ThreadLocal<Boolean> INITIAL = new ThreadLocal<>(); | ||
| private BootstrapSecurity() { | ||
| } | ||
|
|
||
| public static synchronized void start() { | ||
| if (completed || digest != null) { | ||
| return; | ||
| } | ||
| byte[] raw = new byte[32]; | ||
| new SecureRandom().nextBytes(raw); | ||
| String token = Base64.getUrlEncoder().withoutPadding().encodeToString(raw); | ||
| digest = sha256(token); | ||
| expires = System.currentTimeMillis() + LIFETIME_MS; | ||
| LOG.warn(box( | ||
| "ROLLER INITIAL SETUP REQUIRED", | ||
| "", | ||
| "Open Roller in a web browser.", | ||
| "You will be redirected to the secure initial-setup page.", | ||
| "", | ||
| "Enter this one-time setup token (expires in 60 minutes):", | ||
| token)); | ||
| } | ||
|
|
||
| public static boolean isCompleted() { | ||
| return completed; | ||
| } | ||
|
|
||
| public static void complete() { | ||
| completed = true; | ||
| digest = null; | ||
| } | ||
|
|
||
| public static void beginInitialAdmin() { | ||
| INITIAL.set(Boolean.TRUE); | ||
| } | ||
|
|
||
| public static void endInitialAdmin() { | ||
| INITIAL.remove(); | ||
| } | ||
|
|
||
| public static boolean initialAdminScope() { | ||
| return Boolean.TRUE.equals(INITIAL.get()); | ||
| } | ||
|
|
||
| public static boolean isValid(HttpServletRequest request) { | ||
| Object grant = request.getSession(false) == null ? null : request.getSession(false).getAttribute(SESSION); | ||
| return !completed && grant instanceof Long && ((Long) grant) >= System.currentTimeMillis(); | ||
| } | ||
|
|
||
| public static synchronized boolean redeem(HttpServletRequest request, String token) { | ||
| if (completed || digest == null || token == null || System.currentTimeMillis() > expires) { | ||
| return false; | ||
| } | ||
| byte[] supplied = sha256(token); | ||
| if (!MessageDigest.isEqual(digest, supplied)) { | ||
| return false; | ||
| } | ||
| request.getSession(true).setAttribute(SESSION, System.currentTimeMillis() + LIFETIME_MS); | ||
| digest = null; | ||
| return true; | ||
| } | ||
|
|
||
| private static byte[] sha256(String value) { | ||
| try { | ||
| return MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
| } | ||
|
|
||
| private static String box(String... lines) { | ||
| int width = 0; | ||
| for (String line : lines) { | ||
| width = Math.max(width, line.length()); | ||
| } | ||
|
|
||
| String border = "+" + "-".repeat(width + 2) + "+"; | ||
| StringBuilder message = new StringBuilder("\n").append(border); | ||
| for (String line : lines) { | ||
| message.append("\n| ").append(line) | ||
| .append(" ".repeat(width - line.length())).append(" |"); | ||
| } | ||
| return message.append("\n").append(border).toString(); | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/BootstrapToken.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. For additional information regarding | ||
| * copyright in this work, please see the NOTICE file in the top level | ||
| * directory of this distribution. | ||
| */ | ||
|
|
||
| package org.apache.roller.weblogger.ui.struts2.core; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.apache.roller.weblogger.ui.core.security.BootstrapSecurity; | ||
| import org.apache.roller.weblogger.ui.struts2.util.UIAction; | ||
| import org.apache.struts2.interceptor.ServletRequestAware; | ||
| import org.apache.struts2.interceptor.ServletResponseAware; | ||
|
|
||
| public class BootstrapToken extends UIAction implements ServletRequestAware, ServletResponseAware { | ||
|
|
||
| private static final Log LOG = LogFactory.getLog(BootstrapToken.class); | ||
|
|
||
| private HttpServletRequest request; | ||
| private HttpServletResponse response; | ||
| private String token; | ||
|
|
||
| public BootstrapToken() { | ||
| this.pageTitle = "installer.bootstrap.pageTitle"; | ||
| } | ||
|
|
||
| public String execute() { | ||
| setResponseHeaders(); | ||
| LOG.info("Roller is waiting for an administrator to submit the one-time setup token; " | ||
| + "database setup will not continue until the token is accepted. Setup page: " | ||
| + request.getRequestURL()); | ||
| return INPUT; | ||
| } | ||
|
|
||
| public String redeem() { | ||
| setResponseHeaders(); | ||
| if (!"POST".equalsIgnoreCase(request.getMethod())) { | ||
| addActionError(getText("installer.bootstrap.postRequired")); | ||
| return INPUT; | ||
| } | ||
| if (BootstrapSecurity.redeem(request, token == null ? null : token.trim())) { | ||
| LOG.info("One-time setup token accepted; continuing database setup."); | ||
| return SUCCESS; | ||
| } | ||
| addActionError(getText("installer.bootstrap.invalidToken")); | ||
| return INPUT; | ||
| } | ||
|
|
||
| private void setResponseHeaders() { | ||
| response.setHeader("Cache-Control", "no-store"); | ||
| response.setHeader("Referrer-Policy", "no-referrer"); | ||
| } | ||
|
|
||
| public void setToken(String token) { | ||
| this.token = token; | ||
| } | ||
|
|
||
| public void setServletRequest(HttpServletRequest request) { | ||
| this.request = request; | ||
| } | ||
|
|
||
| public void setServletResponse(HttpServletResponse response) { | ||
| this.response = response; | ||
| } | ||
|
|
||
| public boolean isUserRequired() { | ||
| return false; | ||
| } | ||
|
|
||
| public boolean isWeblogRequired() { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐞Codex Issue: Blocking: The gate only matches
/register.roland/register!save.rol, but Struts dynamic method invocation also exposes URLs such as/register!execute.roland/register!activate.rol. Those direct routes can reach the initial registration flow without the token. Gate the resolved register action/method (or reject all register methods while bootstrap is incomplete) and add regression coverage for every allowed method.