Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some optimizations #9

Merged
merged 4 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions SmuggleShield/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function memoize(fn, resolver) {
if (cache.has(key)) return cache.get(key);
const result = fn(...args);
cache.set(key, result);
if (cache.size > 1000) {
const oldestKey = cache.keys().next().value;
cache.delete(oldestKey);
}
return result;
};
}
Expand Down Expand Up @@ -109,9 +113,9 @@ chrome.webRequest.onHeadersReceived.addListener(

setInterval(() => {
const now = Date.now();
urlCache.forEach((data, url) => {
for (const [url, data] of urlCache) {
if (now - data.timestamp > config.cacheDurationMs) {
urlCache.delete(url);
}
});
}
}, config.cacheDurationMs);
13 changes: 10 additions & 3 deletions SmuggleShield/content.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class HTMLSmugglingBlocker {
constructor() {
this.blocked = false; this.suspiciousPatterns = [
this.blocked = false;
this.suspiciousPatterns = [
{ pattern: /atob\s*\([^)]+\).*new\s+uint8array/is, weight: 3 },
{ pattern: /atob\s*\(\s*['"]([A-Za-z0-9+/=]{100,})['"].*\)/i, weight: 3 },
{ pattern: /new\s+blob\s*\(\s*\[\s*(?:data|atob\s*\()/i, weight: 3 },
Expand Down Expand Up @@ -45,6 +46,11 @@ class HTMLSmugglingBlocker {
];
this.threshold = 4;
this.setupListeners();

this.suspiciousPatterns = this.suspiciousPatterns.map(({pattern, weight}) => ({
pattern: new RegExp(pattern, 'is'),
weight
}));
}

setupListeners() {
Expand Down Expand Up @@ -81,12 +87,13 @@ class HTMLSmugglingBlocker {
let score = 0;
const detectedPatterns = [];

this.suspiciousPatterns.forEach(({pattern, weight}) => {
for (const {pattern, weight} of this.suspiciousPatterns) {
if (pattern.test(htmlContent)) {
score += weight;
detectedPatterns.push(pattern.toString());
if (score >= this.threshold) break; // Early exit if threshold is reached
}
});
}

if (score >= this.threshold) {
console.log("HTML Smuggling Blocker: Suspicious content detected");
Expand Down
5 changes: 4 additions & 1 deletion SmuggleShield/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ document.addEventListener('DOMContentLoaded', function() {
const a = document.createElement('a');
a.href = url;
a.download = 'smuggleshield_logs.json';
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} else {
alert('No logs available to export.');
}
});
});
});
});