Skip to content
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
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
**2026-11-21 Arturo Bernal (abernal AT apache DOT org)**
* _3.0.2-git-2_

* Fix for [JSPWIKI-1210](https://issues.apache.org/jira/browse/JSPWIKI-1210) Access rules can combine roles, groups and users with AND, OR, NOT and parentheses, as in `[{ALLOW edit Editors AND (Reviewers OR NOT Banned)}]`. An advanced rule grants its permission together with the other rules of the page, the operators are written in upper case, and a name with an operator word inside it, such as ANDY, is still a name. Based on the work of Alex O'Ree

**2026-11-19 Arturo Bernal (abernal AT apache DOT org)**
* _3.0.2-git-1_

Expand Down
2 changes: 1 addition & 1 deletion jspwiki-api/src/main/java/org/apache/wiki/api/Release.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public final class Release {
* <p>
* If the build identifier is empty, it is not added.
*/
public static final String BUILD = "1";
public static final String BUILD = "2";

/**
* This is the generic version string you should use when printing out the version. It is of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.wiki.api.core.Session;
import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
import org.apache.wiki.api.exceptions.WikiException;
import org.apache.wiki.auth.acl.AclImpl;
import org.apache.wiki.auth.acl.AclManager;
import org.apache.wiki.auth.acl.UnresolvedPrincipal;
import org.apache.wiki.auth.acl.adv.RoleNode;
import org.apache.wiki.auth.acl.adv.RuleNode;
import org.apache.wiki.auth.authorize.GroupManager;
import org.apache.wiki.auth.authorize.Role;
import org.apache.wiki.auth.permissions.AllPermission;
Expand Down Expand Up @@ -59,9 +62,11 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.security.cert.Certificate;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.locks.ReentrantLock;

Expand Down Expand Up @@ -169,10 +174,40 @@ public boolean checkPermission( final Session session, final Permission permissi
return true;
}
}

// The advanced rules grant the permission as well, to the users that satisfy any of them.
if( acl instanceof AclImpl ) {
for( final RuleNode rule : ( ( AclImpl ) acl ).findRules( permission ) ) {
if( satisfies( session, rule ) ) {
fireEvent( WikiSecurityEvent.ACCESS_ALLOWED, user, permission );
return true;
}
}
}
fireEvent( WikiSecurityEvent.ACCESS_DENIED, user, permission );
return false;
}

/**
* Tells if the user of a session satisfies an advanced rule. A name counts as held only if the user really has the role or is the
* logged in user, so a name that is only asserted does not.
*/
private boolean satisfies( final Session session, final RuleNode rule ) {
final Set< String > held = new HashSet<>();
for( final RoleNode node : rule.getRoleNodes() ) {
Principal principal = node.getPrincipal();
if( principal == null || principal instanceof UnresolvedPrincipal ) {
// Same as for the entries of an ACL: a group or user created after the page was read can be resolved now
principal = resolvePrincipal( node.getRole() );
node.setPrincipal( principal );
}
if( hasRoleOrPrincipal( session, principal ) ) {
held.add( node.getRole() );
}
}
return rule.evaluate( held );
}

/** {@inheritDoc} */
@Override
public Authorizer getAuthorizer() throws WikiSecurityException {
Expand Down
103 changes: 102 additions & 1 deletion jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one
package org.apache.wiki.auth.acl;

import org.apache.wiki.api.core.AclEntry;
import org.apache.wiki.auth.acl.adv.RuleNode;
import org.apache.wiki.util.Synchronizer;

import java.io.Serializable;
Expand All @@ -28,6 +29,9 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -51,6 +55,100 @@ public class AclImpl implements Acl, Serializable {

private static final long serialVersionUID = 1L;
private final List< AclEntry > m_entries = new CopyOnWriteArrayList<>();
private final List< AdvancedRule > m_rules = new CopyOnWriteArrayList<>();

/** An advanced rule and the permission it grants to the users that satisfy it. */
private static final class AdvancedRule implements Serializable {

private static final long serialVersionUID = 1L;

private final Permission permission;
private final RuleNode rule;

private AdvancedRule( final Permission permission, final RuleNode rule ) {
this.permission = permission;
this.rule = rule;
}
}

/**
* Adds an advanced rule, which grants a permission to the users that satisfy an expression of roles, groups and users joined by
* AND, OR and NOT. A rule adds to the entries and to the other rules of the same permission: a user is granted the permission if
* any of them grants it.
*
* @param permission the permission the rule grants.
* @param rule the expression.
* @since 3.0.2
*/
public void addRule( final Permission permission, final RuleNode rule ) {
// The same page is parsed again each time it is rendered without the cache, and that must not add its rules again
Synchronizer.synchronize( lock, () -> {
for( final AdvancedRule r : m_rules ) {
if( r.permission.equals( permission ) && r.rule.toString().equals( rule.toString() ) ) {
return;
}
}
m_rules.add( new AdvancedRule( permission, rule ) );
} );
}

/**
* Replaces the names of some principals in the advanced rules with another principal, as when a user renames a profile.
*
* @param oldPrincipals the principals to replace.
* @param newPrincipal the principal that takes their place.
* @return {@code true} if any rule was changed.
* @since 3.0.2
*/
public boolean renameInRules( final Principal[] oldPrincipals, final Principal newPrincipal ) {
final Set< String > oldNames = new HashSet<>();
for( final Principal p : oldPrincipals ) {
oldNames.add( p.getName() );
}
return Synchronizer.synchronize( lock, () -> {
boolean changed = false;
for( int i = 0; i < m_rules.size(); i++ ) {
final AdvancedRule r = m_rules.get( i );
final RuleNode renamed = r.rule.rename( oldNames, newPrincipal );
if( renamed != r.rule ) {
m_rules.set( i, new AdvancedRule( r.permission, renamed ) );
changed = true;
}
}
return changed;
} );
}

/**
* Returns the advanced rules that grant a permission, which are the ones of that permission and of the permissions that imply it.
*
* @param permission the permission asked for.
* @return the expressions of the rules, in the order they were added.
* @since 3.0.2
*/
public List< RuleNode > findRules( final Permission permission ) {
final List< RuleNode > rules = new ArrayList<>();
for( final AdvancedRule r : m_rules ) {
if( r.permission.implies( permission ) ) {
rules.add( r.rule );
}
}
return rules;
}

/**
* Returns the permissions that advanced rules grant, with the expressions of the rules, in the order they were added.
*
* @return one entry for each rule.
* @since 3.0.2
*/
public List< Map.Entry< Permission, RuleNode > > getRules() {
final List< Map.Entry< Permission, RuleNode > > rules = new ArrayList<>();
for( final AdvancedRule r : m_rules ) {
rules.add( Map.entry( r.permission, r.rule ) );
}
return rules;
}

/** {@inheritDoc} */
@Override
Expand Down Expand Up @@ -131,7 +229,7 @@ public AclEntry getAclEntry( final Principal principal ) {
/** {@inheritDoc} */
@Override
public boolean isEmpty() {
return m_entries.isEmpty();
return m_entries.isEmpty() && m_rules.isEmpty();
}

/** {@inheritDoc} */
Expand All @@ -152,6 +250,9 @@ public String toString() {
}
sb.append( ")\n" );
}
for( final AdvancedRule r : m_rules ) {
sb.append( " rule = " ).append( r.rule ).append( ": (" ).append( r.permission ).append( ")\n" );
}
return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.wiki.api.spi.Wiki;
import org.apache.wiki.auth.AuthorizationManager;
import org.apache.wiki.auth.WikiSecurityException;
import org.apache.wiki.auth.acl.adv.RoleNode;
import org.apache.wiki.auth.acl.adv.RuleNode;
import org.apache.wiki.auth.acl.adv.RuleParser;
import org.apache.wiki.auth.permissions.PagePermission;
import org.apache.wiki.auth.permissions.PermissionFactory;
import org.apache.wiki.pages.PageLock;
Expand Down Expand Up @@ -77,6 +80,12 @@ public class DefaultAclManager implements AclManager {
")";
private static final String ACL_REGEX = "\\[\\{\\s*ALLOW\\s+" + PERM_REGEX + "\\s*(.*?)\\s*\\}\\]";

/**
* Identifies an advanced rule, which is a rule without commas that has AND, OR or NOT written in upper case as a word of its own.
* A name that contains one of them is a name when it is written without spaces around it, or in lower case, or in a list with commas.
*/
private static final Pattern ADVANCED_RULE = Pattern.compile( "(?:^|[\\s()])(?:AND|OR|NOT)(?=[\\s()]|$)" );

/**
* Identifies ACL strings in wiki text; the first group is the action (view, edit) and
* the second is the list of Principals separated by commas. The overall match is
Expand Down Expand Up @@ -104,6 +113,15 @@ public Acl parseAcl( final Page page, final String ruleLine ) throws WikiSecurit
fieldToks.nextToken();
final String actions = fieldToks.nextToken();

// a list of names with commas is a simple rule, as it always was, whatever words its names have
final String expression = ruleLine.trim().replaceFirst( "^\\S+\\s+\\S+", "" ).trim();
if( expression.indexOf( ',' ) < 0 && ADVANCED_RULE.matcher( expression ).find() ) {
addAdvancedRule( acl, page, actions, expression, ruleLine );
page.setAcl( acl );
LOG.debug( acl.toString() );
return acl;
}

while( fieldToks.hasMoreTokens() ) {
final String principalName = fieldToks.nextToken(",").trim();
final Principal principal = m_auth.resolvePrincipal(principalName);
Expand Down Expand Up @@ -134,6 +152,25 @@ public Acl parseAcl( final Page page, final String ruleLine ) throws WikiSecurit
return acl;
}

/**
* Adds an advanced rule to an ACL. The ACL has to be one of the implementation of this manager, as that is the one that keeps rules.
*/
private void addAdvancedRule( final Acl acl, final Page page, final String actions, final String expression, final String ruleLine )
throws WikiSecurityException {
if( !( acl instanceof AclImpl ) ) {
throw new WikiSecurityException( "Access rules with AND, OR or NOT are not supported by the ACL of this page: " + ruleLine );
}
final RuleNode rule;
try {
rule = new RuleParser( expression ).parse();
} catch( final IllegalArgumentException iae ) {
throw new WikiSecurityException( "Invalid access rule: " + ruleLine + " (" + iae.getMessage() + ")", iae );
}
for( final RoleNode role : rule.getRoleNodes() ) {
role.setPrincipal( m_auth.resolvePrincipal( role.getRole() ) );
}
( ( AclImpl ) acl ).addRule( PermissionFactory.getPagePermission( page, actions ), rule );
}

/** {@inheritDoc} */
@Override
Expand Down Expand Up @@ -241,6 +278,11 @@ protected static String printAcl( final Acl acl ) {
}
s.append( "}]\n" );
}
if( acl instanceof AclImpl ) {
for( final Map.Entry< Permission, RuleNode > rule : ( ( AclImpl ) acl ).getRules() ) {
s.append( "[{ALLOW " ).append( rule.getKey().getActions() ).append( " " ).append( rule.getValue() ).append( "}]\n" );
}
}
return s.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.
*/
package org.apache.wiki.auth.acl.adv;

import java.security.Principal;
import java.util.List;
import java.util.Set;

/**
* Negates an expression. It is true for every user that does not satisfy the expression, including users that are not logged in.
*
* @since 3.0.2
*/
public class NotNode extends RuleNode {

private static final long serialVersionUID = 1L;

private final RuleNode child;

/**
* Creates a negation.
*
* @param child the expression to negate.
*/
public NotNode( final RuleNode child ) {
this.child = child;
}

/**
* Returns the negated expression.
*
* @return the expression.
*/
public RuleNode getChild() {
return child;
}

/** {@inheritDoc} */
@Override
public boolean evaluate( final Set< String > userRoles ) {
return !child.evaluate( userRoles );
}

/** {@inheritDoc} */
@Override
public RuleNode rename( final Set< String > oldNames, final Principal newPrincipal ) {
final RuleNode renamed = child.rename( oldNames, newPrincipal );
return renamed == child ? this : new NotNode( renamed );
}

/** {@inheritDoc} */
@Override
public List< RoleNode > getRoleNodes() {
return child.getRoleNodes();
}

/** {@inheritDoc} */
@Override
public String toString() {
return "(NOT " + child + ")";
}

}
Loading
Loading