Download checksums.txt and verify the archive integrity. Every non-zero * result is a fail-closed refusal; verification is never optional. */
| 8152 | /* Download checksums.txt and verify the archive integrity. Every non-zero |
| 8153 | * result is a fail-closed refusal; verification is never optional. */ |
| 8154 | static int verify_download_checksum(const char *archive_path, const char *archive_name) { |
| 8155 | char checksum_file[CLI_BUF_256]; |
| 8156 | int checksum_path_length = |
| 8157 | snprintf(checksum_file, sizeof(checksum_file), "%s/cbm-checksums-XXXXXX", cbm_tmpdir()); |
| 8158 | if (checksum_path_length <= 0 || (size_t)checksum_path_length >= sizeof(checksum_file)) { |
| 8159 | return CLI_ERR; |
| 8160 | } |
| 8161 | int checksum_descriptor = cbm_mkstemp(checksum_file); |
| 8162 | if (checksum_descriptor < 0) { |
| 8163 | return CLI_ERR; |
| 8164 | } |
| 8165 | #ifdef _WIN32 |
| 8166 | int checksum_close_status = _close(checksum_descriptor); |
| 8167 | #else |
| 8168 | int checksum_close_status = close(checksum_descriptor); |
| 8169 | #endif |
| 8170 | if (checksum_close_status != 0) { |
| 8171 | cbm_unlink(checksum_file); |
| 8172 | return CLI_ERR; |
| 8173 | } |
| 8174 | |
| 8175 | char dl_base_buf[CLI_BUF_512]; |
| 8176 | const char *dl_base = |
| 8177 | cbm_safe_getenv("CBM_DOWNLOAD_URL", dl_base_buf, sizeof(dl_base_buf), NULL); |
| 8178 | char checksum_url[CLI_BUF_512]; |
| 8179 | int checksum_url_length; |
| 8180 | if (dl_base && dl_base[0]) { |
| 8181 | checksum_url_length = |
| 8182 | snprintf(checksum_url, sizeof(checksum_url), "%s/checksums.txt", dl_base); |
| 8183 | } else { |
| 8184 | checksum_url_length = |
| 8185 | snprintf(checksum_url, sizeof(checksum_url), "%s", |
| 8186 | "https://github.com/DeusData/codebase-memory-mcp/releases/latest/" |
| 8187 | "download/checksums.txt"); |
| 8188 | } |
| 8189 | if (checksum_url_length <= 0 || (size_t)checksum_url_length >= sizeof(checksum_url)) { |
| 8190 | cbm_unlink(checksum_file); |
| 8191 | return CLI_ERR; |
| 8192 | } |
| 8193 | int rc = cbm_download_to_file_quiet(checksum_url, checksum_file); |
| 8194 | if (rc != 0) { |
| 8195 | (void)fprintf(stderr, "error: could not download checksums.txt for mandatory " |
| 8196 | "verification\n"); |
| 8197 | cbm_unlink(checksum_file); |
| 8198 | return CLI_ERR; |
| 8199 | } |
| 8200 | |
| 8201 | char expected[SHA256_BUF_SIZE] = {0}; |
| 8202 | int manifest_status = |
| 8203 | cbm_cli_checksum_manifest_digest(checksum_file, archive_name, expected, sizeof(expected)); |
| 8204 | cbm_unlink(checksum_file); |
| 8205 | if (manifest_status != CLI_OK) { |
| 8206 | (void)fprintf(stderr, |
| 8207 | "error: checksums.txt has no single valid SHA-256 entry " |
| 8208 | "for exact artifact %s\n", |
| 8209 | archive_name); |
| 8210 | return CLI_ERR; |
| 8211 | } |
no test coverage detected