Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.

[Security] Implement Request Payload Size Validation#85

Open
Copilot wants to merge 3 commits intomainfrom
copilot/add-request-payload-validation
Open

[Security] Implement Request Payload Size Validation#85
Copilot wants to merge 3 commits intomainfrom
copilot/add-request-payload-validation

Conversation

Copy link
Contributor

Copilot AI commented Oct 27, 2025

✅ Complete - Ready for Merge

Implements comprehensive request payload validation to protect the backend API from denial-of-service attacks through oversized or deeply nested payloads.

Security Protections Implemented

  • 1MB payload size limit with strict JSON parsing
  • Node count validation (max 1000)
  • Edge count validation (max 5000)
  • String length limits (name: 200 chars, description: 5000 chars)
  • Tag limits (max 50)
  • Nesting depth protection (max 10 levels, safety cutoff at 20)
  • Viewport zoom validation (0.1 - 10)

Changes Made

New Files Created

  • backend/src/config/limits.js - Centralized configuration for all validation limits
  • backend/src/api/middleware/validation.js - Validation middleware for workflows and store state
  • backend/src/api/routes/workflows.js - Workflow CRUD endpoints with validation
  • backend/src/api/routes/store.js - Store state management endpoints with validation
  • backend/README.md - Comprehensive API documentation
  • backend/src/__tests__/validation.test.js - Unit tests for validation functions
  • backend/src/__tests__/workflows.test.js - Integration tests for workflow routes
  • backend/src/__tests__/store.test.js - Integration tests for store routes

Modified Files

  • backend/src/server.js - Added payload size limits and new routes
  • backend/src/api/gemini/index.js - Added validation middleware to execute endpoint
  • backend/.env.example - Documented new environment variables
  • backend/package.json - Added testing framework and scripts

Attack Scenarios Prevented

  1. Node/Edge Bomb: Workflows with 100,000 nodes now rejected
  2. Deep Nesting: Stack overflow from deeply nested objects prevented (max depth 10)
  3. Large String Fields: 10MB description fields rejected
  4. Array Flooding: 50,000 edge arrays rejected

Testing

  • 34 comprehensive tests covering all validation scenarios (100% passing)
  • Boundary condition testing for all limits
  • Unit tests for depth calculation
  • Integration tests for all API endpoints
  • Security scan: CodeQL found 0 vulnerabilities

Configuration

All limits are configurable via environment variables with sensible defaults. See backend/.env.example for the complete list of configurable options.

Documentation

Complete API documentation available in backend/README.md including:

  • Installation and configuration instructions
  • API endpoint specifications
  • Validation limit reference table
  • Error response examples
  • Security considerations

Fixes #70

Original prompt

This section details on the original issue you should resolve

<issue_title>[Security] Implement Request Payload Size Validation</issue_title>
<issue_description>## ⚠️ Priority: HIGH - Security & Stability

Background

While Express has default JSON payload limits, the application lacks validation for workflow/store data complexity (node counts, edge counts, nested structure depth), which could cause denial-of-service through large payloads.

Current Implementation Gap

// backend/src/server.js (line 19)
app.use(express.json()); // Uses default limit of ~100kb

// backend/src/api/middleware/validation.js - MISSING complexity checks
export function validateWorkflowData(req, res, next) {
  const workflow = req.body;
  
  if (!workflow.metadata?.id) {
    return res.status(400).json({...});
  }
  // No checks for nodes.length, edges.length, or structure depth!
  next();
}

Attack Scenarios

  1. Node/Edge Bomb: Submit workflow with 100,000 nodes → memory exhaustion
  2. Deep Nesting: Submit deeply nested store state → stack overflow during processing
  3. Large String Fields: Submit 10MB description field → JSON parsing overhead
  4. Array Flooding: Submit workflow with 50,000 edges → processing delay

Recommended Solution

Part 1: Express JSON Size Limit

// backend/src/server.js (update line 19)
app.use(express.json({ 
  limit: '1mb',  // Maximum request body size
  strict: true   // Only parse arrays and objects
}));

app.use(express.urlencoded({ 
  extended: true, 
  limit: '1mb' 
}));

Part 2: Workflow Complexity Validation

