|
| 1 | +// F:文件下发原语(hotpatch P1 传输层)——wire 编解码 + 校验 + 暂存落盘。 |
| 2 | +// |
| 3 | +// 安全链全部强制:总开关 → 大小上限 → 仅 basename(拒穿越)→ |
| 4 | +// sha256 → 原子落盘暂存目录。**不自动应用**——应用由后续 |
| 5 | +// hotpatch runner 单独编排。 |
| 6 | +// |
| 7 | +// wire(protobuf 兼容,与 Go/Java/JS/Python 手写编解码同构): |
| 8 | +// FilePushRequest { 1: transferId, 2: fileName, 3: contentSha256(hex), 4: data } |
| 9 | +// FilePushResponse { 1: transferId, 2: ok, 3: storedPath, 4: error } |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include "croupier/sdk/croupier_client.h" |
| 14 | + |
| 15 | +#include <nlohmann/json.hpp> |
| 16 | +#include <openssl/sha.h> |
| 17 | + |
| 18 | +#include <cstdio> |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +namespace croupier::sdk { |
| 23 | + |
| 24 | +struct FilePushRequest { |
| 25 | + std::string transfer_id; |
| 26 | + std::string file_name; |
| 27 | + std::string content_sha256; |
| 28 | + std::vector<uint8_t> data; |
| 29 | +}; |
| 30 | + |
| 31 | +struct FilePushResponse { |
| 32 | + std::string transfer_id; |
| 33 | + bool ok = false; |
| 34 | + std::string stored_path; |
| 35 | + std::string error; |
| 36 | +}; |
| 37 | + |
| 38 | +inline void AppendVarint(std::vector<uint8_t>& out, uint64_t value) { |
| 39 | + while (value >= 0x80) { |
| 40 | + out.push_back(static_cast<uint8_t>(value) | 0x80); |
| 41 | + value >>= 7; |
| 42 | + } |
| 43 | + out.push_back(static_cast<uint8_t>(value)); |
| 44 | +} |
| 45 | + |
| 46 | +inline std::string Sha256Hex(const std::vector<uint8_t>& data) { |
| 47 | + unsigned char digest[SHA256_DIGEST_LENGTH]; |
| 48 | + SHA256(data.data(), data.size(), digest); |
| 49 | + static const char* hex = "0123456789abcdef"; |
| 50 | + std::string out; |
| 51 | + out.reserve(SHA256_DIGEST_LENGTH * 2); |
| 52 | + for (unsigned char byte : digest) { |
| 53 | + out.push_back(hex[byte >> 4]); |
| 54 | + out.push_back(hex[byte & 0x0F]); |
| 55 | + } |
| 56 | + return out; |
| 57 | +} |
| 58 | + |
| 59 | +// 手写 protobuf wire 解码(length-delimited 四字段,未知字段跳过)。 |
| 60 | +inline FilePushRequest DecodeFilePushRequest(const std::vector<uint8_t>& body) { |
| 61 | + FilePushRequest req; |
| 62 | + size_t idx = 0; |
| 63 | + auto readVarint = [&](uint64_t& value) -> bool { |
| 64 | + value = 0; |
| 65 | + int shift = 0; |
| 66 | + while (idx < body.size()) { |
| 67 | + uint8_t byte = body[idx++]; |
| 68 | + value |= static_cast<uint64_t>(byte & 0x7F) << shift; |
| 69 | + if (!(byte & 0x80)) return true; |
| 70 | + shift += 7; |
| 71 | + if (shift > 63) return false; |
| 72 | + } |
| 73 | + return false; |
| 74 | + }; |
| 75 | + auto readBytes = [&](std::vector<uint8_t>& value) -> bool { |
| 76 | + uint64_t length = 0; |
| 77 | + if (!readVarint(length)) return false; |
| 78 | + if (idx + length > body.size()) return false; |
| 79 | + value.assign(body.begin() + static_cast<long>(idx), |
| 80 | + body.begin() + static_cast<long>(idx + length)); |
| 81 | + idx += length; |
| 82 | + return true; |
| 83 | + }; |
| 84 | + while (idx < body.size()) { |
| 85 | + uint64_t tag = 0; |
| 86 | + if (!readVarint(tag)) return req; |
| 87 | + const uint64_t field = tag >> 3; |
| 88 | + std::vector<uint8_t> value; |
| 89 | + if (!readBytes(value)) return req; |
| 90 | + switch (field) { |
| 91 | + case 1: |
| 92 | + req.transfer_id.assign(value.begin(), value.end()); |
| 93 | + break; |
| 94 | + case 2: |
| 95 | + req.file_name.assign(value.begin(), value.end()); |
| 96 | + break; |
| 97 | + case 3: |
| 98 | + req.content_sha256.assign(value.begin(), value.end()); |
| 99 | + break; |
| 100 | + case 4: |
| 101 | + req.data = value; |
| 102 | + break; |
| 103 | + default: |
| 104 | + break; // 未知字段跳过 |
| 105 | + } |
| 106 | + } |
| 107 | + return req; |
| 108 | +} |
| 109 | + |
| 110 | +inline std::vector<uint8_t> EncodeFilePushResponse(const FilePushResponse& resp) { |
| 111 | + auto fieldString = [](uint64_t field, const std::string& value) { |
| 112 | + std::vector<uint8_t> out; |
| 113 | + if (value.empty()) return out; |
| 114 | + AppendVarint(out, (field << 3) | 2); |
| 115 | + AppendVarint(out, value.size()); |
| 116 | + out.insert(out.end(), value.begin(), value.end()); |
| 117 | + return out; |
| 118 | + }; |
| 119 | + std::vector<uint8_t> out; |
| 120 | + auto transfer = fieldString(1, resp.transfer_id); |
| 121 | + out.insert(out.end(), transfer.begin(), transfer.end()); |
| 122 | + if (resp.ok) { |
| 123 | + out.push_back(0x10); // field 2 varint |
| 124 | + out.push_back(0x01); |
| 125 | + } |
| 126 | + auto stored = fieldString(3, resp.stored_path); |
| 127 | + out.insert(out.end(), stored.begin(), stored.end()); |
| 128 | + auto error = fieldString(4, resp.error); |
| 129 | + out.insert(out.end(), error.begin(), error.end()); |
| 130 | + return out; |
| 131 | +} |
| 132 | + |
| 133 | +// 校验文件名仅含 basename 且落点仍在暂存目录内。 |
| 134 | +inline bool SafeStagingPath(const std::string& stagingDir, const std::string& fileName, |
| 135 | + std::string& outPath) { |
| 136 | + if (fileName.empty() || fileName == "." || fileName == "..") return false; |
| 137 | + if (fileName.find('/') != std::string::npos || |
| 138 | + fileName.find('\\') != std::string::npos || |
| 139 | + fileName.find("..") != std::string::npos) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + outPath = stagingDir + "/" + fileName; |
| 143 | + return true; |
| 144 | +} |
| 145 | + |
| 146 | +// 原子写:同目录临时文件 + rename。 |
| 147 | +inline bool AtomicWriteFile(const std::string& target, const std::vector<uint8_t>& data) { |
| 148 | + const std::string tmp = target + ".push-tmp"; |
| 149 | + std::FILE* file = std::fopen(tmp.c_str(), "wb"); |
| 150 | + if (!file) return false; |
| 151 | + if (!data.empty()) { |
| 152 | + if (std::fwrite(data.data(), 1, data.size(), file) != data.size()) { |
| 153 | + std::fclose(file); |
| 154 | + std::remove(tmp.c_str()); |
| 155 | + return false; |
| 156 | + } |
| 157 | + } |
| 158 | + if (std::fclose(file) != 0) { |
| 159 | + std::remove(tmp.c_str()); |
| 160 | + return false; |
| 161 | + } |
| 162 | + return std::rename(tmp.c_str(), target.c_str()) == 0; |
| 163 | +} |
| 164 | + |
| 165 | +} // namespace croupier::sdk |
0 commit comments