Skip to content

Commit

Permalink
Merge pull request #9 from RootUp/performance-update
Browse files Browse the repository at this point in the history
some optimizations
  • Loading branch information
RootUp authored Oct 13, 2024
2 parents 3d723e8 + f1fd0c9 commit dc87a3a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
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.');
}
});
});
});
});

0 comments on commit dc87a3a

Please sign in to comment.