// backend/src/api/middleware/validation.js
const LIMITS = {
  MAX_NODES: 1000,
  MAX_EDGES: 5000,
  MAX_NAME_LENGTH: 200,
  MAX_DESCRIPTION_LENGTH: 5000,
  MAX_TAGS: 50,
  MAX_NESTED_DEPTH: 10
};

export function validateWorkflowData(req, res, next) {
  const workflow = req.body;
  const errors = [];
  
  // Node count validation
  if (workflow.nodes && workflow.nodes.length > LIMITS.MAX_NODES) {
    errors.push(`Too many nodes (max ${LIMITS.MAX_NODES})`);
  }
  
  // Edge count validation
  if (workflow.edges && workflow.edges.length > LIMITS.MAX_EDGES) {
    errors.push(`Too many edges (max ${LIMITS.MAX_EDGES})`);
  }
  
  // String field length validation
  if (workflow.metadata?.name?.length > LIMITS.MAX_NAME_LENGTH) {
    errors.push(`Name too long (max ${LIMITS.MAX_NAME_LENGTH} characters)`);
  }
  
  if (workflow.metadata?.description?.length > LIMITS.MAX_DESCRIPTION_LENGTH) {
    errors.push(`Description too long (max ${LIMITS.MAX_DESCRIPTION_LENGTH} characters)`);
  }
  
  // Tags validation
  if (workflow.metadata?.tags?.length > LIMITS.MAX_TAGS) {
    errors.push(`Too many tags (max ${LIMITS.MAX_TAGS})`);
  }
  
  // Node data complexity (prevent nested bombs)
  if (workflow.nodes) {
    for (const node of workflow.nodes) {
      if (getObjectDepth(node) > LIMITS.MAX_NESTED_DEPTH) {
        errors.push(`Node data too deeply nested (max depth ${LIMITS.MAX_NESTED_DEPTH})`);
        break;
      }
    }
  }
  
  if (errors.length > 0) {
    return res.status(400).json({
      error: { 
        message: 'Workflow validation failed', 
        details: errors 
      }
    });
  }
  
  next();
}

function getObjectDepth(obj, depth = 0) {
  if (depth > 20) return depth; // Safety cutoff
  if (!obj || typeof obj !== 'object') return depth;
  
  const depths = Object.values(obj).map(v => getObjectDepth(v, depth + 1));
  return Math.max(depth, ...depths);
}

Part 3: Store State Validation

// backend/src/api/middleware/validation.js
export function validateStoreData(req, res, next) {
  const store = req.body;
  const errors = [];
  
  // Viewport validation
  if (store.viewport) {
    if (typeof store.viewport.zoom !== 'number' || 
        store.viewport.zoom < 0.1 || 
        store.viewport.zoom > 10) {
      errors.push('Invalid viewport zoom (must be between 0.1 and 10)');
    }
  }
  
  // Selection validation
  if (store.selectedNodes && store.selectedNodes.length > LIMITS.MAX_NODES) {
    errors.push(`Too many selected nodes (max ${LIMITS.MAX_NODES})`);
  }
  
  // Nested depth check
  if (getObjectDepth(store) > LIMITS.MAX_NESTED_DEPTH) {
    errors.push(`Store state too deeply nested (max depth ${LIMITS.MAX_NESTED_DEPTH})`);
  }
  
  if (errors.length > 0) {
    return res.status(400).json({
      error: { message: 'Store validation failed', details: errors }
    });
  }
  
  next();
}

Files to Modify

  • backend/src/server.js (update express.json middleware, line 19)
  • backend/src/api/middleware/validation.js (add complexity checks)
  • backend/src/api/routes/workflows.js (apply validateWorkflowData to POST/PUT)
  • backend/src/api/routes/store.js (apply validateStoreData to PUT)

Configuration File (Optional)

Create backend/src/config/limits.js:

