Coverage Report

Created: 2026-10-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/ipfs_url.cc
Line
Count
Source
1
/*
2
 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3
 *
4
 * SPDX-License-Identifier: curl
5
 */
6
7
/// @file
8
/// @brief Implementation of IPFS/IPNS gateway URL construction.
9
10
#include "proto_fuzzer/ipfs_url.h"
11
12
#include <string_view>
13
14
namespace proto_fuzzer {
15
16
namespace {
17
18
// libcurl has no ipfs:// handler: curl's command-line tool rewrites those
19
// schemes onto an HTTP gateway before configuring its easy handle. The runner
20
// mirrors that rewrite against a fixed loopback authority; CONNECT_TO and
21
// OPENSOCKET still confine the resulting HTTP GET to the in-process
22
// MockServer, so no gateway process, DNS lookup, or external port is involved.
23
constexpr char kIpfsGatewayBaseUrl[] = "http://127.0.1.127:8080";
24
25
/// Collapse "." and ".." path segments per RFC 3986 section 5.2.4 without
26
/// decoding percent-encoded bytes (an encoded "%2e" must not become a real
27
/// separator). The result is rooted at the input's own root, so ".." can never
28
/// climb above it — matching how curl's tool_ipfs.c normalizes the user path
29
/// before prepending `/ipfs/<cid>`.
30
1.00k
std::string RemoveDotSegments(std::string_view path) {
31
1.00k
  std::string input(path);
32
1.00k
  std::string output;
33
73.7k
  while (!input.empty()) {
34
72.7k
    if (input.rfind("../", 0) == 0) {
35
0
      input.erase(0, 3);
36
72.7k
    } else if (input.rfind("./", 0) == 0) {
37
0
      input.erase(0, 2);
38
72.7k
    } else if (input.rfind("/./", 0) == 0) {
39
189
      input.replace(0, 3, "/");
40
72.5k
    } else if (input == "/.") {
41
10
      input = "/";
42
72.5k
    } else if (input.rfind("/../", 0) == 0) {
43
1.39k
      input.replace(0, 4, "/");
44
1.39k
      const std::size_t slash = output.rfind('/');
45
1.39k
      output.erase(slash == std::string::npos ? 0 : slash);
46
71.1k
    } else if (input == "/..") {
47
33
      input = "/";
48
33
      const std::size_t slash = output.rfind('/');
49
33
      output.erase(slash == std::string::npos ? 0 : slash);
50
71.1k
    } else if (input == "." || input == "..") {
51
0
      input.clear();
52
71.1k
    } else {
53
71.1k
      const std::size_t segment_start = (input[0] == '/') ? 1 : 0;
54
71.1k
      const std::size_t segment_end = input.find('/', segment_start);
55
71.1k
      if (segment_end == std::string::npos) {
56
894
        output += input;
57
894
        input.clear();
58
70.2k
      } else {
59
70.2k
        output += input.substr(0, segment_end);
60
70.2k
        input.erase(0, segment_end);
61
70.2k
      }
62
71.1k
    }
63
72.7k
  }
64
1.00k
  return output;
65
1.00k
}
66
67
/// Split a pinned CID/name from its mutable suffix, collapse dot segments in
68
/// the suffix's path portion (leaving any query and fragment intact), and
69
/// rejoin. This reproduces tool_ipfs.c, which normalizes the user path before
70
/// prepending `/ipfs/<cid>` so a suffix ending in "/../.." cannot erase the
71
/// gateway namespace.
72
12.0k
std::string NormalizeIpfsHostPath(std::string_view host_path) {
73
12.0k
  const std::size_t suffix_start = host_path.find_first_of("/?#");
74
12.0k
  if (suffix_start == std::string_view::npos) {
75
11.0k
    return std::string(host_path);
76
11.0k
  }
77
1.00k
  const std::string_view identifier = host_path.substr(0, suffix_start);
78
1.00k
  const std::string_view suffix = host_path.substr(suffix_start);
79
1.00k
  const std::size_t path_end = suffix.find_first_of("?#");
80
1.00k
  const std::string_view path = (path_end == std::string_view::npos) ? suffix : suffix.substr(0, path_end);
81
1.00k
  const std::string_view query_fragment =
82
1.00k
      (path_end == std::string_view::npos) ? std::string_view() : suffix.substr(path_end);
83
1.00k
  std::string result(identifier);
84
1.00k
  result += RemoveDotSegments(path);
85
1.00k
  result += query_fragment;
86
1.00k
  return result;
87
12.0k
}
88
89
}  // namespace
90
91
/// Build the IPFS/IPNS gateway URL for the given scheme and pinned host_path.
92
/// The mutable suffix's dot segments are normalized before the `/ipfs/<cid>` or
93
/// `/ipns/<name>` namespace is prepended, matching curl's tool_ipfs.c ordering
94
/// so a suffix ending in "/../.." cannot erase the namespace.
95
/// @param scheme SCHEME_IPFS or SCHEME_IPNS.
96
/// @param host_path Canonical identifier (CID/name) followed by the mutable
97
///                  path, query, and fragment suffix.
98
/// @return The HTTP gateway URL curl will request.
99
12.0k
std::string BuildIpfsGatewayUrl(curl::fuzzer::proto::Scheme scheme, const std::string& host_path) {
100
12.0k
  const char* prefix = scheme == curl::fuzzer::proto::SCHEME_IPNS ? "ipns" : "ipfs";
101
12.0k
  return std::string(kIpfsGatewayBaseUrl) + "/" + prefix + "/" + NormalizeIpfsHostPath(host_path);
102
12.0k
}
103
104
}  // namespace proto_fuzzer