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

Protect internal apis #38

Merged
merged 3 commits into from
Nov 12, 2023
Merged
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
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