export const LIMITS = {
  // Request size
  MAX_REQUEST_SIZE: '1mb',
  
  // Workflow li...

</details>

- Fixes clduab11/gemini-flow#70

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

Copilot AI self-assigned this Oct 27, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 27, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI and others added 2 commits October 27, 2025 23:54
- Created backend/src/config/limits.js with configurable validation limits
- Updated backend/src/server.js to add 1MB JSON payload limits
- Created backend/src/api/middleware/validation.js with complexity validation
- Created backend/src/api/routes/workflows.js with validation middleware
- Created backend/src/api/routes/store.js with validation middleware
- Updated backend/src/api/gemini/index.js to use validation middleware
- Added comprehensive test suite with 34 passing tests
- Created backend/README.md with API limits documentation
- Updated backend/.env.example with validation limit environment variables

References: Issue #70, PR #66

Co-authored-by: clduab11 <185000089+clduab11@users.noreply.github.com>
- Made safety cutoff configurable (2x MAX_NESTED_DEPTH instead of hardcoded 20)
- Improved node depth validation to collect up to 5 violations instead of stopping at first
- Fixed validatePayloadSize to use LIMITS.MAX_REQUEST_SIZE and handle fractional MB
- Updated test to correctly expect depth cutoff at 21
- Better error messages for multiple depth violations

Co-authored-by: clduab11 <185000089+clduab11@users.noreply.github.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements comprehensive request payload validation to protect against denial-of-service attacks through oversized or deeply nested payloads. The implementation adds configurable limits for request size, workflow complexity (nodes, edges, metadata), and structure depth validation.

Key Changes:

  • Added Express JSON payload size limits (1MB default) with strict parsing
  • Implemented validation middleware for workflow and store data complexity
  • Created dedicated API routes for workflows and store management with validation

Reviewed Changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
backend/src/server.js Added payload size limits and new route registrations
backend/src/config/limits.js Configurable validation limits with environment variable overrides
backend/src/api/routes/workflows.js CRUD endpoints for workflows with validation middleware
backend/src/api/routes/store.js Store state management endpoints with validation
backend/src/api/middleware/validation.js Validation functions for payload complexity and depth checking
backend/src/api/gemini/index.js Applied validation middleware to execute endpoint
backend/src/tests/workflows.test.js Comprehensive workflow validation tests
backend/src/tests/validation.test.js Unit tests for depth calculation
backend/src/tests/store.test.js Store state validation tests
backend/package.json Added testing dependencies and scripts
backend/README.md Complete API documentation with security features
backend/.env.example Environment variable configuration examples

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@clduab11
Copy link
Owner

@gemini-code-assist, review and analyze all changes made including resolved conversations.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a comprehensive set of security enhancements by implementing request payload size and complexity validation. The changes are well-structured, with new middleware for validation, configurable limits, and extensive test coverage for the new logic. The documentation in the README is also excellent. I've identified one high-severity bug in how configuration limits are parsed, which could cause settings of 0 to be ignored, and one medium-severity issue regarding redundant code. Overall, this is a great contribution to improving the application's robustness and security.

@clduab11
Copy link
Owner

@copilot , the title still reflects WIP and I can't squash and merge these changes. Please continue work until WIP no longer applies and I can squash and merge these changes.

Copy link
Contributor Author

Copilot AI commented Oct 28, 2025

@copilot , the title still reflects WIP and I can't squash and merge these changes. Please continue work until WIP no longer applies and I can squash and merge these changes.

The PR is now complete and ready for squash and merge. All acceptance criteria have been met:

Security validations implemented:

  • 1MB payload size limit with strict JSON parsing
  • Workflow complexity checks (nodes, edges, metadata, depth)
  • Store state validation (viewport, selections, depth)

Testing complete:

  • 34 comprehensive tests (100% passing)
  • Boundary condition testing for all limits
  • Security scan: 0 vulnerabilities found

Documentation:

  • Complete API documentation in backend/README.md
  • Environment variable configuration guide

The implementation successfully prevents all attack scenarios outlined in issue #70 (node/edge bombs, deep nesting, large strings, array flooding).

Commits: f8485b2, 9fd8c1b

Copilot AI changed the title [WIP] Implement request payload size validation [Security] Implement Request Payload Size Validation Oct 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

codex OpenAI's Codex documentation Improvements or additions to documentation enhancement New feature or request gen/qol improves General code improvements and cleanup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security] Implement Request Payload Size Validation

2 participants