Hey everyone! I'm running some self-hosted software on my home server, which includes both a backend and a frontend part that uses JavaScript. I access it through my PC browser as a web app, and since it stores some personal data, I want to keep that data private. I need to make sure this app only communicates within my local network (LAN) and doesn't connect to the internet at all.
On the backend, I've set up a firewall to restrict connections, but I'm not sure how to handle the JavaScript that's running on the client side. As far as I know, JavaScript could potentially send my data to external locations if it wanted to. I can't firewall my entire PC or block the browser completely because I still need internet access, and disabling JavaScript would break the functionality of the web app.
I've thought about inspecting the JavaScript code on the server to check for any external IPs or URLs, but I'm worried I might miss something important. I've also considered using developer tools to monitor web communications, but I'd prefer a more permanent solution like a firewall to ensure long-term security. I even toyed with the idea of creating a Progressive Web App (PWA) as an alternative, but I haven't figured out how to do that yet.
So, does anyone have any ideas on how to effectively restrict a specific website's JavaScript from communicating with the internet? Thanks in advance!
2 Answers
You can actually use Content Security Policy (CSP) to restrict what JavaScript can do. By setting a CSP header, you can block connections to outside domains. Something like this could work:
`Content-Security-Policy: default-src 'self'; script-src 'self'; connect-src 'self'`
This way, JavaScript can only load and connect to your own site. If you want to prevent all external connections, you could set it to `connect-src 'none'`. Just keep in mind that you might need to modify the source code to implement this, but adding it to the main JS file could be a start!
JavaScript is designed to follow what you code it to do. If your application’s code doesn’t explicitly send requests outside of your local network, it shouldn't do so. However, be cautious about the libraries you use; if they make calls you aren't aware of, they could potentially send your data outside your LAN. It’s really about ensuring your code is set up right to not need external access in the first place rather than just trying to block that access later on.
But what if I don’t know how the app works? I can’t just assume the code is safe if I haven't checked it myself.
I get your point, but isn't it also a good practice to limit access to only what's necessary? If something in the code changes in the future, it might open up a vulnerability that could allow connections to outside servers.

That sounds promising! I’ll give it a shot and see if putting it in the main JS file does the trick.