SHA256
Первая версия вложенных файлов работает
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package server.files;
|
||||
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
/** Регистрирует HTTP API content-addressed хранилища DM-файлов. */
|
||||
public final class DmFileApiConfigurator {
|
||||
|
||||
private DmFileApiConfigurator() {}
|
||||
|
||||
public static void register(ServletContextHandler context) {
|
||||
context.addServlet(new ServletHolder(new DmFileServlet()), "/dm-files/*");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,370 @@
|
||||
package server.files;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import server.logic.ws_protocol.Base64Ws;
|
||||
import server.logic.ws_protocol.JSON.utils.AuthKeyUtils;
|
||||
import shine.db.dao.ActiveSessionsDAO;
|
||||
import shine.db.entities.ActiveSessionEntry;
|
||||
import utils.config.AppConfig;
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.AtomicMoveNotSupportedException;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* HTTP-хранилище зашифрованных файлов из личных сообщений.
|
||||
*
|
||||
* Сервер намеренно ничего не знает об исходном имени, MIME и ключе AES-GCM:
|
||||
* он получает только ciphertext и хранит его под content-addressed именем
|
||||
* Base58(SHA-256(ciphertext)). Ключ файла передаётся внутри E2EE DM.
|
||||
*/
|
||||
public final class DmFileServlet extends HttpServlet {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||
private static final Pattern FILE_ID_PATTERN = Pattern.compile("^[1-9A-HJ-NP-Za-km-z]{32,44}$");
|
||||
|
||||
private static final String HEADER_SESSION_ID = "X-Shine-Session-Id";
|
||||
private static final String HEADER_TIME_MS = "X-Shine-Time-Ms";
|
||||
private static final String HEADER_CONTENT_LENGTH = "X-Shine-Content-Length";
|
||||
private static final String HEADER_SIGNATURE = "X-Shine-Signature";
|
||||
|
||||
private static final long DEFAULT_MAX_BYTES = 50L * 1024L * 1024L + 16L;
|
||||
private static final long MAX_AUTH_SKEW_MS = 60_000L;
|
||||
|
||||
private final boolean enabled;
|
||||
private final Path storageDir;
|
||||
private final long maxBytes;
|
||||
|
||||
public DmFileServlet() {
|
||||
AppConfig config = AppConfig.getInstance();
|
||||
this.enabled = readBoolean(config, "dm.files.enabled", true);
|
||||
this.storageDir = Path.of(readString(config, "dm.files.storageDir", "data/dm-files"))
|
||||
.toAbsolutePath()
|
||||
.normalize();
|
||||
this.maxBytes = Math.max(16L, readLong(config, "dm.files.maxBytes", DEFAULT_MAX_BYTES));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) {
|
||||
applyCors(resp);
|
||||
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
applyCors(resp);
|
||||
if (!enabled) {
|
||||
writeError(resp, HttpServletResponse.SC_NOT_FOUND, "FILES_DISABLED", "Передача файлов отключена на сервере");
|
||||
return;
|
||||
}
|
||||
|
||||
String fileId = extractFileId(req);
|
||||
if (fileId == null) {
|
||||
writeError(resp, HttpServletResponse.SC_BAD_REQUEST, "BAD_FILE_ID", "Некорректный идентификатор файла");
|
||||
return;
|
||||
}
|
||||
|
||||
UploadAuth auth;
|
||||
try {
|
||||
auth = readAndVerifyUploadAuth(req, fileId);
|
||||
} catch (AuthFailure failure) {
|
||||
writeError(resp, failure.httpStatus, failure.code, failure.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
if (auth.declaredLength > maxBytes) {
|
||||
writeError(resp, HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "FILE_TOO_LARGE", "Файл превышает лимит сервера");
|
||||
return;
|
||||
}
|
||||
|
||||
Files.createDirectories(storageDir);
|
||||
Path target = safeTarget(fileId);
|
||||
if (target == null) {
|
||||
writeError(resp, HttpServletResponse.SC_BAD_REQUEST, "BAD_FILE_ID", "Некорректный идентификатор файла");
|
||||
return;
|
||||
}
|
||||
|
||||
// Content-addressed объект неизменяем. Повторная корректно подписанная загрузка того же
|
||||
// ciphertext безопасна и просто возвращает существующий объект.
|
||||
if (Files.isRegularFile(target)) {
|
||||
if (Files.size(target) != auth.declaredLength) {
|
||||
writeError(resp, HttpServletResponse.SC_CONFLICT, "FILE_ID_CONFLICT", "Файл с таким hash уже существует с другим размером");
|
||||
return;
|
||||
}
|
||||
writeUploadOk(resp, fileId, auth.declaredLength, true);
|
||||
return;
|
||||
}
|
||||
|
||||
Path temp = Files.createTempFile(storageDir, ".dm-upload-", ".tmp");
|
||||
boolean keepTemp = false;
|
||||
try {
|
||||
MessageDigest digest = sha256Digest();
|
||||
long actualLength = 0L;
|
||||
byte[] buffer = new byte[64 * 1024];
|
||||
try (InputStream in = req.getInputStream(); OutputStream out = Files.newOutputStream(temp)) {
|
||||
int read;
|
||||
while ((read = in.read(buffer)) >= 0) {
|
||||
if (read == 0) continue;
|
||||
actualLength += read;
|
||||
if (actualLength > maxBytes || actualLength > auth.declaredLength) {
|
||||
writeError(resp, HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "FILE_TOO_LARGE", "Получено больше байтов, чем разрешено");
|
||||
return;
|
||||
}
|
||||
digest.update(buffer, 0, read);
|
||||
out.write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
|
||||
if (actualLength != auth.declaredLength) {
|
||||
writeError(resp, HttpServletResponse.SC_BAD_REQUEST, "LENGTH_MISMATCH", "Размер загруженного файла не совпал с подписанным размером");
|
||||
return;
|
||||
}
|
||||
|
||||
String actualId = toBase58(digest.digest());
|
||||
if (!Objects.equals(fileId, actualId)) {
|
||||
writeError(resp, HttpServletResponse.SC_BAD_REQUEST, "HASH_MISMATCH", "SHA-256 загруженного ciphertext не совпал с адресом файла");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean alreadyExists = false;
|
||||
try {
|
||||
try {
|
||||
Files.move(temp, target, StandardCopyOption.ATOMIC_MOVE);
|
||||
} catch (AtomicMoveNotSupportedException ignored) {
|
||||
Files.move(temp, target);
|
||||
}
|
||||
keepTemp = true; // temp уже перемещён, удалять нечего
|
||||
} catch (FileAlreadyExistsException race) {
|
||||
alreadyExists = true;
|
||||
}
|
||||
|
||||
writeUploadOk(resp, fileId, actualLength, alreadyExists);
|
||||
} finally {
|
||||
if (!keepTemp) {
|
||||
Files.deleteIfExists(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
serveFile(req, resp, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
serveFile(req, resp, true);
|
||||
}
|
||||
|
||||
private void serveFile(HttpServletRequest req, HttpServletResponse resp, boolean headOnly) throws IOException {
|
||||
applyCors(resp);
|
||||
if (!enabled) {
|
||||
writeError(resp, HttpServletResponse.SC_NOT_FOUND, "FILES_DISABLED", "Передача файлов отключена на сервере");
|
||||
return;
|
||||
}
|
||||
|
||||
String fileId = extractFileId(req);
|
||||
Path target = fileId == null ? null : safeTarget(fileId);
|
||||
if (target == null || !Files.isRegularFile(target)) {
|
||||
writeError(resp, HttpServletResponse.SC_NOT_FOUND, "FILE_NOT_FOUND", "Файл не найден");
|
||||
return;
|
||||
}
|
||||
|
||||
long size = Files.size(target);
|
||||
resp.setStatus(HttpServletResponse.SC_OK);
|
||||
resp.setContentType("application/octet-stream");
|
||||
resp.setHeader("Content-Length", Long.toString(size));
|
||||
resp.setHeader("ETag", "\"" + fileId + "\"");
|
||||
resp.setHeader("Cache-Control", "no-store");
|
||||
resp.setHeader("X-Content-Type-Options", "nosniff");
|
||||
resp.setHeader("Cross-Origin-Resource-Policy", "cross-origin");
|
||||
if (!headOnly) {
|
||||
try (InputStream in = Files.newInputStream(target); OutputStream out = resp.getOutputStream()) {
|
||||
in.transferTo(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private UploadAuth readAndVerifyUploadAuth(HttpServletRequest req, String fileId) throws AuthFailure {
|
||||
String sessionId = requiredHeader(req, HEADER_SESSION_ID);
|
||||
String timeRaw = requiredHeader(req, HEADER_TIME_MS);
|
||||
String lengthRaw = requiredHeader(req, HEADER_CONTENT_LENGTH);
|
||||
String signatureRaw = requiredHeader(req, HEADER_SIGNATURE);
|
||||
|
||||
long timeMs;
|
||||
long declaredLength;
|
||||
try {
|
||||
timeMs = Long.parseLong(timeRaw);
|
||||
declaredLength = Long.parseLong(lengthRaw);
|
||||
} catch (NumberFormatException badNumber) {
|
||||
throw new AuthFailure(HttpServletResponse.SC_BAD_REQUEST, "BAD_UPLOAD_HEADERS", "Некорректные числовые заголовки загрузки");
|
||||
}
|
||||
if (declaredLength < 16L) {
|
||||
throw new AuthFailure(HttpServletResponse.SC_BAD_REQUEST, "BAD_UPLOAD_LENGTH", "Зашифрованный файл слишком мал");
|
||||
}
|
||||
long nowMs = System.currentTimeMillis();
|
||||
if (timeMs < nowMs - MAX_AUTH_SKEW_MS || timeMs > nowMs + MAX_AUTH_SKEW_MS) {
|
||||
throw new AuthFailure(HttpServletResponse.SC_UNAUTHORIZED, "UPLOAD_AUTH_EXPIRED", "Подпись загрузки устарела");
|
||||
}
|
||||
|
||||
ActiveSessionEntry session;
|
||||
try {
|
||||
session = ActiveSessionsDAO.getInstance().getBySessionId(sessionId);
|
||||
} catch (SQLException dbError) {
|
||||
throw new AuthFailure(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "SESSION_LOOKUP_FAILED", "Не удалось проверить пользовательскую сессию");
|
||||
}
|
||||
if (session == null || session.getSessionKey() == null || session.getSessionKey().isBlank()) {
|
||||
throw new AuthFailure(HttpServletResponse.SC_UNAUTHORIZED, "SESSION_NOT_FOUND", "Активная сессия не найдена");
|
||||
}
|
||||
|
||||
byte[] signature;
|
||||
byte[] publicKey;
|
||||
try {
|
||||
signature = Base64Ws.decodeLen(signatureRaw, Ed25519Util.SIGNATURE_LEN, "signature");
|
||||
publicKey = AuthKeyUtils.parseEd25519PublicKey(session.getSessionKey(), "sessionKey");
|
||||
} catch (RuntimeException badKey) {
|
||||
throw new AuthFailure(HttpServletResponse.SC_UNAUTHORIZED, "BAD_UPLOAD_SIGNATURE", "Некорректная подпись загрузки");
|
||||
}
|
||||
|
||||
String preimage = uploadPreimage(sessionId, fileId, declaredLength, timeMs);
|
||||
if (!Ed25519Util.verify(preimage.getBytes(StandardCharsets.UTF_8), signature, publicKey)) {
|
||||
throw new AuthFailure(HttpServletResponse.SC_UNAUTHORIZED, "BAD_UPLOAD_SIGNATURE", "Подпись загрузки не прошла проверку");
|
||||
}
|
||||
return new UploadAuth(declaredLength);
|
||||
}
|
||||
|
||||
private static String uploadPreimage(String sessionId, String fileId, long encryptedSize, long timeMs) {
|
||||
return "DM_FILE_UPLOAD_V1:" + sessionId + ':' + fileId + ':' + encryptedSize + ':' + timeMs;
|
||||
}
|
||||
|
||||
private String extractFileId(HttpServletRequest req) {
|
||||
String pathInfo = String.valueOf(req.getPathInfo() == null ? "" : req.getPathInfo()).trim();
|
||||
if (!pathInfo.startsWith("/") || pathInfo.indexOf('/', 1) >= 0) return null;
|
||||
String fileId = pathInfo.substring(1);
|
||||
return FILE_ID_PATTERN.matcher(fileId).matches() ? fileId : null;
|
||||
}
|
||||
|
||||
private Path safeTarget(String fileId) {
|
||||
if (fileId == null || !FILE_ID_PATTERN.matcher(fileId).matches()) return null;
|
||||
Path target = storageDir.resolve(fileId).normalize();
|
||||
return target.getParent() != null && target.getParent().equals(storageDir) ? target : null;
|
||||
}
|
||||
|
||||
private static String requiredHeader(HttpServletRequest req, String name) throws AuthFailure {
|
||||
String value = req.getHeader(name);
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new AuthFailure(HttpServletResponse.SC_UNAUTHORIZED, "MISSING_UPLOAD_AUTH", "Не хватает заголовка " + name);
|
||||
}
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
private static MessageDigest sha256Digest() throws IOException {
|
||||
try {
|
||||
return MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException impossible) {
|
||||
throw new IOException("SHA-256 недоступен", impossible);
|
||||
}
|
||||
}
|
||||
|
||||
static String toBase58(byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) return "";
|
||||
BigInteger value = new BigInteger(1, bytes);
|
||||
StringBuilder result = new StringBuilder();
|
||||
BigInteger base = BigInteger.valueOf(58L);
|
||||
while (value.signum() > 0) {
|
||||
BigInteger[] divRem = value.divideAndRemainder(base);
|
||||
result.append(BASE58_ALPHABET.charAt(divRem[1].intValue()));
|
||||
value = divRem[0];
|
||||
}
|
||||
for (byte b : bytes) {
|
||||
if (b != 0) break;
|
||||
result.append('1');
|
||||
}
|
||||
return result.reverse().toString();
|
||||
}
|
||||
|
||||
private static boolean readBoolean(AppConfig config, String key, boolean fallback) {
|
||||
String value = config.getParam(key);
|
||||
if (value == null || value.isBlank()) return fallback;
|
||||
return Boolean.parseBoolean(value.trim());
|
||||
}
|
||||
|
||||
private static long readLong(AppConfig config, String key, long fallback) {
|
||||
String value = config.getParam(key);
|
||||
if (value == null || value.isBlank()) return fallback;
|
||||
try {
|
||||
return Long.parseLong(value.trim());
|
||||
} catch (NumberFormatException ignored) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
private static String readString(AppConfig config, String key, String fallback) {
|
||||
String value = config.getParam(key);
|
||||
return value == null || value.isBlank() ? fallback : value.trim();
|
||||
}
|
||||
|
||||
private static void applyCors(HttpServletResponse resp) {
|
||||
resp.setHeader("Access-Control-Allow-Origin", "*");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "GET, HEAD, PUT, OPTIONS");
|
||||
resp.setHeader("Access-Control-Allow-Headers",
|
||||
"Content-Type, X-Shine-Session-Id, X-Shine-Time-Ms, X-Shine-Content-Length, X-Shine-Signature");
|
||||
resp.setHeader("Access-Control-Expose-Headers", "Content-Length, ETag");
|
||||
resp.setHeader("Vary", "Origin");
|
||||
}
|
||||
|
||||
private static void writeUploadOk(HttpServletResponse resp, String fileId, long size, boolean alreadyExists) throws IOException {
|
||||
ObjectNode payload = MAPPER.createObjectNode();
|
||||
payload.put("ok", true);
|
||||
payload.put("fileId", fileId);
|
||||
payload.put("size", size);
|
||||
payload.put("alreadyExists", alreadyExists);
|
||||
writeJson(resp, HttpServletResponse.SC_OK, payload);
|
||||
}
|
||||
|
||||
private static void writeError(HttpServletResponse resp, int status, String code, String message) throws IOException {
|
||||
ObjectNode payload = MAPPER.createObjectNode();
|
||||
payload.put("ok", false);
|
||||
payload.put("error", code);
|
||||
payload.put("message", message);
|
||||
writeJson(resp, status, payload);
|
||||
}
|
||||
|
||||
private static void writeJson(HttpServletResponse resp, int status, ObjectNode payload) throws IOException {
|
||||
resp.setStatus(status);
|
||||
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
MAPPER.writeValue(resp.getOutputStream(), payload);
|
||||
}
|
||||
|
||||
private record UploadAuth(long declaredLength) {}
|
||||
|
||||
private static final class AuthFailure extends Exception {
|
||||
private final int httpStatus;
|
||||
private final String code;
|
||||
|
||||
private AuthFailure(int httpStatus, String code, String message) {
|
||||
super(message);
|
||||
this.httpStatus = httpStatus;
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerI
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.debug.DebugApiConfigurator;
|
||||
import server.files.DmFileApiConfigurator;
|
||||
import server.sync.BlockchainResyncRecoveryOnStartup;
|
||||
import server.sync.PeriodicBlockchainSyncService;
|
||||
import server.sync.PeriodicDmDeliveryService;
|
||||
@@ -93,6 +94,9 @@ public final class WsServer {
|
||||
// HTTP debug API
|
||||
DebugApiConfigurator.register(context);
|
||||
|
||||
// Зашифрованные вложения личных сообщений: PUT/GET /dm-files/{sha256-base58}
|
||||
DmFileApiConfigurator.register(context);
|
||||
|
||||
// Инициализация контейнера WebSocket
|
||||
JettyWebSocketServletContainerInitializer.configure(context, (servletContext, wsContainer) -> {
|
||||
// Таймаут простоя соединения (Jetty 11 синтаксис)
|
||||
|
||||
@@ -53,6 +53,16 @@ server.ui.indexPath=/home/player/SHiNE/shine-UI/index.html
|
||||
server.ui.buildHash=
|
||||
|
||||
# Web Push (VAPID)
|
||||
|
||||
# ================================
|
||||
# DM encrypted file storage
|
||||
# ================================
|
||||
# На диске лежит только AES-GCM ciphertext. Имя файла = Base58(SHA-256(ciphertext)).
|
||||
dm.files.enabled=true
|
||||
dm.files.storageDir=data/dm-files
|
||||
# 50 MiB исходного файла + 16 bytes AES-GCM authentication tag.
|
||||
dm.files.maxBytes=52428816
|
||||
|
||||
webpush.vapid.public=BOdoWZndZRaNe9kyUFsJ5-xEfFABXNKennAKg15Z7ycAwUIQ7yDV_sIWWYJCwJriN4g9oU-CyJPrn1U6lfxuDbI
|
||||
webpush.vapid.private=3hCt7XxTvLzuoxinjT5QcKRQEBnGZHXn8ZilU31RPNE
|
||||
webpush.vapid.subject=mailto:admin@shine.local
|
||||
|
||||
Reference in New Issue
Block a user