Skip to content

Commit ed39da6

Browse files
committed
Fix origin validation to support Libby subdomain patterns
Libby uses dynamic subdomains like dewey-abc123.listen.libbyapp.com for content delivery. Update origin validation to use regex patterns that match both exact domains and subdomains. - Replace exact string matching with regex patterns - Support subdomain patterns: *.listen.libbyapp.com, *.thunder.libbyapp.com - Update both content script and shared validators - Fixes "Rejected message from untrusted origin" errors
1 parent 5f9790a commit ed39da6

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

chrome-extension/content/content.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,18 @@
5151
// VALIDATORS
5252
// ====================================
5353

54-
const VALID_ORIGINS = [
55-
'https://listen.libbyapp.com',
56-
'https://thunder.libbyapp.com',
54+
// Valid origin patterns for Libby domains
55+
// Supports both exact matches and subdomains (e.g., dewey-abc123.listen.libbyapp.com)
56+
const VALID_ORIGIN_PATTERNS = [
57+
/^https:\/\/([a-z0-9-]+\.)?listen\.libbyapp\.com$/,
58+
/^https:\/\/([a-z0-9-]+\.)?thunder\.libbyapp\.com$/,
5759
];
5860

5961
function validateOrigin(origin) {
6062
if (origin === window.location.origin) {
6163
return true;
6264
}
63-
return VALID_ORIGINS.some((validOrigin) => origin === validOrigin);
65+
return VALID_ORIGIN_PATTERNS.some((pattern) => pattern.test(origin));
6466
}
6567

6668
function validateBookData(bookData) {

chrome-extension/shared/validators.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
*/
55

66
/**
7-
* Valid origins for postMessage communication
8-
* Only accept messages from Libby's iframe domain
7+
* Valid origin patterns for postMessage communication
8+
* Supports both exact matches and subdomains (e.g., dewey-abc123.listen.libbyapp.com)
9+
* Only accept messages from Libby's iframe domains
910
*/
10-
const VALID_ORIGINS = [
11-
'https://listen.libbyapp.com',
12-
'https://thunder.libbyapp.com',
11+
const VALID_ORIGIN_PATTERNS = [
12+
/^https:\/\/([a-z0-9-]+\.)?listen\.libbyapp\.com$/,
13+
/^https:\/\/([a-z0-9-]+\.)?thunder\.libbyapp\.com$/,
1314
];
1415

1516
/**
@@ -23,8 +24,8 @@ export function validateOrigin(origin) {
2324
return true;
2425
}
2526

26-
// Check against whitelist
27-
return VALID_ORIGINS.some((validOrigin) => origin === validOrigin);
27+
// Check against pattern whitelist
28+
return VALID_ORIGIN_PATTERNS.some((pattern) => pattern.test(origin));
2829
}
2930

3031
/**
@@ -106,7 +107,7 @@ export function sanitizeFilename(filename) {
106107
export function validateLibbyUrl(url) {
107108
try {
108109
const urlObj = new URL(url);
109-
return VALID_ORIGINS.some((origin) => urlObj.origin === origin);
110+
return VALID_ORIGIN_PATTERNS.some((pattern) => pattern.test(urlObj.origin));
110111
} catch {
111112
return false;
112113
}

0 commit comments

Comments
 (0)