Creating a Chromium Extension to Modify SNI and Host Headers
Yes, it's possible to create a Manifest V3 extension that modifies the SNI (Server Name Indication) while keeping the original Host header, similar to Chromium's --host-rules functionality. Here's how you could approach this:
Key Components Needed
- Declarative Net Request API - For modifying requests
- Web Request API - For more complex modifications (though limited in MV3)
- Background Service Worker - To handle the logic
Implementation Approach
1. Manifest.json Setup
{
"manifest_version": 3,
"name": "Host Header Modifier",
"version": "1.0",
"permissions": [
"declarativeNetRequest",
"declarativeNetRequestWithHostAccess",
"webRequest",
"webRequestAuthProvider"
],
"host_permissions": [
"*://*.github.io/*"
],
"background": {
"service_worker": "background.js"
}
}
2. Using Declarative Net Request (Preferred in MV3)
For simple cases where you just need to modify the Host header:
// In your background.js
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
id: 1,
priority: 1,
action: {
type: 'modifyHeaders',
requestHeaders: [
{ header: 'Host', operation: 'set', value: 'bbb.github.io' }
]
},
condition: {
urlFilter: 'aaa.github.io',
resourceTypes: ['main_frame', 'sub_frame', 'xmlhttprequest']
}
}],
removeRuleIds: [1] // Remove existing rules if any
});
3. For SNI Modification (More Complex)
Modifying SNI directly isn't exposed in extension APIs, but you can achieve similar results by:
- DNS Redirection Approach:
- Intercept requests to
bbb.github.io
- Resolve
aaa.github.io to get its IP
- Make the request to that IP while keeping Host header as
bbb.github.io
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (details.url.includes('bbb.github.io')) {
// Replace the domain in the URL but keep Host header
const newUrl = details.url.replace('bbb.github.io', 'aaa.github.io');
return { redirectUrl: newUrl };
}
},
{ urls: ["*://bbb.github.io/*"] },
["blocking"]
);
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
if (details.url.includes('aaa.github.io')) {
// Find and modify the Host header
for (let i = 0; i < details.requestHeaders.length; i++) {
if (details.requestHeaders[i].name.toLowerCase() === 'host') {
details.requestHeaders[i].value = 'bbb.github.io';
break;
}
}
return { requestHeaders: details.requestHeaders };
}
},
{ urls: ["*://aaa.github.io/*"] },
["blocking", "requestHeaders"]
);
4. Limitations and Considerations
-
SNI Modification: The actual TLS SNI cannot be directly modified by extensions. The above approach works by making the request to the "real" server while keeping the original Host header.
-
HTTPS Challenges: Some servers may reject requests where the Host header doesn't match the certificate.
-
CORS Issues: You might encounter CORS problems with this approach.
-
Performance Impact: Redirecting requests adds latency.
Alternative Approach
If you need more control over network requests, consider:
-
Using a local proxy: The extension could configure system proxy settings to route through a local proxy that modifies requests.
-
WebSocket-based solution: Have a background page that fetches content via WebSocket from a server that can modify headers.
Conclusion
While you can't directly modify SNI in a Chrome extension, you can achieve similar functionality by redirecting requests while preserving the original Host header. The Declarative Net Request API is the most future-proof approach in Manifest V3, though it has some limitations compared to the older webRequest API.
Would you like me to elaborate on any specific part of this implementation?