Blog | G5 Cyber Security

Controlling Access with Ownership

TL;DR

This guide explains how to manage access permissions based on ownership in a complex system. We’ll cover identifying owners, defining permission levels, and implementing checks within your application code.

1. Identify Owners

First, you need to clearly define who owns what resources. This could be users, groups, or even other systems. A common approach is to store ownership information in a database table alongside the resource itself.

-- Example SQL table for resources with owners
CREATE TABLE resources (
    id INT PRIMARY KEY,
    name VARCHAR(255),
    owner_id INT,
    FOREIGN KEY (owner_id) REFERENCES users(id)
);

Consider these points:

2. Define Permission Levels

Next, establish the different levels of access users might need. Common examples include:

You can represent these permissions using numbers (e.g., 1 = Read, 2 = Write, 4 = Delete, 8 = Admin) or strings (e.g., ‘read’, ‘write’, ‘delete’, ‘admin’).

3. Implement Access Checks in Your Code

This is where you enforce the permissions based on ownership. The core logic involves these steps:

  1. Get Resource Owner: Retrieve the owner ID associated with the resource being accessed.
  2. Identify Current User: Determine who is attempting to access the resource.
  3. Check Ownership: Compare the current user’s ID (or group membership) against the resource owner ID.
  4. Verify Permission Level: If the user owns the resource, check if they have the required permission level for the requested action.

Here’s a simplified Python example:

def has_permission(resource, user, permission):
    owner_id = resource['owner_id']
    if user['id'] == owner_id:
        # Check if the user has sufficient permissions (e.g., from a database lookup)
        user_permissions = get_user_permissions(user['id'], resource['id'])
        return permission in user_permissions
    else:
        return False

Important Considerations:

4. Handling Indirect Ownership

If you support indirect ownership (e.g., group membership), your access check logic needs to be extended.

def has_permission(resource, user, permission):
    owner_id = resource['owner_id']
    if user['id'] == owner_id:
        user_permissions = get_user_permissions(user['id'], resource['id'])
        return permission in user_permissions
    else:
        # Check if the user is a member of a group that owns the resource
        groups = get_user_groups(user['id'])
        for group in groups:
            if group['owner_of'] == resource['id']:
                return permission in group['permissions']
        return False

This example assumes you have functions to retrieve user permissions and user groups.

5. Auditing

Log all access attempts, including successful and failed ones. This provides valuable information for security monitoring and troubleshooting.

Exit mobile version