Enterprise systems require high security and throughput. In this module, we will explore upload sanitization, JNDI connection pools, and Tomcat thread scaling.
5.1. Secure File Uploads & JNDI Connection Pooling
Definition: Securing upload processes against directory traversal, and managing pre-opened database connections via JNDI.
Core Working Principles:
- Path Sanitization: Sanitize file names to prevent path traversal attacks (e.g.
../../etc/passwd). - JNDI Connection Pooling: Tomcat manages a pool of pre-opened database connections. Look up the datasource in Java using JNDI.
// JNDI lookup
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");
try (Connection conn = ds.getConnection()) { ... }
5-Marks Exam Scoring Points
- RCE Prevention: Prevents remote code execution by storing files outside the web root.
- Sanitizes file paths to prevent attackers from overwriting system files.
- JNDI connection pooling eliminates the overhead of opening database connections for every request.
5.2. Tomcat Concurrency and Thread Pools
Definition: Analyzing Tomcat's thread allocation model to understand how request processing scales.
Core Working Principles:
- Worker Threads: Tomcat uses a thread pool (default 200 threads) to process concurrent requests.
- Blocking: Worker threads block while waiting for slow database queries or client uploads, consuming system resources.
5-Marks Exam Scoring Points
- Thread pooling prevents the overhead of creating new threads for every request.
- Blocking I/O processes one request per thread, limiting concurrent connections.
- Non-blocking configurations allow a small number of threads to handle thousands of concurrent requests.