Set isSubsetOf and isDisjointFrom Check Policy

Verify required scopes and mutual-exclusion rules cleanly with ES2025 Set.prototype.isSubsetOf, isSupersetOf, and isDisjointFrom without array copies.

JavascriptSecurity Boundaries

Code

snippet
const userScopes = new Set(['read:orders', 'write:orders', 'export:csv']);
const requiredForExport = new Set(['read:orders', 'export:csv']);
const restrictedAdminScopes = new Set(['delete:tenant', 'impersonate']);

// Check policy preconditions without converting Sets into temporary arrays
const hasAccess = requiredForExport.isSubsetOf(userScopes);
const userCoversAll = userScopes.isSupersetOf(requiredForExport);
const noConflict = userScopes.isDisjointFrom(restrictedAdminScopes);

console.log('hasAccess:', hasAccess, '| coversAll:', userCoversAll, '| noConflict:', noConflict);

const elevated = new Set([...userScopes, 'impersonate']);
console.log('elevated safe?:', elevated.isDisjointFrom(restrictedAdminScopes));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

hasAccess: true | coversAll: true | noConflict: true
elevated safe?: false

Logic trace — Set isSubsetOf and isDisjointFrom Check Policy

The file’s state is a handful of top-level bindings: userScopes, requiredForExport, restrictedAdminScopes, hasAccess.

Caveats

  • No third-party code is loaded at any point while this runs.

Using it

The run emits 2 labeled sections — hasAccess:, elevated safe?: — one per step.

Key statements

const userScopes = new Set(['read:orders', 'write:orders', 'export:csv']);
const restrictedAdminScopes = new Set(['delete:tenant', 'impersonate']);
const requiredForExport = new Set(['read:orders', 'export:csv']);

Interactive Tools for This Topic

FAQ

Is anything beyond the standard library required?
Nothing beyond the standard library — the snippet is dependency-free. Its state lives in module-level bindings: `userScopes`, `requiredForExport`, `restrictedAdminScopes`.