HIGH broken access controlrestify

Broken Access Control in Restify

How Broken Access Control Manifests in Restify

Broken Access Control in Restify APIs typically emerges through predictable patterns that exploit the framework's middleware-based architecture. The most common vulnerability occurs when authentication middleware is improperly chained or skipped entirely for certain routes.

const restify = require('restify');
const server = restify.createServer();

// Authentication middleware defined but NOT applied to all routes
server.use((req, res, next) => {
  if (!req.authorization) {
    return next(new restify.ForbiddenError('Authentication required'));
  }
  next();
});

server.get('/public', (req, res) => {
  res.send(200, { message: 'Public data' });
});

server.get('/admin', (req, res) => {
  res.send(200, { message: 'Admin data' });
}); // <-- Missing authentication check!

This creates a classic BOLA (Broken Object Level Authorization) scenario where any authenticated user can access admin endpoints. Restify's middleware chain can be bypassed if developers assume authentication is global when it's only applied to specific routes.

Another Restify-specific pattern involves improper use of route parameters without authorization checks:

server.get('/users/:userId', (req, res) => {
  const userId = req.params.userId;
  // No check if user owns this resource or has permission
  db.getUserById(userId, (err, user) => {
    res.send(200, user);
  });
});

Property-level authorization failures are particularly insidious in Restify when handling JSON payloads:

server.put('/users/:userId', restify.plugins.bodyParser(), (req, res) => {
  const { role, email, password } = req.body;
  // No validation that user can modify role field
  db.updateUser(req.params.userId, { role, email, password }, (err) => {
    res.send(200);
  });
});

Privilege escalation often occurs when Restify's default error handling reveals sensitive information about resource existence or user roles through HTTP status codes and error messages.

Restify-Specific Detection

Detecting Broken Access Control in Restify requires both manual code review and automated scanning. The framework's middleware-based architecture creates specific detection patterns that middleBrick's scanner is designed to identify.

Manual detection should focus on middleware chain analysis. Look for routes that lack proper authentication middleware:

const routes = server.router.routes();
const authRoutes = routes.filter(r => r.method === 'GET' && r.path.includes('auth'));
// Compare against all routes to find missing auth

middleBrick's scanner specifically tests for these Restify patterns by:

  • Analyzing middleware chains to identify authentication gaps
  • Testing parameter-based access control by modifying user IDs in requests
  • Checking for privilege escalation by attempting to modify protected fields
  • Verifying rate limiting and session management implementation

The scanner's black-box approach is particularly effective for Restify APIs because it doesn't require source code access. It submits requests with modified parameters to test BOLA vulnerabilities:

// middleBrick would test patterns like:
const testBOLA = async (baseUrl) => {
  const response1 = await fetch(`${baseUrl}/users/123`);
  const response2 = await fetch(`${baseUrl}/users/456`);
  // Compare responses to detect unauthorized data access
};

For property-level authorization, middleBrick tests whether users can modify fields they shouldn't have access to by sending modified payloads and analyzing the server's response.

Restify-Specific Remediation

Effective remediation in Restify requires leveraging the framework's built-in middleware system and validation plugins. The most critical fix is implementing a robust authentication middleware that's applied to all protected routes:

const authenticate = (req, res, next) => {
  const authHeader = req.headers.authorization;
  if (!authHeader) {
    return next(new restify.ForbiddenError('Authentication required'));
  }
  
  const token = authHeader.split(' ')[1];
  jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
    if (err) {
      return next(new restify.ForbiddenError('Invalid token'));
    }
    req.user = decoded;
    next();
  });
};

// Apply to all routes except public ones
server.use('/api/*', authenticate);

For BOLA prevention, implement resource ownership validation:

const authorizeResource = (req, res, next) => {
  const resourceId = req.params.resourceId;
  const userId = req.user.id;
  
  db.checkOwnership(resourceId, userId, (err, isOwner) => {
    if (err || !isOwner) {
      return next(new restify.ForbiddenError('Access denied'));
    }
    next();
  });
};

// Chain middleware for protected routes
server.get('/api/resources/:resourceId', 
  authenticate, 
  authorizeResource, 
  (req, res) => {
    // Safe to access
    res.send(200);
  }
);

Property-level authorization requires schema validation using restify's built-in plugins:

const validation = require('restify-validation');
server.use(restify.plugins.bodyParser());
server.use(validation());

server.put('/api/users/:id', authenticate, (req, res, next) => {
  const { role, email } = req.body;
  
  // Check if user can modify role
  if (role && req.user.role !== 'admin') {
    return next(new restify.ForbiddenError('Cannot modify role'));
  }
  
  // Validate email format
  if (email && !validator.isEmail(email)) {
    return next(new restify.InvalidArgumentError('Invalid email'));
  }
  
  db.updateUser(req.params.id, { role, email }, (err) => {
    if (err) return next(err);
    res.send(200);
  });
});

middleBrick's GitHub Action can automatically scan your Restify API during CI/CD to ensure these fixes remain effective:

- name: Scan API Security
  uses: middlebrick/middlebrick-action@v1
  with:
    url: ${{ secrets.API_URL }}
    fail-on-severity: high
    token: ${{ secrets.MIDDLEBRICK_TOKEN }}

This integration ensures that any regression in access control gets caught before deployment, maintaining the security posture of your Restify application.

Frequently Asked Questions

How does middleBrick specifically detect Broken Access Control in Restify APIs?
middleBrick's scanner tests for BOLA by systematically modifying user IDs and resource parameters in requests to see if unauthorized data can be accessed. It analyzes the middleware chain structure to identify routes that lack proper authentication checks, and tests property-level authorization by attempting to modify protected fields like user roles or admin privileges. The scanner runs 12 parallel security checks and provides a detailed risk score with specific findings about where access control is broken.
Can middleBrick scan my Restify API without access to the source code?
Yes, middleBrick uses black-box scanning that works entirely from the API's URL endpoint. No source code, credentials, or agents are required. The scanner sends test requests to your Restify API endpoints, analyzes the responses, and identifies security vulnerabilities including Broken Access Control patterns. This makes it ideal for testing staging APIs, third-party services, or any REST API without needing internal access.