Skip to content

Commit

Permalink
fix: prevent domain duplication (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency authored Oct 25, 2023
1 parent 56b431b commit f34bed9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/many-web/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ define_attribute_many_error!(
17: pub fn site_name_too_long(site_name) => "Site name too long: {site_name}.",
18: pub fn invalid_domain(domain) => "Invalid domain: {domain}.",
19: pub fn page_size_too_large(size) => "Page size too large: {size}.",
20: pub fn domain_already_in_use(domain) => "Domain already in use: {domain}.",
}
);

Expand Down
12 changes: 12 additions & 0 deletions src/many-web/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ impl WebCommandsModuleBackend for WebModuleImpl {
return Err(error::existent_site(site_name));
}

if let Some(domain) = &domain {
if self.storage.has_domain(domain) {
return Err(error::domain_already_in_use(domain));
}
}

let tmpdir = Builder::new()
.prefix("dweb-")
.tempdir()
Expand Down Expand Up @@ -368,6 +374,12 @@ impl WebCommandsModuleBackend for WebModuleImpl {
return Err(error::nonexistent_site(site_name));
}

if let Some(domain) = &domain {
if self.storage.has_domain(domain) {
return Err(error::domain_already_in_use(domain));
}
}

let tmpdir = Builder::new()
.prefix("dweb-")
.tempdir()
Expand Down
6 changes: 6 additions & 0 deletions src/many-web/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ impl WebStorage {
.map_err(error::storage_get_failed)
}

// Check all websites for a given domain
pub fn has_domain(&self, domain: &String) -> bool {
self.list(SortOrder::Descending, None)
.any(|(_, meta)| meta.domain.as_ref() == Some(domain))
}

pub fn list(
&self,
order: SortOrder,
Expand Down
23 changes: 23 additions & 0 deletions tests/e2e/web/web.bats
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ function teardown() {
assert_output --partial "Nonexistent site: test_dweb"
}

@test "$SUITE dweb deploy fails if domain already in use" {
call_web --pem=1 --port=8000 deploy test_dweb test_dweb.zip --domain foobar.com
assert_output --partial "https://test_dweb-$(identity 1).ghostcloud.org"
assert_output --partial "foobar.com"

# We check all websites, not just the ones owned by the sender
call_web --pem=2 --port=8000 deploy test_dweb2 test_dweb.zip --domain foobar.com
assert_output --partial "Domain already in use: foobar.com."
}

@test "$SUITE dweb update fails if domain already in use" {
call_web --pem=1 --port=8000 deploy test_dweb test_dweb.zip --domain foobar.com
assert_output --partial "https://test_dweb-$(identity 1).ghostcloud.org"
assert_output --partial "foobar.com"

call_web --pem=1 --port=8000 deploy test_dweb2 test_dweb.zip
assert_output --partial "https://test_dweb2-$(identity 1).ghostcloud.org"
refute_output --partial "foobar.com"

call_web --pem=1 --port=8000 update test_dweb2 test_dweb.zip --domain foobar.com
assert_output --partial "Domain already in use: foobar.com."
}

@test "$SUITE: dweb website removal works" {
call_web --pem=1 --port=8000 deploy test_dweb test_dweb.zip
assert_output --partial "https://test_dweb-$(identity 1).ghostcloud.org"
Expand Down

0 comments on commit f34bed9

Please sign in to comment.