Blog | G5 Cyber Security

Secure Data Passing: Beyond URL Encryption

TL;DR

URL parameter encryption is often a weak security measure. This guide shows better ways to pass data securely, including using POST requests, cookies (with appropriate flags), and server-side sessions. We’ll cover the pros and cons of each approach.

1. Why URL Encryption Isn’t Enough

Encrypting data in a URL seems like a good idea, but it has several drawbacks:

2. Using POST Requests

POST requests send data in the body of the HTTP request instead of as part of the URL. This is a much more secure approach.

  1. Form Submission: Use an HTML form with method="post" to submit data.
  2. JavaScript (Fetch/Axios): Send data using JavaScript’s fetch or libraries like axios.
// Example using fetch
fetch('/your-endpoint', {
  method: 'POST',
  body: JSON.stringify({ key1: 'value1', key2: 'value2' }),
  headers: { 'Content-Type': 'application/json' }
});

Pros: More secure than URL encryption, widely supported.

Cons: Requires a server endpoint to handle the POST request. Data isn’t directly bookmarkable or shareable via URLs.

3. Secure Cookies

Cookies can store data on the user’s browser. Use them carefully with these flags:

// Example setting a secure cookie in PHP
setcookie('my_data', 'sensitive_value', [
    'expires' => time() + 3600,
    'path' => '/',
    'domain' => '.example.com',
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Strict'
]);

Pros: Can store more data than URLs, automatically sent with requests.

Cons: Vulnerable to XSS if HttpOnly is not set. Size limits apply (typically around 4KB per cookie). Requires careful handling of security flags.

4. Server-Side Sessions

Sessions store data on the server and use a session ID in a cookie to identify the user. This is generally the most secure option for sensitive information.

  1. Session Start: Begin a session using your server-side language (e.g., session_start() in PHP).
  2. Store Data: Store data in the $_SESSION array (PHP) or equivalent for other languages.
// Example storing data in a session using PHP
session_start();
$_SESSION['user_id'] = 123;
$_SESSION['username'] = 'john.doe';

Pros: Most secure option, stores sensitive data on the server.

Cons: Requires server-side support, can impact performance if not managed correctly. Session IDs are still vulnerable to hijacking (use strong session ID generation and proper security measures).

5. Choosing the Right Method

Always combine these techniques with other cyber security best practices like input validation, output encoding, and regular security audits.

Exit mobile version