For historical reasons, cookies contain a number of security and privacy infelicities. For example, a server can indicate that a given cookie is intended for "secure" connections, but the Secure attribute does not provide integrity in the presence of an active network attacker. Similarly, cookies for a given host are shared across all the ports on that host, even though the usual "same-origin policy" used by web browsers isolates content retrieved via different ports.
// In-memory storage for sessions (in production, use Redis or database) const sessions = {};
// Parse cookies from request header functionparseCookies(request) { const cookies = {}; if (request.headers.cookie) { request.headers.cookie.split(';').forEach(cookie => { const parts = cookie.trim().split('='); if (parts.length === 2) { cookies[parts[0]] = parts[1]; } }); } return cookies; }
// Generate a random session ID functiongenerateSessionId() { returnMath.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); }
const server = http.createServer((req, res) => { const parsedUrl = url.parse(req.url, true); const path = parsedUrl.pathname; const cookies = parseCookies(req);
// Set CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Credentials', 'true'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (path === '/set-session' && req.method === 'GET') { // Set session endpoint const sessionId = generateSessionId(); // Store the feature value with the session ID sessions[sessionId] = FEATURE_VALUE; // Set the session ID as a cookie res.setHeader('Set-Cookie', [`sessionId=${sessionId}; HttpOnly; Path=/; Max-Age=3600`]); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: true, sessionId: sessionId, message: 'Session set with feature value' })); } elseif (path === '/validate-session' && req.method === 'GET') { // Validate session endpoint const sessionId = cookies.sessionId; if (!sessionId) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ valid: false, message: 'No session cookie found' })); return; } const storedFeatureValue = sessions[sessionId]; if (!storedFeatureValue) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ valid: false, message: 'Invalid session' })); return; } // Check if the stored feature value matches the expected one const isValid = storedFeatureValue === FEATURE_VALUE; res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ valid: isValid, message: isValid ? 'Session valid' : 'Feature value mismatch' })); } else { res.writeHead(404, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Not found' })); } });
server.listen(PORT, () => { console.log(`Server running on port ${PORT}`); console.log(`Feature value: ${FEATURE_VALUE}`); console.log('Endpoints:'); console.log(' GET /set-session - Sets a session with the feature value'); console.log(' GET /validate-session - Validates session using cookie'); });