2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-10 06:25:00 +00:00

Validate alias represents real file before setting not found

This commit is contained in:
asonix 2023-07-07 13:33:27 -05:00
parent 9e7376d411
commit 3ccb8ecd8c
2 changed files with 19 additions and 11 deletions

View file

@ -602,14 +602,17 @@ async fn not_found_hash<R: FullRepo>(repo: &R) -> Result<Option<(Alias, R::Bytes
return Ok(None); return Ok(None);
}; };
let alias = String::from_utf8_lossy(not_found.as_ref()) let Some(alias) = Alias::from_slice(not_found.as_ref()) else {
.parse::<Alias>() tracing::warn!("Couldn't parse not-found alias");
.expect("Infallible"); return Ok(None);
};
repo.hash(&alias) let Some(hash) = repo.hash(&alias).await? else {
.await tracing::warn!("No hash found for not-found alias");
.map(|opt| opt.map(|hash| (alias, hash))) return Ok(None);
.map_err(Error::from) };
Ok(Some((alias, hash)))
} }
/// Process files /// Process files
@ -1005,8 +1008,13 @@ async fn set_not_found<R: FullRepo>(
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let alias = json.into_inner().alias; let alias = json.into_inner().alias;
repo.set(NOT_FOUND_KEY, Vec::from(alias.to_string()).into()) if repo.hash(&alias).await?.is_none() {
.await?; return Ok(HttpResponse::BadRequest().json(serde_json::json!({
"msg": "No hash associated with provided alias"
})));
}
repo.set(NOT_FOUND_KEY, alias.to_bytes().into()).await?;
Ok(HttpResponse::Created().json(serde_json::json!({ Ok(HttpResponse::Created().json(serde_json::json!({
"msg": "ok", "msg": "ok",

View file

@ -746,7 +746,7 @@ impl Alias {
self.extension.as_deref() self.extension.as_deref()
} }
fn to_bytes(&self) -> Vec<u8> { pub(crate) fn to_bytes(&self) -> Vec<u8> {
let mut v = self.id.as_bytes().to_vec(); let mut v = self.id.as_bytes().to_vec();
if let Some(ext) = self.extension() { if let Some(ext) = self.extension() {
@ -756,7 +756,7 @@ impl Alias {
v v
} }
fn from_slice(bytes: &[u8]) -> Option<Self> { pub(crate) fn from_slice(bytes: &[u8]) -> Option<Self> {
if let Ok(s) = std::str::from_utf8(bytes) { if let Ok(s) = std::str::from_utf8(bytes) {
Some(Self::from_existing(s)) Some(Self::from_existing(s))
} else if bytes.len() >= 16 { } else if bytes.len() >= 16 {