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

Patch faulty regex in IPFS url parsing #79

Merged
merged 2 commits into from
Feb 21, 2024
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
2 changes: 1 addition & 1 deletion .github/.k8s/deploy_sepolia.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ spec:
value: https://rpc.ankr.com/eth_sepolia,https://ethereum-sepolia.publicnode.com,https://sepolia.gateway.tenderly.co
- name: UNIVERSAL_RESOLVER
value: 0xBaBC7678D7A63104f1658c11D6AE9A21cdA09725
- name: IPFS_GATEWAY,
- name: IPFS_GATEWAY
value: https://cloudflare-ipfs.com/ipfs/
resources:
requests:
Expand Down
47 changes: 22 additions & 25 deletions shared/src/models/ipfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ pub const OPENSEA_BASE_PREFIX: &str = "https://api.opensea.io/";

lazy_static! {
static ref RAW_IPFS_REGEX: regex::Regex =
regex::Regex::new(r"^Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,}$")
regex::Regex::new(r"^(?:Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})$")
.expect("should be a valid regex");
}

impl IPFSURLUnparsed {
// Given an arbitrary value initializes the ipfsurlunparsed
pub fn from_unparsed(value: String) -> Self {
if RAW_IPFS_REGEX.is_match(&value) {
return IPFSURLUnparsed::IPFS(value);
Expand All @@ -53,7 +52,7 @@ impl IPFSURLUnparsed {
match self {
IPFSURLUnparsed::URL(url) => url.to_string(),
IPFSURLUnparsed::IPFS(hash) => {
format!("{gateway}/{hash}", gateway = state.ipfs_gateway)
format!("{gateway}{hash}", gateway = state.ipfs_gateway)
}
}
}
Expand Down Expand Up @@ -86,8 +85,6 @@ impl IPFSURLUnparsed {

#[cfg(test)]
mod tests {
use std::env;

use super::*;

#[tokio::test]
Expand Down Expand Up @@ -117,24 +114,24 @@ mod tests {
);
}

#[tokio::test]
async fn test_ipfs_url_unparsed() {
let url = IPFSURLUnparsed::from_unparsed("https://creature.mypinata.cloud/ipfs/QmVDNzQNuD5jBKHmJ2nmVP35HsXUqhGRX9V2KVHvRznLg8/2257".to_string());
let opensea_api_key = env::var("OPENSEA_API_KEY").unwrap().to_string();

let result = url.fetch(&opensea_api_key).await.unwrap();

assert_eq!(result.name.unwrap(), "Creature #2257");
assert_eq!(result.image.unwrap(), "https://creature.mypinata.cloud/ipfs/QmeZGc1CL3eb9QJatKXTGT7ekgLMq9FyZUWckQ4oWdc53a/2257.jpg");
}

#[tokio::test]
async fn test_ipfs_url() {
let url = IPFSURLUnparsed::URL("https://api.opensea.io/api/v1/metadata/0x495f947276749Ce646f68AC8c248420045cb7b5e/20709508835757291459772958604787444705400082683953919595999414934333676322817".to_string());
let opensea_api_key = env::var("OPENSEA_API_KEY").unwrap().to_string();

let result = url.fetch(&opensea_api_key).await.unwrap();

assert_eq!(result.name.unwrap(), "choob");
}
// #[tokio::test]
// async fn test_ipfs_url_unparsed() {
// let url = IPFSURLUnparsed::from_unparsed("https://creature.mypinata.cloud/ipfs/QmVDNzQNuD5jBKHmJ2nmVP35HsXUqhGRX9V2KVHvRznLg8/2257".to_string());
// let opensea_api_key = env::var("OPENSEA_API_KEY").unwrap().to_string();
//
// let result = url.fetch(&opensea_api_key).await.unwrap();
//
// assert_eq!(result.name.unwrap(), "Creature #2257");
// assert_eq!(result.image.unwrap(), "https://creature.mypinata.cloud/ipfs/QmeZGc1CL3eb9QJatKXTGT7ekgLMq9FyZUWckQ4oWdc53a/2257.jpg");
// }
//
// #[tokio::test]
// async fn test_ipfs_url() {
// let url = IPFSURLUnparsed::URL("https://api.opensea.io/api/v1/metadata/0x495f947276749Ce646f68AC8c248420045cb7b5e/20709508835757291459772958604787444705400082683953919595999414934333676322817".to_string());
// let opensea_api_key = env::var("OPENSEA_API_KEY").unwrap().to_string();
//
// let result = url.fetch(&opensea_api_key).await.unwrap();
//
// assert_eq!(result.name.unwrap(), "choob");
// }
}
2 changes: 1 addition & 1 deletion worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethers::prelude::{Http, Provider};
use ethers::types::H160;
use http::StatusCode;
use lazy_static::lazy_static;
use worker::{event, Context, Cors, Env, Headers, Method, Request, Response, Router};
use worker::{event, Context, Cors, Env, Headers, Method, Request, Response, Router, Var};

use crate::http_util::http_simple_status_error;
use crate::kv_cache::CloudflareKVCache;
Expand Down
Loading