-
Notifications
You must be signed in to change notification settings - Fork 607
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
controllers/krate/delete: Send deletion notification email after successful deletion #10265
Open
Turbo87
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
Turbo87:crate-deletion-email
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||
use crate::app::AppState; | ||||||
use crate::auth::AuthCheck; | ||||||
use crate::controllers::krate::CratePath; | ||||||
use crate::email::Email; | ||||||
use crate::models::{NewDeletedCrate, Rights}; | ||||||
use crate::schema::{crate_downloads, crates, dependencies}; | ||||||
use crate::util::errors::{custom, AppResult, BoxedAppError}; | ||||||
|
@@ -79,6 +80,7 @@ pub async fn delete_crate(path: CratePath, parts: Parts, app: AppState) -> AppRe | |||||
} | ||||||
} | ||||||
|
||||||
let crate_name = krate.name.clone(); | ||||||
conn.transaction(|conn| { | ||||||
async move { | ||||||
diesel::delete(crates::table.find(krate.id)) | ||||||
|
@@ -116,6 +118,23 @@ pub async fn delete_crate(path: CratePath, parts: Parts, app: AppState) -> AppRe | |||||
}) | ||||||
.await?; | ||||||
|
||||||
let email_future = async { | ||||||
if let Some(recipient) = user.email(&mut conn).await? { | ||||||
let email = CrateDeletionEmail { | ||||||
user: &user.gh_login, | ||||||
krate: &crate_name, | ||||||
}; | ||||||
|
||||||
app.emails.send(&recipient, email).await? | ||||||
} | ||||||
|
||||||
Ok::<_, anyhow::Error>(()) | ||||||
}; | ||||||
|
||||||
if let Err(err) = email_future.await { | ||||||
error!("Failed to send crate deletion email: {err}"); | ||||||
} | ||||||
|
||||||
Ok(StatusCode::NO_CONTENT) | ||||||
} | ||||||
|
||||||
|
@@ -147,6 +166,33 @@ async fn has_rev_dep(conn: &mut AsyncPgConnection, crate_id: i32) -> QueryResult | |||||
Ok(rev_dep.is_some()) | ||||||
} | ||||||
|
||||||
/// Email template for notifying a crate owner about a crate being deleted. | ||||||
/// | ||||||
/// The owner usually should be aware of the deletion since they initiated it, | ||||||
/// but this email can be helpful in detecting malicious account activity. | ||||||
#[derive(Debug, Clone)] | ||||||
struct CrateDeletionEmail<'a> { | ||||||
user: &'a str, | ||||||
krate: &'a str, | ||||||
} | ||||||
|
||||||
impl Email for CrateDeletionEmail<'_> { | ||||||
fn subject(&self) -> String { | ||||||
format!("crates.io: Deleted \"{}\" crate", self.krate) | ||||||
} | ||||||
|
||||||
fn body(&self) -> String { | ||||||
format!( | ||||||
"Hi {}, | ||||||
|
||||||
this is a confirmation email for the deletion of your \"{}\" crate. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wording tweak (feel free to ignore it if you want, but in that case let's at least capitalise the first letter):
Suggested change
|
||||||
|
||||||
If you did not initiate this deletion, your account may have been compromised. Please contact us at [email protected].", | ||||||
self.user, self.krate | ||||||
) | ||||||
} | ||||||
} | ||||||
|
||||||
#[cfg(test)] | ||||||
mod tests { | ||||||
use super::*; | ||||||
|
@@ -185,6 +231,8 @@ mod tests { | |||||
assert_eq!(response.status(), StatusCode::NO_CONTENT); | ||||||
assert!(response.body().is_empty()); | ||||||
|
||||||
assert_snapshot!(app.emails_snapshot().await); | ||||||
|
||||||
// Assert that the crate no longer exists | ||||||
assert_crate_exists(&anon, "foo", false).await; | ||||||
assert!(!upstream.crate_exists("foo")?); | ||||||
|
@@ -220,6 +268,8 @@ mod tests { | |||||
assert_eq!(response.status(), StatusCode::NO_CONTENT); | ||||||
assert!(response.body().is_empty()); | ||||||
|
||||||
assert_snapshot!(app.emails_snapshot().await); | ||||||
|
||||||
// Assert that the crate no longer exists | ||||||
assert_crate_exists(&anon, "foo", false).await; | ||||||
assert!(!upstream.crate_exists("foo")?); | ||||||
|
@@ -255,6 +305,8 @@ mod tests { | |||||
assert_eq!(response.status(), StatusCode::NO_CONTENT); | ||||||
assert!(response.body().is_empty()); | ||||||
|
||||||
assert_snapshot!(app.emails_snapshot().await); | ||||||
|
||||||
// Assert that the crate no longer exists | ||||||
assert_crate_exists(&anon, "foo", false).await; | ||||||
assert!(!upstream.crate_exists("foo")?); | ||||||
|
33 changes: 33 additions & 0 deletions
33
...krate/snapshots/crates_io__controllers__krate__delete__tests__happy_path_new_crate-2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
source: src/controllers/krate/delete.rs | ||
expression: app.emails_snapshot().await | ||
snapshot_kind: text | ||
--- | ||
To: [email protected] | ||
From: crates.io <[email protected]> | ||
Subject: crates.io: Successfully published [email protected] | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Transfer-Encoding: quoted-printable | ||
|
||
Hello foo! | ||
|
||
A new version of the package foo (1.0.0) was published by your account (htt= | ||
ps://crates.io/users/foo) at [0000-00-00T00:00:00Z]. | ||
|
||
If you have questions or security concerns, you can contact us at help@crat= | ||
es.io. If you would like to stop receiving these security notifications, yo= | ||
u can disable them in your account settings. | ||
---------------------------------------- | ||
|
||
To: [email protected] | ||
From: crates.io <[email protected]> | ||
Subject: crates.io: Deleted "foo" crate | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Transfer-Encoding: quoted-printable | ||
|
||
Hi foo, | ||
|
||
this is a confirmation email for the deletion of your "foo" crate. | ||
|
||
If you did not initiate this deletion, your account may have been compromis= | ||
ed. Please contact us at [email protected]. |
33 changes: 33 additions & 0 deletions
33
...krate/snapshots/crates_io__controllers__krate__delete__tests__happy_path_old_crate-2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
source: src/controllers/krate/delete.rs | ||
expression: app.emails_snapshot().await | ||
snapshot_kind: text | ||
--- | ||
To: [email protected] | ||
From: crates.io <[email protected]> | ||
Subject: crates.io: Successfully published [email protected] | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Transfer-Encoding: quoted-printable | ||
|
||
Hello foo! | ||
|
||
A new version of the package foo (1.0.0) was published by your account (htt= | ||
ps://crates.io/users/foo) at [0000-00-00T00:00:00Z]. | ||
|
||
If you have questions or security concerns, you can contact us at help@crat= | ||
es.io. If you would like to stop receiving these security notifications, yo= | ||
u can disable them in your account settings. | ||
---------------------------------------- | ||
|
||
To: [email protected] | ||
From: crates.io <[email protected]> | ||
Subject: crates.io: Deleted "foo" crate | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Transfer-Encoding: quoted-printable | ||
|
||
Hi foo, | ||
|
||
this is a confirmation email for the deletion of your "foo" crate. | ||
|
||
If you did not initiate this deletion, your account may have been compromis= | ||
ed. Please contact us at [email protected]. |
33 changes: 33 additions & 0 deletions
33
...napshots/crates_io__controllers__krate__delete__tests__happy_path_really_old_crate-2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
source: src/controllers/krate/delete.rs | ||
expression: app.emails_snapshot().await | ||
snapshot_kind: text | ||
--- | ||
To: [email protected] | ||
From: crates.io <[email protected]> | ||
Subject: crates.io: Successfully published [email protected] | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Transfer-Encoding: quoted-printable | ||
|
||
Hello foo! | ||
|
||
A new version of the package foo (1.0.0) was published by your account (htt= | ||
ps://crates.io/users/foo) at [0000-00-00T00:00:00Z]. | ||
|
||
If you have questions or security concerns, you can contact us at help@crat= | ||
es.io. If you would like to stop receiving these security notifications, yo= | ||
u can disable them in your account settings. | ||
---------------------------------------- | ||
|
||
To: [email protected] | ||
From: crates.io <[email protected]> | ||
Subject: crates.io: Deleted "foo" crate | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Transfer-Encoding: quoted-printable | ||
|
||
Hi foo, | ||
|
||
this is a confirmation email for the deletion of your "foo" crate. | ||
|
||
If you did not initiate this deletion, your account may have been compromis= | ||
ed. Please contact us at [email protected]. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curiosity, rather than any actual change required: was there a reason for doing this as an
async
block instead of anasync fn
?