Skip to content

Conversation

@romanornr
Copy link
Contributor

What ❔

Fix let timestamp_diff = timestamp_now - timestamp.parse::<i64>().unwrap();

.unwrap on untrusted input from HTTP header X-timestamp

However, this can also be fixed with

let timestamp_diff = timestamp_now - timestamp.parse::<i64>()
    .map_err(|_| ApiError::Unauthorized("Invalid timestamp".into()))?;

or

// Pre-validate format
if !timestamp.chars().all(|c| c.is_ascii_digit() || c == '-') {
    tracing::warn!("Rejected malformed timestamp");
    return Err(ApiError::Unauthorized("Invalid timestamp format".into()));
}

// Parse with logging
let parsed = timestamp.parse::<i64>().map_err(|e| {
    tracing::warn!("Parse failed: {}", e);
    ApiError::Unauthorized("Invalid timestamp".into())
})?;

// Bounds check
if parsed < 0 || parsed > 253402300799 {
    return Err(ApiError::Unauthorized("Timestamp out of range".into()));
}

Why ❔

Simple crash - malformed timestamp causes panic

curl -X POST "http://localhost:8080/session/new" \
  -H "X-Timestamp: CRASH" \
  -H "X-Verifier-Index: 0" \
  -H "X-Signature: fake" \
  -H "X-Sequencer-Version: 1.0.0"

Empty string

curl -X GET "http://localhost:8080/session/" \
  -H "X-Timestamp: " \
  -H "X-Verifier-Index: 0" \
  -H "X-Signature: fake" \
  -H "X-Sequencer-Version: 1.0.0"

Overflow attempt (too large for i64)

curl -X POST "http://localhost:8080/session/nonce" \
  -H "X-Timestamp: 99999999999999999999999999999" \
  -H "X-Verifier-Index: 0" \
  -H "X-Signature: fake" \
  -H "X-Sequencer-Version: 1.0.0"

Special characters

curl -X GET "http://localhost:8080/session/nonce" \
  -H "X-Timestamp: <script>alert(1)</script>" \
  -H "X-Verifier-Index: 0" \
  -H "X-Signature: fake" \
  -H "X-Sequencer-Version: 1.0.0"

keeps crashing the coordinator

#!/bin/bash
TARGET="http://localhost:8080/session/new"

while true; do
  curl -s -X POST "$TARGET" \
    -H "X-Timestamp: KILL" \
    -H "X-Verifier-Index: 0" \
    -H "X-Signature: x" \
    -H "X-Sequencer-Version: 1.0.0"
  sleep 0.1
done

From the code at api.rs

let bind_address = config.bind_addr();  // Configurable - could be 0.0.0.0
let listener = tokio::net::TcpListener::bind(bind_address).await

The coordinator binds to a network socket

From verifier/mod.rs

let url = format!(
    "{}/session/nonce",
    self.verifier_config.coordinator_http_url  // Other verifiers connect here
);

Production

   Verifier 1 ──┐                                           
   Verifier 2 ──┼──► Coordinator:8080 ◄── Attacker?         
   Verifier 3 ──┘        │                                  
                         │                                  
                    ┌────▼────┐                             
                    │ Bitcoin │                             
                    │ Network │                             
                    └─────────┘                             

Would suck with malicious verifier operators (1/N verifiers goes rogue)
Attacker gains shell
Network insider

Should not be bad if properly isolated and firewall blocks external access

Checklist

  • PR title corresponds to the body of PR (we generate changelog entries from PRs).
  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • Code has been formatted via zkstack dev fmt and zkstack dev lint.

@romanornr romanornr added the bug Something isn't working label Dec 30, 2025
@gemini-code-assist
Copy link

Summary of Changes

Hello @romanornr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical vulnerability where malformed X-Timestamp HTTP headers could lead to application panics and denial of service. It introduces robust validation and parsing logic for the timestamp header, including checks for format, parseability, and reasonable bounds (0 to Year 9999). This significantly enhances the stability and security of the coordinator by preventing crashes from untrusted input and centralizing the validation logic into a testable function.

Highlights

  • Robust Timestamp Validation: Implemented comprehensive validation for the X-Timestamp HTTP header to prevent application panics caused by malformed, unparseable, or out-of-bounds timestamp inputs.
  • Centralized Validation Logic: Extracted the timestamp validation logic into a dedicated, testable function validate_timestamp for improved code organization, reusability, and clarity.
  • Enhanced Security and Stability: The changes prevent potential denial-of-service attacks by malicious actors sending invalid timestamps, significantly improving the coordinator's resilience and preventing crashes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a critical panic caused by malformed timestamp headers, which is an important security fix. The approach of adding robust validation, including format and bounds checks, is excellent. The addition of a comprehensive test suite for the new validation logic is also a great practice. My review focuses on improving code reuse by leveraging the new helper function in the middleware and simplifying the validation logic for better clarity and maintainability.

@romanornr romanornr changed the title via fix auth: prevetn panic on malformed timestamp header input via fix auth: prevent panic on malformed timestamp header input Dec 30, 2025
@romanornr romanornr force-pushed the via-auth-002 branch 3 times, most recently from e6b3b93 to fb61cc3 Compare December 30, 2025 03:54
Copy link
Collaborator

@0xatomFusion 0xatomFusion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix

@0xatomFusion 0xatomFusion changed the title via fix auth: prevent panic on malformed timestamp header input via( via_verifier_coordinator): prevent panic on malformed timestamp header input Dec 30, 2025
@0xatomFusion 0xatomFusion changed the title via( via_verifier_coordinator): prevent panic on malformed timestamp header input fix(via_verifier_coordinator): prevent panic on malformed timestamp header input Dec 30, 2025
@0xatomFusion 0xatomFusion merged commit 3c51553 into vianetwork:main Dec 30, 2025
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants