Skip to content

Commit

Permalink
Merge pull request #38 from mempool/mononaut/protect-internal-apis
Browse files Browse the repository at this point in the history
Protect internal apis
  • Loading branch information
wiz authored Nov 12, 2023
2 parents 649f28d + 281b699 commit e28f887
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const TTL_SHORT: u32 = 10; // ttl for volatie resources
const TTL_MEMPOOL_RECENT: u32 = 5; // ttl for GET /mempool/recent
const CONF_FINAL: usize = 10; // reorgs deeper than this are considered unlikely

// internal api prefix
const INTERNAL_PREFIX: &str = "internal";

#[derive(Serialize, Deserialize)]
struct BlockValue {
id: String,
Expand Down Expand Up @@ -726,7 +729,7 @@ fn handle_request(
.ok_or_else(|| HttpError::not_found("Block not found".to_string()))?;
json_response(txids, TTL_LONG)
}
(&Method::GET, Some(&"block"), Some(hash), Some(&"txs"), None, None) => {
(&Method::GET, Some(&INTERNAL_PREFIX), Some(&"block"), Some(hash), Some(&"txs"), None) => {
let hash = BlockHash::from_hex(hash)?;
let txs = query
.chain()
Expand Down Expand Up @@ -1165,7 +1168,14 @@ fn handle_request(
(&Method::GET, Some(&"mempool"), Some(&"txids"), None, None, None) => {
json_response(query.mempool().txids(), TTL_SHORT)
}
(&Method::GET, Some(&"mempool"), Some(&"txs"), Some(&"all"), None, None) => {
(
&Method::GET,
Some(&INTERNAL_PREFIX),
Some(&"mempool"),
Some(&"txs"),
Some(&"all"),
None,
) => {
let txs = query
.mempool()
.txs()
Expand All @@ -1175,7 +1185,7 @@ fn handle_request(

json_response(prepare_txs(txs, query, config), TTL_SHORT)
}
(&Method::POST, Some(&"mempool"), Some(&"txs"), None, None, None) => {
(&Method::POST, Some(&INTERNAL_PREFIX), Some(&"mempool"), Some(&"txs"), None, None) => {
let txid_strings: Vec<String> =
serde_json::from_slice(&body).map_err(|err| HttpError::from(err.to_string()))?;

Expand All @@ -1198,7 +1208,14 @@ fn handle_request(
Err(err) => http_message(StatusCode::BAD_REQUEST, err.to_string(), 0),
}
}
(&Method::GET, Some(&"mempool"), Some(&"txs"), last_seen_txid, None, None) => {
(
&Method::GET,
Some(&INTERNAL_PREFIX),
Some(&"mempool"),
Some(&"txs"),
last_seen_txid,
None,
) => {
let last_seen_txid = last_seen_txid.and_then(|txid| Txid::from_hex(txid).ok());
let txs = query
.mempool()
Expand Down

0 comments on commit e28f887

Please sign in to comment.