Edgewall Software

Security Sandbox

Warning: this page was used as a scratch pad during the development of the fine grained permissions for Trac. This is by no means the latest documentation for this feature, see rather the TracFineGrainedPermissions page for that, or, for plugin developers, have a look at TracDev/ApiChanges/0.11.


This sandbox aims at adding a finer grained control for the TracPermissions system.

TODO 0.11:

  • check the status of above tickets
  • update the TracPermissions page to link to fine grained permissions
  • integrate the FineGrainedPermissions page (#3636)
    • focus that page on the new pluggable security features
    • keep the SVN authz specific stuff in a secondary chapter, hinting that this will become a specific security policy in 0.12

The permission policy system has been rewritten on top of the Context objects.

The Wiki system, a significant part of the Ticket system and the attachment subsystem are now using the new permission policy engine.

1000 ' View

  • Add an interface (IPermissionPolicy) for checking a users permission to access Trac resources.
  • Convert the current permission system to a plugin (DefaultPermissionPolicy).
  • Modify PermissionCache to cache the fine-grained policy check results (still needs some cleanup).
  • Convert each module to use fine-grained permissions (only the Wiki module has been converted so far).
  • API is backwards compatible.
  • Security policies can be "stacked".

API

Using the new permission system

While the new permission system is completely backwards compatible, to make full use of it you will need to change your permission checks.

Old style:

# Check for permission
if 'WIKI_MODIFY' in req.perm:
        ...

# Assert that user has permission
req.perm.require('WIKI_MODIFY')

New style is based on adding a resource descriptor (trac.resource.Resource) as identification in permission checks:

# Check for permission, explicitly providing the realm ("wiki"), id
# ("WikiStart") and version ("20"). All of these are optional but you must
# provide all components up to the most specific you require. eg. if you 
# wish to restrict 'WikiStart' you must provide ('wiki', 'WikiStart').
if 'WIKI_MODIFY' in req.perm('wiki', 'WikiStart', 20):
    ...

# A resource descriptor can be created and reused for the purpose.
page_resource = Resource('wiki', 'WikiStart', 20)
if 'WIKI_MODIFY' in req.perm(page_resource):
    ...

# Assert that user has permission
req.perm(page_resource).require('WIKI_MODIFY')
# or ...
req.perm(page_resource).require('WIKI_MODIFY')

Implementing a custom security policy

Your plugin must implement this interface:

class IPermissionPolicy(Interface):
    """A security policy provider used for fine grained permission checks."""

    def check_permission(action, username, resource, perm):
        """Check that the action can be performed by username on the resource
        ...

See trac.perm.IPermissionPolicy source code for much more information.

Testing the features

An example policy based on an Authz-style system has been added. See trunk/sample-plugins/permissions/authz_policy.py for details. (See also trunk/sample-plugins/permissions for more samples.)

  • Install ConfigObj (required).
  • Copy this file in your plugins directory
  • Plonk a authzpolicy.conf file somewhere.
  • Update your trac.ini:
    [trac]
    ...
    permission_policies = AuthzPolicy, DefaultPermissionPolicy
    
    [authz_policy]
    authz_file = /some/trac/env/conf/authzpolicy.conf
    
    [components]
    ...
    authz_policy = enabled
    
  • Finally, restart your web server.

Note that the order in which permission policies are specified is quite critical, as policies will be examined in the sequence provided.

A policy will return either True, False or None for a given permission check. Only if the return value is None will the next permission policy be consulted. If no policy explicitly grants the permission, the final result will be False (i.e. no permission).

For example, if the authz_file contains:

[wiki:WikiStart@*]
* = VIEW

[wiki:PrivatePage@*]
john = VIEW
* =

and the default permissions are set like this:

john           WIKI_VIEW
jack           WIKI_VIEW
# anonymous has no WIKI_VIEW

Then:

  • All versions of WikiStart will be viewable by everybody (including anonymous)
  • PrivatePage will be viewable only by john
  • other pages will be viewable only by john and jack

See also: TracFineGrainedPermissions, WikiContext

Last modified 2 years ago Last modified on Feb 9, 2012 11:18:55 PM