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

Add CSRF protection to workspace settings forms #84

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ public boolean matches(HttpServletRequest request) {
if ("POST".equals(method)) {
String uri = request.getRequestURI();

if (
uri.startsWith("/login")
) {
/*
* Matches URIs like:
* /login*
* /workspace/123/images/delete
* /workspace/123/private
* /workspace/123/public
* /workspace/123/unshare
* /workspace/123/share
* /workspace/123/delete
*/
if (uri.startsWith("/login")
|| uri.matches("/workspace/\\d+/(images/delete|private|public|unshare|share|delete)")) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<c:if test="${workspace.editable}">
<div class="centered">
<form id="deleteImagesForm" class="form-inline small centered" style="display: inline-block; margin-bottom: 5px" action="/workspace/${workspace.id}/images/delete" method="post">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button class="btn btn-default" type="submit" name="action" value="delete" title="Delete published images"><img src="${structurizrConfiguration.cdnUrl}/bootstrap-icons/trash3.svg" class="icon-btn" /> Delete published images</button>
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<c:choose>
<c:when test="${workspace.editable}">
<form action="/workspace/${workspace.id}/users" method="post">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />

<div class="row">
<div class="col-sm-6">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@
<c:when test="${workspace.publicWorkspace}">
<form id="privateWorkspaceForm" class="form-inline small centered" style="display: inline-block; margin-bottom: 5px" action="/workspace/${workspace.id}/private" method="post">
<input type="hidden" name="workspaceId" value="${workspace.id}" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button class="btn btn-default small" type="submit" name="action" value="private" title="Make workspace private"><img src="/static/bootstrap-icons/lock.svg" class="icon-btn" /> Make private</button>
</form>
</c:when>
<c:otherwise>
<form id="publicWorkspaceForm" class="form-inline small centered" style="display: inline-block; margin-bottom: 5px" action="/workspace/${workspace.id}/public" method="post">
<input type="hidden" name="workspaceId" value="${workspace.id}" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button class="btn btn-default small" type="submit" name="action" value="public" title="Make workspace public"><img src="/static/bootstrap-icons/unlock.svg" class="icon-btn" /> Make public</button>
</form>
</c:otherwise>
Expand All @@ -110,12 +112,14 @@
<c:when test="${not empty workspace.sharingToken}">
<form id="unshareWorkspaceForm" class="form-inline small centered" style="display: inline-block; margin-bottom: 5px" action="/workspace/${workspace.id}/unshare" method="post">
<input type="hidden" name="workspaceId" value="${workspace.id}" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button class="btn btn-default small" type="submit" name="action" value="unshare" title="Disable sharing link"><img src="/static/bootstrap-icons/link.svg" class="icon-btn" /> Disable sharing link</button>
</form>
</c:when>
<c:otherwise>
<form id="shareWorkspaceForm" class="form-inline small centered" style="display: inline-block; margin-bottom: 5px" action="/workspace/${workspace.id}/share" method="post">
<input type="hidden" name="workspaceId" value="${workspace.id}" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button class="btn btn-default small" type="submit" name="action" value="share" title="Enable sharing link"><img src="/static/bootstrap-icons/link.svg" class="icon-btn" /> Enable sharing link</button>
</form>
</c:otherwise>
Expand All @@ -135,6 +139,7 @@
<button id="exportWorkspaceButton" class="btn btn-default small" title="Export workspace as JSON"><img src="${structurizrConfiguration.cdnUrl}/bootstrap-icons/filetype-json.svg" class="icon-btn" /> Export workspace</button>
<form id="deleteWorkspaceForm" class="form-inline small centered" style="display: inline-block; margin-bottom: 5px" action="/workspace/${workspace.id}/delete" method="post">
<input type="hidden" name="workspaceId" value="${workspace.id}" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button class="btn btn-danger small" type="submit" name="action" value="remove" title="Delete workspace"><img src="/static/bootstrap-icons/folder-x.svg" class="icon-white icon-btn" /> Delete workspace</button>
</form>
</c:if>
Expand Down