Download checksums.txt and verify the archive integrity. Every non-zero * result is a fail-closed refusal; verification is never optional. */
| 6917 | /* Download checksums.txt and verify the archive integrity. Every non-zero |
| 6918 | * result is a fail-closed refusal; verification is never optional. */ |
| 6919 | static int verify_download_checksum(const char *archive_path, const char *archive_name) { |
| 6920 | char checksum_file[CLI_BUF_256]; |
| 6921 | int checksum_path_length = |
| 6922 | snprintf(checksum_file, sizeof(checksum_file), "%s/cbm-checksums-XXXXXX", cbm_tmpdir()); |
| 6923 | if (checksum_path_length <= 0 || (size_t)checksum_path_length >= sizeof(checksum_file)) { |
| 6924 | return CLI_ERR; |
| 6925 | } |
| 6926 | int checksum_descriptor = cbm_mkstemp(checksum_file); |
| 6927 | if (checksum_descriptor < 0) { |
| 6928 | return CLI_ERR; |
| 6929 | } |
| 6930 | #ifdef _WIN32 |
| 6931 | int checksum_close_status = _close(checksum_descriptor); |
| 6932 | #else |
| 6933 | int checksum_close_status = close(checksum_descriptor); |
| 6934 | #endif |
| 6935 | if (checksum_close_status != 0) { |
| 6936 | cbm_unlink(checksum_file); |
| 6937 | return CLI_ERR; |
| 6938 | } |
| 6939 | |
| 6940 | char dl_base_buf[CLI_BUF_512]; |
| 6941 | const char *dl_base = |
| 6942 | cbm_safe_getenv("CBM_DOWNLOAD_URL", dl_base_buf, sizeof(dl_base_buf), NULL); |
| 6943 | char checksum_url[CLI_BUF_512]; |
| 6944 | int checksum_url_length; |
| 6945 | if (dl_base && dl_base[0]) { |
| 6946 | checksum_url_length = |
| 6947 | snprintf(checksum_url, sizeof(checksum_url), "%s/checksums.txt", dl_base); |
| 6948 | } else { |
| 6949 | checksum_url_length = |
| 6950 | snprintf(checksum_url, sizeof(checksum_url), "%s", |
| 6951 | "https://github.com/DeusData/codebase-memory-mcp/releases/latest/" |
| 6952 | "download/checksums.txt"); |
| 6953 | } |
| 6954 | if (checksum_url_length <= 0 || (size_t)checksum_url_length >= sizeof(checksum_url)) { |
| 6955 | cbm_unlink(checksum_file); |
| 6956 | return CLI_ERR; |
| 6957 | } |
| 6958 | int rc = cbm_download_to_file_quiet(checksum_url, checksum_file); |
| 6959 | if (rc != 0) { |
| 6960 | (void)fprintf(stderr, "error: could not download checksums.txt for mandatory " |
| 6961 | "verification\n"); |
| 6962 | cbm_unlink(checksum_file); |
| 6963 | return CLI_ERR; |
| 6964 | } |
| 6965 | |
| 6966 | char expected[SHA256_BUF_SIZE] = {0}; |
| 6967 | int manifest_status = |
| 6968 | cbm_cli_checksum_manifest_digest(checksum_file, archive_name, expected, sizeof(expected)); |
| 6969 | cbm_unlink(checksum_file); |
| 6970 | if (manifest_status != CLI_OK) { |
| 6971 | (void)fprintf(stderr, |
| 6972 | "error: checksums.txt has no single valid SHA-256 entry " |
| 6973 | "for exact artifact %s\n", |
| 6974 | archive_name); |
| 6975 | return CLI_ERR; |
| 6976 | } |
no test coverage detected