Skip to content

Commit 435c1c0

Browse files
committed
containers: Make sure that we only persist when we call methods like allow/deny Host
1 parent 4d38f10 commit 435c1c0

1 file changed

Lines changed: 37 additions & 17 deletions

File tree

src/lib/container.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
505505
ctor.outboundByHost !== undefined ||
506506
ctor.outbound !== undefined ||
507507
ctor.outboundHandlers !== undefined ||
508-
this.allowedHosts.length > 0 ||
509-
this.deniedHosts.length > 0
508+
this.effectiveAllowedHosts.length > 0 ||
509+
this.effectiveDeniedHosts.length > 0
510510
) {
511511
this.usingInterception = true;
512512
this.applyOutboundInterceptionPromise = this.applyOutboundInterception();
@@ -638,7 +638,7 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
638638
* @param hosts - Array of hostnames to allow (e.g. `['api.stripe.com', 'example.com']`)
639639
*/
640640
async setAllowedHosts(hosts: string[]): Promise<void> {
641-
this.allowedHosts = [...hosts];
641+
this.allowedHostsOverride = [...hosts];
642642
this.usingInterception = true;
643643
await this.refreshOutboundInterception();
644644
}
@@ -651,7 +651,7 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
651651
* @param hosts - Array of hostnames to deny (e.g. `['evil.com', 'blocked.org']`)
652652
*/
653653
async setDeniedHosts(hosts: string[]): Promise<void> {
654-
this.deniedHosts = [...hosts];
654+
this.deniedHostsOverride = [...hosts];
655655
this.usingInterception = true;
656656
await this.refreshOutboundInterception();
657657
}
@@ -662,8 +662,9 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
662662
* @param hostname - The hostname to allow (e.g. `'api.stripe.com'`)
663663
*/
664664
async allowHost(hostname: string): Promise<void> {
665-
if (!this.allowedHosts.includes(hostname)) {
666-
this.allowedHosts = [...this.allowedHosts, hostname];
665+
const effective = this.effectiveAllowedHosts;
666+
if (!effective.includes(hostname)) {
667+
this.allowedHostsOverride = [...effective, hostname];
667668
}
668669
this.usingInterception = true;
669670
await this.refreshOutboundInterception();
@@ -675,8 +676,9 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
675676
* @param hostname - The hostname to deny (e.g. `'evil.com'`)
676677
*/
677678
async denyHost(hostname: string): Promise<void> {
678-
if (!this.deniedHosts.includes(hostname)) {
679-
this.deniedHosts = [...this.deniedHosts, hostname];
679+
const effective = this.effectiveDeniedHosts;
680+
if (!effective.includes(hostname)) {
681+
this.deniedHostsOverride = [...effective, hostname];
680682
}
681683
this.usingInterception = true;
682684
await this.refreshOutboundInterception();
@@ -688,7 +690,7 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
688690
* @param hostname - The hostname to remove from the allow list
689691
*/
690692
async removeAllowedHost(hostname: string): Promise<void> {
691-
this.allowedHosts = this.allowedHosts.filter(h => h !== hostname);
693+
this.allowedHostsOverride = this.effectiveAllowedHosts.filter(h => h !== hostname);
692694
await this.refreshOutboundInterception();
693695
}
694696

@@ -698,7 +700,7 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
698700
* @param hostname - The hostname to remove from the deny list
699701
*/
700702
async removeDeniedHost(hostname: string): Promise<void> {
701-
this.deniedHosts = this.deniedHosts.filter(h => h !== hostname);
703+
this.deniedHostsOverride = this.effectiveDeniedHosts.filter(h => h !== hostname);
702704
await this.refreshOutboundInterception();
703705
}
704706

@@ -1301,6 +1303,10 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
13011303
private outboundByHostOverrides: OutboundByHostOverrides = {};
13021304
private outboundHandlerOverride?: OutboundHandlerOverride;
13031305

1306+
// Only set when the user calls setAllowedHosts/setDeniedHosts at runtime
1307+
private allowedHostsOverride?: string[];
1308+
private deniedHostsOverride?: string[];
1309+
13041310
// ==========================
13051311
// GENERAL HELPERS
13061312
// ==========================
@@ -1318,21 +1324,35 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
13181324
}
13191325
}
13201326

1327+
private get effectiveAllowedHosts(): string[] {
1328+
return this.allowedHostsOverride ?? this.allowedHosts;
1329+
}
1330+
1331+
private get effectiveDeniedHosts(): string[] {
1332+
return this.deniedHostsOverride ?? this.deniedHosts;
1333+
}
1334+
13211335
private getOutboundConfiguration(): PersistedOutboundConfiguration {
1336+
const allowedHosts = this.effectiveAllowedHosts;
1337+
const deniedHosts = this.effectiveDeniedHosts;
13221338
return {
13231339
enableInternet: this.enableInternet,
13241340
outboundByHostOverrides:
13251341
Object.keys(this.outboundByHostOverrides).length > 0
13261342
? this.outboundByHostOverrides
13271343
: undefined,
13281344
outboundHandlerOverride: this.outboundHandlerOverride,
1329-
allowedHosts: this.allowedHosts.length > 0 ? this.allowedHosts : undefined,
1330-
deniedHosts: this.deniedHosts.length > 0 ? this.deniedHosts : undefined,
1345+
allowedHosts: allowedHosts.length > 0 ? allowedHosts : undefined,
1346+
deniedHosts: deniedHosts.length > 0 ? deniedHosts : undefined,
13311347
};
13321348
}
13331349

13341350
private persistOutboundConfiguration(configuration: PersistedOutboundConfiguration): void {
1335-
this.ctx.storage.kv.put(OUTBOUND_CONFIGURATION_KEY, configuration);
1351+
this.ctx.storage.kv.put(OUTBOUND_CONFIGURATION_KEY, {
1352+
...configuration,
1353+
allowedHosts: this.allowedHostsOverride,
1354+
deniedHosts: this.deniedHostsOverride,
1355+
});
13361356
}
13371357

13381358
private restoreOutboundConfiguration(): PersistedOutboundConfiguration | undefined {
@@ -1371,11 +1391,11 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
13711391
}
13721392

13731393
if (configuration.allowedHosts) {
1374-
this.allowedHosts = configuration.allowedHosts;
1394+
this.allowedHostsOverride = configuration.allowedHosts;
13751395
}
13761396

13771397
if (configuration.deniedHosts) {
1378-
this.deniedHosts = configuration.deniedHosts;
1398+
this.deniedHostsOverride = configuration.deniedHosts;
13791399
}
13801400

13811401
return this.getOutboundConfiguration();
@@ -1414,11 +1434,11 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
14141434
hosts.add(hostname);
14151435
}
14161436

1417-
for (const hostname of this.allowedHosts) {
1437+
for (const hostname of this.effectiveAllowedHosts) {
14181438
hosts.add(hostname);
14191439
}
14201440

1421-
for (const hostname of this.deniedHosts) {
1441+
for (const hostname of this.effectiveDeniedHosts) {
14221442
hosts.add(hostname);
14231443
}
14241444

0 commit comments

Comments
 (0)