Skip to content

Security Directives

Security directives help you manage permissions and sanitize content.

v-permission

Permission-based element control for access management.

Basic Usage

vue
<template>
  <!-- Single permission -->
  <button v-permission="'admin'">Admin Only</button>

  <!-- Multiple permissions (OR logic by default) -->
  <button v-permission="['admin', 'editor']">Admin or Editor</button>

  <!-- AND logic with mode option -->
  <button v-permission="{ value: ['read', 'write'], mode: 'every' }">
    Requires Read & Write
  </button>

  <!-- Different actions -->
  <button v-permission="{ value: 'admin', action: 'disable' }">
    Disabled for non-admin
  </button>
</template>

Configuration

Configure the permission directive in your app:

typescript
import { configurePermission } from 'directix'

configurePermission({
  getPermissions: () => store.getters.permissions,
  getRoles: () => store.getters.roles,
  roleMap: {
    admin: ['*'],
    editor: ['read', 'write'],
    viewer: ['read']
  }
})

API

OptionTypeDefaultDescription
valuestring | string[]-Permission(s) to check (required)
mode'some' | 'every''some'Logic for multiple permissions
action'remove' | 'disable' | 'hide''remove'Action when denied
checkFunction-Custom check function

v-sanitize

Sanitize HTML content to prevent XSS attacks.

Basic Usage

vue
<template>
  <!-- Basic sanitization -->
  <div v-sanitize v-html="userContent"></div>

  <!-- With allowed tags -->
  <div v-sanitize="{ allowedTags: ['b', 'i', 'p'] }" v-html="userContent"></div>
</template>

Default Behavior

By default, v-sanitize:

  • Allows safe tags: b, i, u, strong, em, br, p, span, div
  • Allows safe attributes: title, alt, href, src
  • Removes dangerous tags: script, iframe, object, embed, form, etc.
  • Removes dangerous attributes: onclick, onerror, onload, etc.
  • Removes javascript: URLs

API

OptionTypeDefaultDescription
allowedTagsstring[]Safe subsetAllowed HTML tags
allowedAttributesstring[]Safe subsetAllowed HTML attributes
allowDataUrlsbooleanfalseAllow data: URLs
allowStylesbooleanfalseAllow style attribute
allowClassbooleanfalseAllow class attribute
allowIdbooleanfalseAllow id attribute
handlerFunction-Custom sanitize function

Released under the MIT License.