Index of every page on this site: llms.txt
All skills
Run it yourself

Frontend security patterns including XSS prevention, Content Security Policy, CSRF protection, input sanitization, authentication token handling, dependency auditing, and secure headers. Use when hardening a web app, handling user input, managing auth tokens, configuring CSP, or when the user asks about web security, XSS, CSRF, or secure coding practices.

Use this Skill: https://skilld.dev/gh/grahamcrackers/skills/security-patterns

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

SKILL.md

≈94 tokens for metadata: the name and description. ≈2.2k when used: this file.

Description uses 4.7% of example budget

Before choosing a Skill, your Agent reads its name and description. All available Skills share that space.

  • A shorter description leaves more room for other Skills. This entry exceeds our 1% size suggestion.

In our Claude Code example, all Skill names and descriptions share 8,000 characters. This Skill uses ≈377 characters, or 4.7%.

The 1% threshold is a size suggestion. Longer descriptions can still fit.

Your model, settings, and other Skills decide how much text your Agent can read.

Example settings and source

The example uses a 200k-token context and default Claude Code settings. The count includes the name, description, separators, and when_to_use when present. Codex also counts local file paths.

Skit's source and limits: Codex 0.160.1, Claude Code 2.1.292.

Frontend Security Patterns

XSS Prevention

React's Built-In Protection

React escapes all values rendered in JSX by default. This is safe:

<p>{userInput}</p>                    // escaped — safe
<div title={userInput}>...</div>      // escaped — safe

Dangerous Patterns

// DANGEROUS — renders raw HTML
<div dangerouslySetInnerHTML={{ __html: userInput }} />

// DANGEROUS — inline event handlers from user data
<a href={userProvidedUrl}>Link</a>    // javascript: URLs are dangerous

// DANGEROUS — dynamic script injection
document.innerHTML = userInput;
element.insertAdjacentHTML("beforeend", userInput);

Mitigations

Sanitize HTML when you must render user-provided markup:

import DOMPurify from "dompurify";

const cleanHtml = DOMPurify.sanitize(dirtyHtml, {
  ALLOWED_TAGS: ["b", "i", "em", "strong", "a", "p", "br"],
  ALLOWED_ATTR: ["href", "title"],
});

<div dangerouslySetInnerHTML={{ __html: cleanHtml }} />

Validate URLs before rendering:

function isSafeUrl(url: string): boolean {
  try {
    const parsed = new URL(url);
    return ["http:", "https:", "mailto:"].includes(parsed.protocol);
  } catch {
    return false;
  }
}

<a href={isSafeUrl(url) ? url : "#"}>Link</a>

Content Security Policy (CSP)

HTTP Header

Content-Security-Policy:
  default-src 'self';
  script-src 'self';
  style-src 'self' 'unsafe-inline';
  img-src 'self' data: https:;
  font-src 'self';
  connect-src 'self' https://api.example.com;
  frame-ancestors 'none';
  base-uri 'self';
  form-action 'self';

Meta Tag (Fallback)

<meta
    http-equiv="Content-Security-Policy"
    content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline';"
/>

Key Directives

Directive Purpose
default-src Fallback for all resource types
script-src Allowed script sources
style-src Allowed style sources
connect-src Allowed fetch/XHR/WebSocket targets
img-src Allowed image sources
frame-ancestors Who can embed this page ('none' = no iframes)
base-uri Restricts <base> tag URLs
form-action Restricts form submission targets

Start strict (default-src 'self') and whitelist only what's needed. Use report-uri or report-to to monitor violations in production before enforcing.

CSRF Protection

Token-Based

Include a CSRF token in state-changing requests:

const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content;

fetch("/api/transfer", {
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        "X-CSRF-Token": csrfToken,
    },
    body: JSON.stringify(data),
});

SameSite Cookies

Set-Cookie: session=abc123; SameSite=Lax; Secure; HttpOnly; Path=/
  • SameSite=Lax — cookie sent on top-level navigations but not cross-site subrequests (default in modern browsers).
  • SameSite=Strict — cookie never sent cross-site (may break OAuth flows).
  • Always combine with Secure (HTTPS only) and HttpOnly (no JS access).

Authentication Token Handling

Storage Options

Storage XSS Risk CSRF Risk Use When
HttpOnly cookie None (not accessible to JS) Mitigated with SameSite Server can set cookies
localStorage High (accessible to any script) None Never for auth tokens
Memory (variable) Low (cleared on page close) None Short-lived SPA sessions

