Skip to content

fix(policy): handle unmapped HTTP methods in methodToAction (HEAD, OPTIONS) #3137

@PierreBrisorgueil

Description

@PierreBrisorgueil

Context

Raised by Copilot review on #3134.

Problem

lib/middlewares/policy.js maps HTTP methods to CASL actions:

const methodToAction = {
  get: 'read', post: 'create', put: 'update', patch: 'update', delete: 'delete',
};

HEAD and OPTIONS are not mapped. When such a request hits policy.isAllowed, action resolves to undefined and is passed to ability.can(undefined, subject), which can produce incorrect authorization decisions or a runtime error depending on CASL's input validation.

Fix

Add explicit mappings for unmapped methods:

const methodToAction = {
  get: 'read', post: 'create', put: 'update', patch: 'update', delete: 'delete',
  head: 'read', options: 'read',
};

Or add a fallback that denies unknown methods by default.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions