Skip to content
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

Lowercase infohashes #226

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE torrust_torrents SET info_hash = LOWER(info_hash);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE torrust_torrents SET info_hash = LOWER(info_hash);
6 changes: 3 additions & 3 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ impl Database for Mysql {
let torrent_id = query("INSERT INTO torrust_torrents (uploader_id, category_id, info_hash, size, name, pieces, piece_length, private, root_hash, date_uploaded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())")
.bind(uploader_id)
.bind(category_id)
.bind(info_hash.to_uppercase())
.bind(info_hash.to_lowercase())
.bind(torrent.file_size())
.bind(torrent.info.name.to_string())
.bind(pieces)
Expand Down Expand Up @@ -582,7 +582,7 @@ impl Database for Mysql {
query_as::<_, DbTorrentInfo>(
"SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE info_hash = ?",
)
.bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.bind(info_hash.to_hex_string().to_lowercase())
.fetch_one(&self.pool)
.await
.map_err(|_| database::Error::TorrentNotFound)
Expand Down Expand Up @@ -652,7 +652,7 @@ impl Database for Mysql {
WHERE tt.info_hash = ?
GROUP BY torrent_id"
)
.bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.bind(info_hash.to_hex_string().to_lowercase())
.fetch_one(&self.pool)
.await
.map_err(|_| database::Error::TorrentNotFound)
Expand Down
6 changes: 3 additions & 3 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl Database for Sqlite {
let torrent_id = query("INSERT INTO torrust_torrents (uploader_id, category_id, info_hash, size, name, pieces, piece_length, private, root_hash, date_uploaded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, strftime('%Y-%m-%d %H:%M:%S',DATETIME('now', 'utc')))")
.bind(uploader_id)
.bind(category_id)
.bind(info_hash.to_uppercase())
.bind(info_hash.to_lowercase())
.bind(torrent.file_size())
.bind(torrent.info.name.to_string())
.bind(pieces)
Expand Down Expand Up @@ -572,7 +572,7 @@ impl Database for Sqlite {
query_as::<_, DbTorrentInfo>(
"SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE info_hash = ?",
)
.bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.bind(info_hash.to_hex_string().to_lowercase())
.fetch_one(&self.pool)
.await
.map_err(|_| database::Error::TorrentNotFound)
Expand Down Expand Up @@ -642,7 +642,7 @@ impl Database for Sqlite {
WHERE tt.info_hash = ?
GROUP BY ts.torrent_id"
)
.bind(info_hash.to_string().to_uppercase()) // `info_hash` is stored as uppercase
.bind(info_hash.to_string().to_lowercase())
.fetch_one(&self.pool)
.await
.map_err(|_| database::Error::TorrentNotFound)
Expand Down
2 changes: 1 addition & 1 deletion src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl Torrent {

#[must_use]
pub fn info_hash(&self) -> String {
from_bytes(&self.calculate_info_hash_as_bytes())
from_bytes(&self.calculate_info_hash_as_bytes()).to_lowercase()
}

#[must_use]
Expand Down
14 changes: 10 additions & 4 deletions src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ pub async fn upload_torrent_handler(
#[derive(Deserialize)]
pub struct InfoHashParam(pub String);

impl InfoHashParam {
fn lowercase(&self) -> String {
self.0.to_lowercase()
}
}

/// Returns the torrent as a byte stream `application/x-bittorrent`.
///
/// # Errors
Expand All @@ -68,7 +74,7 @@ pub async fn download_torrent_handler(
Extract(maybe_bearer_token): Extract,
Path(info_hash): Path<InfoHashParam>,
) -> Response {
let Ok(info_hash) = InfoHash::from_str(&info_hash.0) else { return ServiceError::BadRequest.into_response() };
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else { return ServiceError::BadRequest.into_response() };

let opt_user_id = match get_optional_logged_in_user(maybe_bearer_token, app_data.clone()).await {
Ok(opt_user_id) => opt_user_id,
Expand Down Expand Up @@ -114,7 +120,7 @@ pub async fn get_torrent_info_handler(
Extract(maybe_bearer_token): Extract,
Path(info_hash): Path<InfoHashParam>,
) -> Response {
let Ok(info_hash) = InfoHash::from_str(&info_hash.0) else { return ServiceError::BadRequest.into_response() };
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else { return ServiceError::BadRequest.into_response() };

let opt_user_id = match get_optional_logged_in_user(maybe_bearer_token, app_data.clone()).await {
Ok(opt_user_id) => opt_user_id,
Expand Down Expand Up @@ -143,7 +149,7 @@ pub async fn update_torrent_info_handler(
Path(info_hash): Path<InfoHashParam>,
extract::Json(update_torrent_info_form): extract::Json<UpdateTorrentInfoForm>,
) -> Response {
let Ok(info_hash) = InfoHash::from_str(&info_hash.0) else { return ServiceError::BadRequest.into_response() };
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else { return ServiceError::BadRequest.into_response() };

let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
Ok(user_id) => user_id,
Expand Down Expand Up @@ -182,7 +188,7 @@ pub async fn delete_torrent_handler(
Extract(maybe_bearer_token): Extract,
Path(info_hash): Path<InfoHashParam>,
) -> Response {
let Ok(info_hash) = InfoHash::from_str(&info_hash.0) else { return ServiceError::BadRequest.into_response() };
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else { return ServiceError::BadRequest.into_response() };

let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
Ok(user_id) => user_id,
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/web/api/v1/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod for_guests {
let expected_torrent = TorrentDetails {
torrent_id: uploaded_torrent.torrent_id,
uploader: uploader.username,
info_hash: test_torrent.file_info.info_hash.to_uppercase(),
info_hash: test_torrent.file_info.info_hash.to_lowercase(),
title: test_torrent.index_info.title.clone(),
description: test_torrent.index_info.description,
category: Category {
Expand All @@ -203,7 +203,7 @@ mod for_guests {
magnet_link: format!(
// cspell:disable-next-line
"magnet:?xt=urn:btih:{}&dn={}&tr={}&tr={}",
test_torrent.file_info.info_hash.to_uppercase(),
test_torrent.file_info.info_hash.to_lowercase(),
urlencoding::encode(&test_torrent.index_info.title),
encoded_tracker_url,
encoded_tracker_url
Expand Down