Prefer HttpOnly cookies for auth tokens — they're invisible to JavaScript and automatically sent with requests.

Token Refresh

let isRefreshing = false;
let pendingRequests: Array<() => void> = [];

async function fetchWithAuth(url: string, options: RequestInit = {}) {
    const response = await fetch(url, { ...options, credentials: "include" });

    if (response.status === 401) {
        if (!isRefreshing) {
            isRefreshing = true;
            await fetch("/api/auth/refresh", {
                method: "POST",
                credentials: "include",
            });
            isRefreshing = false;
            pendingRequests.forEach((cb) => cb());
            pendingRequests = [];
        }

        return new Promise<Response>((resolve) => {
            pendingRequests.push(() => {
                resolve(fetch(url, { ...options, credentials: "include" }));
            });
        });
    }

    return response;
}

Logout

Clear tokens and invalidate the session on the server:

async function logout() {
    await fetch("/api/auth/logout", {
        method: "POST",
        credentials: "include",
    });
    window.location.href = "/login";
}

Input Validation

Always validate on both client and server — client validation is for UX, server validation is for security.

import { z } from "zod";

const UserInputSchema = z.object({
    name: z.string().min(1).max(100).trim(),
    email: z.string().email().max(254),
    bio: z.string().max(500).optional(),
    website: z
        .string()
        .url()
        .optional()
        .refine((url) => !url || /^https?:/.test(url), "Must be an HTTP(S) URL"),
});

Never trust client-side validation alone — always validate and sanitize on the server.

Secure Headers

Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
Header Purpose
Strict-Transport-Security Force HTTPS
X-Content-Type-Options: nosniff Prevent MIME type sniffing
X-Frame-Options: DENY Prevent clickjacking via iframes
Referrer-Policy Control referer header leakage
Permissions-Policy Disable unused browser APIs

Dependency Security

Audit Regularly

npm audit
npm audit --production  # only production deps

Automated Scanning

Enable GitHub Dependabot or Snyk for automated vulnerability alerts:

# .github/dependabot.yml
version: 2
updates:
    - package-ecosystem: "npm"
      directory: "/"
      schedule:
          interval: "weekly"
      open-pull-requests-limit: 10

Lock Files

Always commit lock files (package-lock.json, pnpm-lock.yaml). Use npm ci in CI to install from the lockfile exactly.

Sensitive Data

  • Never commit secrets — use environment variables and .env.local (gitignored).
  • Never expose server secrets to the client — in Vite, only VITE_* vars are client-visible.
  • Never log sensitive data — PII, tokens, passwords should never appear in console.log or error tracking.
  • Mask sensitive fields in error reporting (Sentry, LogRocket):
Sentry.init({
    beforeSend(event) {
        if (event.request?.headers) {
            delete event.request.headers["Authorization"];
            delete event.request.headers["Cookie"];
        }
        return event;
    },
});

Third-Party Scripts

  • Load third-party scripts with async or defer.
  • Use Subresource Integrity (SRI) for CDN-hosted scripts:
<script src="https://cdn.example.com/lib.js" integrity="sha384-abc123..." crossorigin="anonymous"></script>
  • Sandbox third-party content in iframes with restrictive sandbox attributes.
  • Audit third-party scripts regularly — they run with full page access.

Checklist

  • No dangerouslySetInnerHTML without DOMPurify
  • CSP header configured (at minimum default-src 'self')
  • Auth tokens in HttpOnly cookies, not localStorage
  • SameSite cookies for CSRF protection
  • Input validated on both client and server
  • Security headers set (HSTS, X-Content-Type-Options, X-Frame-Options)
  • Dependencies audited regularly
  • No secrets in client-visible code or environment variables
  • Third-party scripts use SRI

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning8mo4 checks · Risk SAFE
  • Gen Agent Trust Hub8mo

    The skill is an educational resource containing frontend security best practices and patterns. It provides safe implementations for XSS prevention, CSP, and authentication handling.

  • Socket8mo

    No alerts

  • Snyk8mo

    Risk: LOW · No issues

  • Runlayer8mo

    1/1 file flagged

Signed by skilld at ca83de4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 9 months ago

README badge

README badge for grahamcrackers/skills/security-patterns