Compare commits

..
Author SHA1 Message Date
Abdullah Atta 5b1de7cc62 monograph: fix typo 2026-02-25 15:42:58 +05:00
Abdullah Atta 3f2ba697bc monograph: fix monograph content sanitization 2026-02-25 15:39:25 +05:00
35 changed files with 274 additions and 1017 deletions
-4
View File
@@ -36,10 +36,6 @@ jobs:
- image: streetwriters/sse - image: streetwriters/sse
file: ./Streetwriters.Messenger/Dockerfile file: ./Streetwriters.Messenger/Dockerfile
context: . context: .
- image: streetwriters/notesnook-inbox
file: ./Notesnook.Inbox.API/Dockerfile
context: ./Notesnook.Inbox.API/
permissions: permissions:
packages: write packages: write
contents: read contents: read
@@ -78,7 +78,7 @@ namespace Notesnook.API.Authorization
return AuthenticateResult.Fail("API key has expired"); return AuthenticateResult.Fail("API key has expired");
} }
inboxApiKey.LastUsedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); inboxApiKey.LastUsedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
await _inboxApiKeyRepository.UpsertAsync(inboxApiKey, k => k.Key == apiKey); await _inboxApiKeyRepository.UpsertAsync(inboxApiKey, k => k.Key == apiKey);
var claims = new[] var claims = new[]
+21 -5
View File
@@ -63,7 +63,7 @@ namespace Notesnook.API.Controllers
[HttpPost("api-keys")] [HttpPost("api-keys")]
[Authorize(Policy = "Notesnook")] [Authorize(Policy = "Notesnook")]
public async Task<IActionResult> CreateApiKeyAsync([FromBody] CreateInboxApiKeyRequest request) public async Task<IActionResult> CreateApiKeyAsync([FromBody] InboxApiKey request)
{ {
var userId = User.GetUserId(); var userId = User.GetUserId();
try try
@@ -151,18 +151,34 @@ namespace Notesnook.API.Controllers
var userId = User.GetUserId(); var userId = User.GetUserId();
try try
{ {
if (string.IsNullOrWhiteSpace(request.Cipher)) if (request.Key.Algorithm != Algorithms.XSAL_X25519_7)
{ {
return BadRequest(new { error = "Inbox item is required." }); return BadRequest(new { error = $"Only {Algorithms.XSAL_X25519_7} is supported for inbox item password." });
} }
if (string.IsNullOrWhiteSpace(request.Algorithm)) if (string.IsNullOrWhiteSpace(request.Key.Cipher))
{ {
return BadRequest(new { error = "Inbox item algorithm is required." }); return BadRequest(new { error = "Inbox item password cipher is required." });
}
if (request.Key.Length <= 0)
{
return BadRequest(new { error = "Valid inbox item password length is required." });
}
if (request.Algorithm != Algorithms.Default)
{
return BadRequest(new { error = $"Only {Algorithms.Default} is supported for inbox item." });
} }
if (request.Version <= 0) if (request.Version <= 0)
{ {
return BadRequest(new { error = "Valid inbox item version is required." }); return BadRequest(new { error = "Valid inbox item version is required." });
} }
if (string.IsNullOrWhiteSpace(request.Cipher) || string.IsNullOrWhiteSpace(request.IV))
{
return BadRequest(new { error = "Inbox item cipher and iv is required." });
}
if (request.Length <= 0)
{
return BadRequest(new { error = "Valid inbox item length is required." });
}
request.UserId = userId; request.UserId = userId;
request.ItemId = ObjectId.GenerateNewId().ToString(); request.ItemId = ObjectId.GenerateNewId().ToString();
+72 -210
View File
@@ -18,19 +18,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using AngleSharp; using AngleSharp;
using AngleSharp.Dom;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
using NanoidDotNet; using Notesnook.API.Authorization;
using Notesnook.API.Extensions;
using Notesnook.API.Models; using Notesnook.API.Models;
using Notesnook.API.Services; using Notesnook.API.Services;
using Streetwriters.Common; using Streetwriters.Common;
@@ -39,6 +40,7 @@ using Streetwriters.Common.Enums;
using Streetwriters.Common.Helpers; using Streetwriters.Common.Helpers;
using Streetwriters.Common.Interfaces; using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Messages; using Streetwriters.Common.Messages;
using Streetwriters.Data.Interfaces;
using Streetwriters.Data.Repositories; using Streetwriters.Data.Repositories;
namespace Notesnook.API.Controllers namespace Notesnook.API.Controllers
@@ -95,29 +97,6 @@ namespace Notesnook.API.Controllers
return await result.FirstOrDefaultAsync(); return await result.FirstOrDefaultAsync();
} }
private async Task<Monograph> FindMonographBySlugAsync(string slug)
{
var result = await monographs.Collection.FindAsync(
Builders<Monograph>.Filter.Eq("Slug", slug), new FindOptions<Monograph>
{
Limit = 1
});
return await result.FirstOrDefaultAsync();
}
private async Task<string> GenerateUniqueSlugAsync(int length = 10, int maxAttempts = 5)
{
for (var i = 0; i < maxAttempts; i++)
{
var slug = Nanoid.Generate(size: length);
var exists = await monographs.Collection.Find(Builders<Monograph>.Filter.Eq("Slug", slug))
.Limit(1)
.AnyAsync();
if (!exists) return slug;
}
throw new Exception("Failed to generate unique slug");
}
[HttpPost] [HttpPost]
public async Task<IActionResult> PublishAsync([FromQuery] string? deviceId, [FromBody] Monograph monograph) public async Task<IActionResult> PublishAsync([FromQuery] string? deviceId, [FromBody] Monograph monograph)
{ {
@@ -129,12 +108,24 @@ namespace Notesnook.API.Controllers
var existingMonograph = await FindMonographAsync(userId, monograph); var existingMonograph = await FindMonographAsync(userId, monograph);
if (existingMonograph != null && !existingMonograph.Deleted) return await UpdateAsync(deviceId, monograph); if (existingMonograph != null && !existingMonograph.Deleted) return await UpdateAsync(deviceId, monograph);
monograph = await CreateMonographAsync(monograph, userId); if (monograph.EncryptedContent == null)
{
var sanitizationLevel = User.IsUserSubscribed() ? ContentSanitizationLevel.Partial : ContentSanitizationLevel.Full;
monograph.CompressedContent = (await SanitizeContentAsync(monograph.Content, sanitizationLevel)).CompressBrotli();
monograph.ContentSanitizationLevel = sanitizationLevel;
}
monograph.UserId = userId;
monograph.DatePublished = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (monograph.EncryptedContent?.Cipher.Length > MAX_DOC_SIZE || monograph.CompressedContent?.Length > MAX_DOC_SIZE)
return base.BadRequest("Monograph is too big. Max allowed size is 15mb.");
if (existingMonograph != null) if (existingMonograph != null)
{ {
monograph.Id = existingMonograph.Id; monograph.Id = existingMonograph.Id;
} }
monograph.Deleted = false;
monograph.ViewCount = 0;
await monographs.Collection.ReplaceOneAsync( await monographs.Collection.ReplaceOneAsync(
CreateMonographFilter(userId, monograph), CreateMonographFilter(userId, monograph),
monograph, monograph,
@@ -146,54 +137,13 @@ namespace Notesnook.API.Controllers
return Ok(new return Ok(new
{ {
id = monograph.ItemId, id = monograph.ItemId,
datePublished = monograph.DatePublished, datePublished = monograph.DatePublished
}); });
} }
catch (Exception e) catch (Exception e)
{ {
logger.LogError(e, "Failed to publish monograph"); logger.LogError(e, "Failed to publish monograph");
return BadRequest(new { error = e.Message }); return BadRequest();
}
}
[HttpPost("v2")]
public async Task<IActionResult> PublishV2Async([FromQuery] string? deviceId, [FromBody] Monograph monograph)
{
try
{
var userId = this.User.GetUserId();
var jti = this.User.FindFirstValue("jti");
var existingMonograph = await FindMonographAsync(userId, monograph);
if (existingMonograph != null && !existingMonograph.Deleted) return await UpdateAsync(deviceId, monograph);
monograph = await CreateMonographAsync(monograph, userId);
monograph.Slug = await GenerateUniqueSlugAsync();
if (existingMonograph != null)
{
monograph.Id = existingMonograph.Id;
}
await monographs.Collection.ReplaceOneAsync(
CreateMonographFilter(userId, monograph),
monograph,
new ReplaceOptions { IsUpsert = true }
);
await MarkMonographForSyncAsync(userId, monograph.ItemId ?? monograph.Id, deviceId, jti);
return Ok(new
{
id = monograph.ItemId,
datePublished = monograph.DatePublished,
publishUrl = Helpers.UrlHelper.ConstructPublishUrl(monograph)
});
}
catch (Exception e)
{
logger.LogError(e, "Failed to publish monograph");
return BadRequest(new { error = e.Message });
} }
} }
@@ -242,14 +192,13 @@ namespace Notesnook.API.Controllers
return Ok(new return Ok(new
{ {
id = monograph.ItemId, id = monograph.ItemId,
datePublished = monograph.DatePublished, datePublished = monograph.DatePublished
publishUrl = Helpers.UrlHelper.ConstructPublishUrl(existingMonograph)
}); });
} }
catch (Exception e) catch (Exception e)
{ {
logger.LogError(e, "Failed to update monograph"); logger.LogError(e, "Failed to update monograph");
return BadRequest(new { error = e.Message }); return BadRequest();
} }
} }
@@ -284,7 +233,25 @@ namespace Notesnook.API.Controllers
}); });
} }
return Ok(await ProcessMonographAsync(monograph)); if (monograph.EncryptedContent == null)
{
var isContentUnsanitized = monograph.ContentSanitizationLevel == ContentSanitizationLevel.Partial || monograph.ContentSanitizationLevel == ContentSanitizationLevel.Unknown;
if (!Constants.IS_SELF_HOSTED && isContentUnsanitized && serviceAccessor.UserSubscriptionService != null && !await serviceAccessor.UserSubscriptionService.IsUserSubscribedAsync(Clients.Notesnook.Id, monograph.UserId!))
{
var cleaned = await SanitizeContentAsync(monograph.CompressedContent?.DecompressBrotli(), ContentSanitizationLevel.Full);
monograph.CompressedContent = cleaned.CompressBrotli();
await monographs.Collection.UpdateOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
Builders<Monograph>.Update
.Set(m => m.CompressedContent, monograph.CompressedContent)
.Set(m => m.ContentSanitizationLevel, ContentSanitizationLevel.Full)
);
}
monograph.Content = monograph.CompressedContent?.DecompressBrotli();
}
monograph.ItemId ??= monograph.Id;
return Ok(monograph);
} }
[HttpGet("{id}/view")] [HttpGet("{id}/view")]
@@ -292,47 +259,47 @@ namespace Notesnook.API.Controllers
public async Task<IActionResult> TrackView([FromRoute] string id) public async Task<IActionResult> TrackView([FromRoute] string id)
{ {
var monograph = await FindMonographAsync(id); var monograph = await FindMonographAsync(id);
if (monograph == null || monograph.Deleted) if (monograph == null || monograph.Deleted) return Content(SVG_PIXEL, "image/svg+xml");
return Content(SVG_PIXEL, "image/svg+xml");
var cookieName = $"viewed_{id}"; var cookieName = $"viewed_{id}";
await TrackViewAsync(monograph, cookieName, $"/monographs/{id}"); var hasVisitedBefore = Request.Cookies.ContainsKey(cookieName);
return Content(SVG_PIXEL, "image/svg+xml"); if (monograph.SelfDestruct)
}
[HttpGet("v2/{slug}/view")]
[AllowAnonymous]
public async Task<IActionResult> TrackViewV2([FromRoute] string slug)
{
var monograph = await FindMonographBySlugAsync(slug);
if (monograph == null || monograph.Deleted)
return Content(SVG_PIXEL, "image/svg+xml");
var cookieName = $"viewed_{slug}";
await TrackViewAsync(monograph, cookieName, $"/monographs/v2/{slug}");
return Content(SVG_PIXEL, "image/svg+xml");
}
[HttpGet("v2/{slug}")]
[AllowAnonymous]
public async Task<IActionResult> GetMonographBySlugAsync([FromRoute] string slug)
{
var monograph = await FindMonographBySlugAsync(slug);
if (monograph == null || monograph.Deleted)
{ {
return NotFound(new await monographs.Collection.ReplaceOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
new Monograph
{
ItemId = id,
Id = monograph.Id,
Deleted = true,
UserId = monograph.UserId,
ViewCount = 0
}
);
await MarkMonographForSyncAsync(monograph.UserId!, id);
}
else if (!hasVisitedBefore)
{
await monographs.Collection.UpdateOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
Builders<Monograph>.Update.Inc(m => m.ViewCount, 1)
);
var cookieOptions = new CookieOptions
{ {
error = "invalid_id", Path = $"/monographs/{id}",
error_description = $"No such monograph found." HttpOnly = true,
}); Secure = Request.IsHttps,
Expires = DateTimeOffset.UtcNow.AddMonths(1)
};
Response.Cookies.Append(cookieName, "1", cookieOptions);
} }
return Ok(await ProcessMonographAsync(monograph)); return Content(SVG_PIXEL, "image/svg+xml");
} }
[HttpGet("{id}/analytics")] [HttpGet("{id}/analytics")]
[Obsolete("This endpoint is deprecated and will be removed in future versions. Use GET /monographs/{id}/metadata instead.")]
public async Task<IActionResult> GetMonographAnalyticsAsync([FromRoute] string id) public async Task<IActionResult> GetMonographAnalyticsAsync([FromRoute] string id)
{ {
if (!FeatureAuthorizationHelper.IsFeatureAllowed(Features.MONOGRAPH_ANALYTICS, Clients.Notesnook.Id, User)) if (!FeatureAuthorizationHelper.IsFeatureAllowed(Features.MONOGRAPH_ANALYTICS, Clients.Notesnook.Id, User))
@@ -376,29 +343,6 @@ namespace Notesnook.API.Controllers
return Ok(); return Ok();
} }
[HttpGet("{id}/metadata")]
public async Task<IActionResult> GetMetadataAsync([FromRoute] string id)
{
var userId = this.User.GetUserId();
var monograph = await FindMonographAsync(id);
if (monograph == null || monograph.Deleted || monograph.UserId != userId)
{
return NotFound();
}
var isPro = FeatureAuthorizationHelper.IsFeatureAllowed(Features.MONOGRAPH_ANALYTICS, Clients.Notesnook.Id, User);
var totalViews = isPro ? monograph.ViewCount : 0;
return Ok(new
{
publishUrl = Helpers.UrlHelper.ConstructPublishUrl(monograph),
analytics = new
{
totalViews
}
});
}
private async Task MarkMonographForSyncAsync(string userId, string monographId, string? deviceId, string? jti) private async Task MarkMonographForSyncAsync(string userId, string monographId, string? deviceId, string? jti)
{ {
if (deviceId == null) return; if (deviceId == null) return;
@@ -424,88 +368,6 @@ namespace Notesnook.API.Controllers
("audio", "src"), ("audio", "src"),
]; ];
private async Task<Monograph> CreateMonographAsync(Monograph monograph, string userId)
{
if (monograph.EncryptedContent == null)
{
var sanitizationLevel = User.IsUserSubscribed() ? ContentSanitizationLevel.Partial : ContentSanitizationLevel.Full;
monograph.CompressedContent = (await SanitizeContentAsync(monograph.Content, sanitizationLevel)).CompressBrotli();
monograph.ContentSanitizationLevel = sanitizationLevel;
}
monograph.UserId = userId;
monograph.DatePublished = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (monograph.EncryptedContent?.Cipher.Length > MAX_DOC_SIZE || monograph.CompressedContent?.Length > MAX_DOC_SIZE)
throw new Exception("Monograph is too big. Max allowed size is 15mb.");
monograph.Deleted = false;
monograph.ViewCount = 0;
return monograph;
}
private async Task TrackViewAsync(Monograph monograph, string cookieName, string cookiePath)
{
var hasVisitedBefore = Request.Cookies.ContainsKey(cookieName);
if (monograph.SelfDestruct)
{
await monographs.Collection.ReplaceOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
new Monograph
{
ItemId = monograph.ItemId,
Id = monograph.Id,
Deleted = true,
UserId = monograph.UserId,
ViewCount = 0
}
);
await MarkMonographForSyncAsync(monograph.UserId!, monograph.ItemId ?? monograph.Id);
}
else if (!hasVisitedBefore)
{
await monographs.Collection.UpdateOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
Builders<Monograph>.Update.Inc(m => m.ViewCount, 1)
);
var cookieOptions = new CookieOptions
{
Path = cookiePath,
HttpOnly = true,
Secure = Request.IsHttps,
Expires = DateTimeOffset.UtcNow.AddMonths(1)
};
Response.Cookies.Append(cookieName, "1", cookieOptions);
}
}
private async Task<Monograph> ProcessMonographAsync(Monograph monograph)
{
if (monograph.EncryptedContent == null)
{
var isContentUnsanitized = monograph.ContentSanitizationLevel == ContentSanitizationLevel.Partial || monograph.ContentSanitizationLevel == ContentSanitizationLevel.Unknown;
if (!Constants.IS_SELF_HOSTED && isContentUnsanitized && serviceAccessor.UserSubscriptionService != null && !await serviceAccessor.UserSubscriptionService.IsUserSubscribedAsync(Clients.Notesnook.Id, monograph.UserId!))
{
var cleaned = await SanitizeContentAsync(monograph.CompressedContent?.DecompressBrotli(), ContentSanitizationLevel.Full);
monograph.CompressedContent = cleaned.CompressBrotli();
await monographs.Collection.UpdateOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
Builders<Monograph>.Update
.Set(m => m.CompressedContent, monograph.CompressedContent)
.Set(m => m.ContentSanitizationLevel, ContentSanitizationLevel.Full)
);
}
monograph.Content = monograph.CompressedContent?.DecompressBrotli();
}
monograph.ItemId ??= monograph.Id;
return monograph;
}
private async Task<string> SanitizeContentAsync(string? content, ContentSanitizationLevel level) private async Task<string> SanitizeContentAsync(string? content, ContentSanitizationLevel level)
{ {
if (string.IsNullOrEmpty(content)) return string.Empty; if (string.IsNullOrEmpty(content)) return string.Empty;
+19 -19
View File
@@ -210,25 +210,25 @@ namespace Notesnook.API.Controllers
} }
} }
// [HttpPost("bulk-delete")] [HttpPost("bulk-delete")]
// public async Task<IActionResult> DeleteBulkAsync([FromBody] DeleteBulkObjectsRequest request) public async Task<IActionResult> DeleteBulkAsync([FromBody] DeleteBulkObjectsRequest request)
// { {
// try try
// { {
// if (request.Names == null || request.Names.Length == 0) if (request.Names == null || request.Names.Length == 0)
// { {
// return BadRequest(new { error = "No files specified for deletion." }); return BadRequest(new { error = "No files specified for deletion." });
// } }
// var userId = this.User.GetUserId(); var userId = this.User.GetUserId();
// await s3Service.DeleteObjectsAsync(userId, request.Names); await s3Service.DeleteObjectsAsync(userId, request.Names);
// return Ok(); return Ok();
// } }
// catch (Exception ex) catch (Exception ex)
// { {
// logger.LogError(ex, "Error deleting objects for user."); logger.LogError(ex, "Error deleting objects for user.");
// return BadRequest(new { error = "Failed to delete attachments." }); return BadRequest(new { error = "Failed to delete attachments." });
// } }
// } }
} }
} }
-50
View File
@@ -1,50 +0,0 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Notesnook.API.Models;
using Streetwriters.Common;
namespace Notesnook.API.Helpers
{
public class UrlHelper
{
public static string ConstructPublishUrl(string slug)
{
var baseUrl = Constants.MONOGRAPH_PUBLIC_URL;
return $"{baseUrl}/{slug}";
}
public static string ConstructPublishUrl(Monograph monograph)
{
if (!string.IsNullOrEmpty(monograph.Slug))
{
return ConstructPublishUrl("s/" + monograph.Slug);
}
return ConstructPublishUrl(monograph.ItemId ?? monograph.Id);
}
public static string ConstructPublishUrl(MonographMetadata metadata)
{
if (!string.IsNullOrEmpty(metadata.PublishUrl))
{
return ConstructPublishUrl("s/" + metadata.PublishUrl);
}
return ConstructPublishUrl(metadata.PublishUrl ?? metadata.ItemId);
}
}
}
+10 -41
View File
@@ -32,8 +32,6 @@ using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MongoDB.Driver; using MongoDB.Driver;
using Notesnook.API.Authorization; using Notesnook.API.Authorization;
using Notesnook.API.Extensions;
using Notesnook.API.Helpers;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Notesnook.API.Models; using Notesnook.API.Models;
using Notesnook.API.Services; using Notesnook.API.Services;
@@ -122,19 +120,6 @@ namespace Notesnook.API.Hubs
await base.OnConnectedAsync(); await base.OnConnectedAsync();
} }
public override async Task OnDisconnectedAsync(Exception? exception)
{
if (exception != null)
{
Logger.LogWarning(exception, "Connection {ConnectionId} disconnected with error (server-side drop)", Context.ConnectionId);
}
else
{
Logger.LogInformation("Connection {ConnectionId} disconnected cleanly (client-initiated)", Context.ConnectionId);
}
await base.OnDisconnectedAsync(exception);
}
public async Task<int> PushItems(string deviceId, SyncTransferItemV2 pushItem) public async Task<int> PushItems(string deviceId, SyncTransferItemV2 pushItem)
{ {
@@ -145,19 +130,13 @@ namespace Notesnook.API.Hubs
var stopwatch = Stopwatch.StartNew(); var stopwatch = Stopwatch.StartNew();
try try
{ {
var UpsertItems = UpsertActionsMap[pushItem.Type] ?? throw new Exception($"Invalid item type: {pushItem.Type}."); var UpsertItems = UpsertActionsMap[pushItem.Type] ?? throw new Exception($"Invalid item type: {pushItem.Type}.");
UpsertItems(pushItem.Items, userId, 1); UpsertItems(pushItem.Items, userId, 1);
if (!await unit.Commit()) return 0; if (!await unit.Commit()) return 0;
await SyncDeviceService.AddIdsToOtherDevicesAsync(userId, deviceId, pushItem.Items.Select((i) => new ItemKey(i.ItemId, pushItem.Type))); await SyncDeviceService.AddIdsToOtherDevicesAsync(userId, deviceId, pushItem.Items.Select((i) => new ItemKey(i.ItemId, pushItem.Type)));
// we need to delete the inbox items from the inbox collection
// after syncing to prevent them from being sent again in the
// next fetch.
var itemIds = pushItem.Items.Select(i => i.ItemId).ToList();
await Repositories.InboxItems.DeleteManyAsync(i => i.UserId == userId && itemIds.Contains(i.ItemId));
return 1; return 1;
} }
finally finally
@@ -270,7 +249,7 @@ namespace Notesnook.API.Hubs
ids, ids,
size: 100, size: 100,
resetSync: device.IsSyncReset, resetSync: device.IsSyncReset,
maxBytes: 3 * 1024 * 1024 maxBytes: 7 * 1024 * 1024
); );
await foreach (var chunk in chunks) await foreach (var chunk in chunks)
@@ -296,25 +275,15 @@ namespace Notesnook.API.Hubs
Builders<Monograph>.Filter.In("_id", unsyncedMonographIds) Builders<Monograph>.Filter.In("_id", unsyncedMonographIds)
) )
); );
var userMonographs = await Repositories.Monographs.Collection var userMonographs = await Repositories.Monographs.Collection.Find(filter).Project((m) => new MonographMetadata
.Find(filter)
.Project((m) => new MonographMetadata
{
DatePublished = m.DatePublished,
Deleted = m.Deleted,
Password = m.Password,
SelfDestruct = m.SelfDestruct,
Title = m.Title,
ItemId = m.ItemId ?? m.Id.ToString(),
PublishUrl = m.Slug // this will be converted to full url in the end, but we only need slug for now
})
.ToListAsync();
userMonographs = userMonographs.Select((p) =>
{ {
p.PublishUrl = UrlHelper.ConstructPublishUrl(p); DatePublished = m.DatePublished,
return p; Deleted = m.Deleted,
}).ToList(); Password = m.Password,
SelfDestruct = m.SelfDestruct,
Title = m.Title,
ItemId = m.ItemId ?? m.Id.ToString()
}).ToListAsync();
if (userMonographs.Count > 0 && !await Clients.Caller.SendMonographs(userMonographs).WaitAsync(TimeSpan.FromMinutes(10))) if (userMonographs.Count > 0 && !await Clients.Caller.SendMonographs(userMonographs).WaitAsync(TimeSpan.FromMinutes(10)))
throw new HubException("Client rejected monographs."); throw new HubException("Client rejected monographs.");
+1
View File
@@ -22,5 +22,6 @@ namespace Notesnook.API.Models
public class Algorithms public class Algorithms
{ {
public static string Default => "xcha-argon2i13-7"; public static string Default => "xcha-argon2i13-7";
public static string XSAL_X25519_7 => "xsal-x25519-7";
} }
} }
-9
View File
@@ -24,15 +24,6 @@ using NanoidDotNet;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
public class CreateInboxApiKeyRequest
{
[JsonPropertyName("name")]
public required string Name { get; set; }
[JsonPropertyName("expiryDate")]
public long ExpiryDate { get; set; }
}
public class InboxApiKey public class InboxApiKey
{ {
public InboxApiKey() public InboxApiKey()
+28 -46
View File
@@ -20,64 +20,46 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
[MessagePack.MessagePackObject] [MessagePack.MessagePackObject]
public class InboxSyncItem public class InboxSyncItem : SyncItem
{ {
[DataMember(Name = "key")]
[JsonPropertyName("key")]
[MessagePack.Key("key")]
[Required]
public required EncryptedKey Key { get; set; }
[DataMember(Name = "salt")]
[JsonPropertyName("salt")]
[MessagePack.Key("salt")]
[Required]
public required string Salt { get; set; }
}
[MessagePack.MessagePackObject]
public class EncryptedKey
{
[DataMember(Name = "alg")]
[JsonPropertyName("alg")]
[MessagePack.Key("alg")]
[Required]
public required string Algorithm { get; set; }
[DataMember(Name = "cipher")] [DataMember(Name = "cipher")]
[JsonPropertyName("cipher")] [JsonPropertyName("cipher")]
[MessagePack.Key("cipher")] [MessagePack.Key("cipher")]
[Required] [Required]
public string Cipher public required string Cipher { get; set; }
{
get; set;
}
[DataMember(Name = "userId")] [JsonPropertyName("length")]
[JsonPropertyName("userId")] [DataMember(Name = "length")]
[MessagePack.Key("userId")] [MessagePack.Key("length")]
public string? UserId
{
get; set;
}
[DataMember(Name = "id")]
[JsonPropertyName("id")]
[MessagePack.Key("id")]
public string? ItemId
{
get; set;
}
[BsonId]
[BsonIgnoreIfDefault]
[BsonRepresentation(BsonType.ObjectId)]
[JsonIgnore]
[MessagePack.IgnoreMember]
public ObjectId Id
{
get; set;
}
[JsonPropertyName("v")]
[DataMember(Name = "v")]
[MessagePack.Key("v")]
[Required] [Required]
public double Version public long Length
{
get; set;
}
[JsonPropertyName("alg")]
[DataMember(Name = "alg")]
[MessagePack.Key("alg")]
[Required]
public string Algorithm
{ {
get; set; get; set;
} }
-3
View File
@@ -56,9 +56,6 @@ namespace Notesnook.API.Models
[JsonPropertyName("title")] [JsonPropertyName("title")]
public string? Title { get; set; } public string? Title { get; set; }
[JsonPropertyName("slug")]
public string? Slug { get; set; }
[JsonPropertyName("userId")] [JsonPropertyName("userId")]
public string? UserId { get; set; } public string? UserId { get; set; }
+2 -3
View File
@@ -19,6 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
@@ -35,9 +37,6 @@ namespace Notesnook.API.Models
[JsonPropertyName("title")] [JsonPropertyName("title")]
public string? Title { get; set; } public string? Title { get; set; }
[JsonPropertyName("publishUrl")]
public string? PublishUrl { get; set; }
[JsonPropertyName("selfDestruct")] [JsonPropertyName("selfDestruct")]
public bool SelfDestruct { get; set; } public bool SelfDestruct { get; set; }
-2
View File
@@ -9,7 +9,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AngleSharp" Version="1.3.0" /> <PackageReference Include="AngleSharp" Version="1.3.0" />
<PackageReference Include="AspNetCore.HealthChecks.Aws.S3" Version="9.0.0" /> <PackageReference Include="AspNetCore.HealthChecks.Aws.S3" Version="9.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="9.0.0" />
<PackageReference Include="AWSSDK.Core" Version="3.7.304.31" /> <PackageReference Include="AWSSDK.Core" Version="3.7.304.31" />
<PackageReference Include="DotNetEnv" Version="2.3.0" /> <PackageReference Include="DotNetEnv" Version="2.3.0" />
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="6.2.0" /> <PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="6.2.0" />
@@ -18,7 +17,6 @@
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.1-rc2.2" /> <PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.1-rc2.2" />
<PackageReference Include="AWSSDK.S3" Version="3.7.310.8" /> <PackageReference Include="AWSSDK.S3" Version="3.7.310.8" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="6.0.3" /> <PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="9.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0" />
<PackageReference Include="Nanoid" Version="3.1.0" /> <PackageReference Include="Nanoid" Version="3.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-alpha.2" /> <PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-alpha.2" />
@@ -49,7 +49,7 @@ namespace Notesnook.API.Repositories
this.logger = logger; this.logger = logger;
} }
private readonly List<string> ALGORITHMS = [Algorithms.Default]; private readonly List<string> ALGORITHMS = [Algorithms.Default, Algorithms.XSAL_X25519_7];
private bool IsValidAlgorithm(string algorithm) private bool IsValidAlgorithm(string algorithm)
{ {
return ALGORITHMS.Contains(algorithm); return ALGORITHMS.Contains(algorithm);
+4 -4
View File
@@ -82,7 +82,7 @@ namespace Notesnook.API.Services
if (chunk != null) if (chunk != null)
{ {
var update = Builders<DeviceIdsChunk>.Update.AddToSetEach(x => x.Ids, ids.Select(i => i.ToString())); var update = Builders<DeviceIdsChunk>.Update.AddToSetEach(x => x.Ids, ids.Select(i => i.ToString()));
await repositories.DeviceIdsChunks.Collection.WithWriteConcern(WriteConcern.W1).UpdateOneAsync( await repositories.DeviceIdsChunks.Collection.UpdateOneAsync(
Builders<DeviceIdsChunk>.Filter.Eq(x => x.Id, chunk.Id), Builders<DeviceIdsChunk>.Filter.Eq(x => x.Id, chunk.Id),
update update
); );
@@ -96,11 +96,11 @@ namespace Notesnook.API.Services
Key = key, Key = key,
Ids = [.. ids.Select(i => i.ToString())] Ids = [.. ids.Select(i => i.ToString())]
}; };
await repositories.DeviceIdsChunks.Collection.WithWriteConcern(WriteConcern.W1).InsertOneAsync(newChunk); await repositories.DeviceIdsChunks.Collection.InsertOneAsync(newChunk);
} }
var emptyChunksFilter = DeviceIdsChunkFilter(userId, deviceId, key) & Builders<DeviceIdsChunk>.Filter.Size(x => x.Ids, 0); var emptyChunksFilter = DeviceIdsChunkFilter(userId, deviceId, key) & Builders<DeviceIdsChunk>.Filter.Size(x => x.Ids, 0);
await repositories.DeviceIdsChunks.Collection.WithWriteConcern(WriteConcern.W1).DeleteManyAsync(emptyChunksFilter); await repositories.DeviceIdsChunks.Collection.DeleteManyAsync(emptyChunksFilter);
} }
public async Task WriteIdsAsync(string userId, string deviceId, string key, IEnumerable<ItemKey> ids) public async Task WriteIdsAsync(string userId, string deviceId, string key, IEnumerable<ItemKey> ids)
@@ -121,7 +121,7 @@ namespace Notesnook.API.Services
}; };
writes.Add(new InsertOneModel<DeviceIdsChunk>(newChunk)); writes.Add(new InsertOneModel<DeviceIdsChunk>(newChunk));
} }
await repositories.DeviceIdsChunks.Collection.WithWriteConcern(WriteConcern.W1).BulkWriteAsync(writes); await repositories.DeviceIdsChunks.Collection.BulkWriteAsync(writes);
} }
public async Task<HashSet<ItemKey>> FetchUnsyncedIdsAsync(string userId, string deviceId) public async Task<HashSet<ItemKey>> FetchUnsyncedIdsAsync(string userId, string deviceId)
-2
View File
@@ -184,8 +184,6 @@ namespace Notesnook.API.Services
}; };
await Repositories.InboxApiKey.InsertAsync(defaultInboxKey); await Repositories.InboxApiKey.InsertAsync(defaultInboxKey);
} }
await Repositories.InboxItems.DeleteManyAsync(t => t.UserId == userId);
} }
await Repositories.UsersSettings.UpdateAsync(userSettings.Id, userSettings); await Repositories.UsersSettings.UpdateAsync(userSettings.Id, userSettings);
+1 -25
View File
@@ -25,7 +25,6 @@ using System.Text;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Threading.Tasks; using System.Threading.Tasks;
using Amazon.Runtime; using Amazon.Runtime;
using StackExchange.Redis;
using IdentityModel.AspNetCore.OAuth2Introspection; using IdentityModel.AspNetCore.OAuth2Introspection;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -211,30 +210,13 @@ namespace Notesnook.API
services.AddHealthChecks(); services.AddHealthChecks();
var signalR = services.AddSignalR((hub) => services.AddSignalR((hub) =>
{ {
hub.MaximumReceiveMessageSize = 100 * 1024 * 1024; hub.MaximumReceiveMessageSize = 100 * 1024 * 1024;
hub.KeepAliveInterval = TimeSpan.FromSeconds(15);
hub.ClientTimeoutInterval = TimeSpan.FromMinutes(10); hub.ClientTimeoutInterval = TimeSpan.FromMinutes(10);
hub.EnableDetailedErrors = true; hub.EnableDetailedErrors = true;
}).AddMessagePackProtocol().AddJsonProtocol(); }).AddMessagePackProtocol().AddJsonProtocol();
if (!string.IsNullOrEmpty(Constants.SIGNALR_REDIS_CONNECTION_STRING))
{
services.AddHealthChecks()
.AddRedis(Constants.SIGNALR_REDIS_CONNECTION_STRING, tags: ["ready"]);
signalR.AddStackExchangeRedis(options =>
{
options.Configuration = ConfigurationOptions.Parse(Constants.SIGNALR_REDIS_CONNECTION_STRING);
options.Configuration.AbortOnConnectFail = false;
options.Configuration.ConnectRetry = 5;
options.Configuration.ReconnectRetryPolicy = new ExponentialRetry(5000, 30000);
options.Configuration.KeepAlive = 60;
options.Configuration.ConnectTimeout = 5000;
options.Configuration.SyncTimeout = 5000;
});
}
services.AddResponseCompression(options => services.AddResponseCompression(options =>
{ {
options.EnableForHttps = true; options.EnableForHttps = true;
@@ -285,12 +267,6 @@ namespace Notesnook.API
app.UseOpenTelemetryPrometheusScrapingEndpoint((context) => context.Request.Path == "/metrics" && context.Connection.LocalPort == 5067); app.UseOpenTelemetryPrometheusScrapingEndpoint((context) => context.Request.Path == "/metrics" && context.Connection.LocalPort == 5067);
app.UseResponseCompression(); app.UseResponseCompression();
app.UseWebSockets(new Microsoft.AspNetCore.Builder.WebSocketOptions
{
KeepAliveInterval = TimeSpan.FromSeconds(30),
KeepAliveTimeout = TimeSpan.FromSeconds(60),
});
app.UseCors("notesnook"); app.UseCors("notesnook");
app.UseVersion(Servers.NotesnookAPI); app.UseVersion(Servers.NotesnookAPI);
+1 -1
View File
@@ -1,4 +1,4 @@
FROM oven/bun:1.3.5-slim FROM oven/bun:1.2.21-slim
RUN mkdir -p /home/bun/app && chown -R bun:bun /home/bun/app RUN mkdir -p /home/bun/app && chown -R bun:bun /home/bun/app
-68
View File
@@ -1,68 +0,0 @@
# Notesnook Inbox API
## Running locally
### Requirements
- Bun (v1.3.0 or higher)
### Commands
- `bun install` - Install dependencies
- `bun run dev` - Start the development server
- `bun run build` - Build the project for production
- `bun run start` - Start the production server
## Self-hosting
The easiest way to self-host is with Docker or Docker Compose.
Prerequisites:
- `docker` (Engine) installed
- `docker-compose` (optional, for multi-service setups)
Build and run with Docker:
```bash
# build the image from the current folder
docker build -t notesnook-inbox-api .
# run the container (example)
docker run --rm -p 3000:3000 \
-e PORT=3000 \
-e NOTESNOOK_API_SERVER_URL="https://api.notesnook.com" \
notesnook-inbox-api
```
Docker Compose (example):
```yaml
services:
inbox-api:
image: notesnook-inbox-api
build: .
ports:
- "3000:3000"
environment:
PORT: 3000
NOTESNOOK_API_SERVER_URL: "https://api.notesnook.com"
restart: unless-stopped
```
Environment variables:
- `PORT` — port the service listens on (default: `5181`)
- `NOTESNOOK_API_SERVER_URL` — base URL of the Notesnook API used to fetch public inbox keys
_If you prefer running without Docker, use `bun install` and `bun run start` with the environment variables set._
## Writing from scratch
The inbox API server is pretty simple to write from scratch in any programming language and/or framework. There's only one endpoint that needs to be implemented, which does these three steps:
1. Fetch the user's public inbox API key from the Notesnook API.
2. Encrypt the payload using `openpgp` or any other `openpgp` compatible library.
3. Post the encrypted payload to the Notesnook API.
You can refer to the [source code](./src/index.ts) for implementation details.
+5 -3
View File
@@ -6,7 +6,7 @@
"dependencies": { "dependencies": {
"express": "^5.1.0", "express": "^5.1.0",
"express-rate-limit": "^8.1.0", "express-rate-limit": "^8.1.0",
"openpgp": "^6.2.2", "libsodium-wrappers-sumo": "^0.7.15",
"zod": "^4.1.9", "zod": "^4.1.9",
}, },
"devDependencies": { "devDependencies": {
@@ -116,6 +116,10 @@
"is-promise": ["is-promise@4.0.0", "", {}, "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ=="], "is-promise": ["is-promise@4.0.0", "", {}, "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ=="],
"libsodium-sumo": ["libsodium-sumo@0.7.15", "", {}, "sha512-5tPmqPmq8T8Nikpm1Nqj0hBHvsLFCXvdhBFV7SGOitQPZAA6jso8XoL0r4L7vmfKXr486fiQInvErHtEvizFMw=="],
"libsodium-wrappers-sumo": ["libsodium-wrappers-sumo@0.7.15", "", { "dependencies": { "libsodium-sumo": "^0.7.15" } }, "sha512-aSWY8wKDZh5TC7rMvEdTHoyppVq/1dTSAeAR7H6pzd6QRT3vQWcT5pGwCotLcpPEOLXX6VvqihSPkpEhYAjANA=="],
"math-intrinsics": ["math-intrinsics@1.1.0", "", {}, "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g=="], "math-intrinsics": ["math-intrinsics@1.1.0", "", {}, "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g=="],
"media-typer": ["media-typer@1.1.0", "", {}, "sha512-aisnrDP4GNe06UcKFnV5bfMNPBUw4jsLGaWwWfnH3v02GnBuXX2MCVn5RbrWo0j3pczUilYblq7fQ7Nw2t5XKw=="], "media-typer": ["media-typer@1.1.0", "", {}, "sha512-aisnrDP4GNe06UcKFnV5bfMNPBUw4jsLGaWwWfnH3v02GnBuXX2MCVn5RbrWo0j3pczUilYblq7fQ7Nw2t5XKw=="],
@@ -136,8 +140,6 @@
"once": ["once@1.4.0", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="], "once": ["once@1.4.0", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="],
"openpgp": ["openpgp@6.2.2", "", {}, "sha512-P/dyEqQ3gfwOCo+xsqffzXjmUhGn4AZTOJ1LCcN21S23vAk+EAvMJOQTsb/C8krL6GjOSBxqGYckhik7+hneNw=="],
"parseurl": ["parseurl@1.3.3", "", {}, "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ=="], "parseurl": ["parseurl@1.3.3", "", {}, "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ=="],
"path-to-regexp": ["path-to-regexp@8.3.0", "", {}, "sha512-7jdwVIRtsP8MYpdXSwOS0YdD0Du+qOoF/AEPIt88PcCFrZCzx41oxku1jD88hZBwbNUIEfpqvuhjFaMAqMTWnA=="], "path-to-regexp": ["path-to-regexp@8.3.0", "", {}, "sha512-7jdwVIRtsP8MYpdXSwOS0YdD0Du+qOoF/AEPIt88PcCFrZCzx41oxku1jD88hZBwbNUIEfpqvuhjFaMAqMTWnA=="],
+1 -1
View File
@@ -22,7 +22,7 @@
"dependencies": { "dependencies": {
"express": "^5.1.0", "express": "^5.1.0",
"express-rate-limit": "^8.1.0", "express-rate-limit": "^8.1.0",
"openpgp": "^6.2.2", "libsodium-wrappers-sumo": "^0.7.15",
"zod": "^4.1.9" "zod": "^4.1.9"
}, },
"devDependencies": { "devDependencies": {
-5
View File
@@ -1,5 +0,0 @@
#!/bin/bash
GNUPGHOME=$(mktemp -d)
curl -s http://localhost:5264/inbox/public-encryption-key -H "Authorization: $API_KEY" | jq -r .key > "$GNUPGHOME"/pubkey.asc && gpg --batch --homedir "$GNUPGHOME" --import "$GNUPGHOME"/pubkey.asc >/dev/null 2>&1 && KEYID=$(gpg --homedir "$GNUPGHOME" --list-keys --with-colons | awk -F: '/^pub:/ {print $5; exit}') && printf '%s' '{"title":"Test title CLIE S","type":"note","source":"cli","version":1}' | gpg --batch --homedir "$GNUPGHOME" --trust-model always --armor --encrypt -r "$KEYID" | jq -Rs --arg alg "pgp-aes256" '{v:1, cipher:., alg:$alg}' | curl -s -X POST http://localhost:5264/inbox/items -H "Content-Type: application/json" -H "Authorization: $API_KEY" -d @- && rm -rf "$GNUPGHOME"
-18
View File
@@ -1,18 +0,0 @@
const response = await fetch("http://localhost:5181/inbox", {
method: "POST",
headers: {
Authorization: process.env.API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
title: "This is test note 4",
type: "note",
source: "script",
version: 1,
content: {
type: "html",
data: "<p>This is test note content 3</p>",
},
}),
});
console.log(await response.text());
+72 -37
View File
@@ -1,13 +1,15 @@
import express from "express"; import express from "express";
import _sodium, { base64_variants } from "libsodium-wrappers-sumo";
import { z } from "zod"; import { z } from "zod";
import { rateLimit } from "express-rate-limit"; import { rateLimit } from "express-rate-limit";
import * as openpgp from "openpgp";
const NOTESNOOK_API_SERVER_URL = process.env.NOTESNOOK_API_SERVER_URL; const NOTESNOOK_API_SERVER_URL = process.env.NOTESNOOK_API_SERVER_URL;
if (!NOTESNOOK_API_SERVER_URL) { if (!NOTESNOOK_API_SERVER_URL) {
throw new Error("NOTESNOOK_API_SERVER_URL is not defined"); throw new Error("NOTESNOOK_API_SERVER_URL is not defined");
} }
let sodium: typeof _sodium;
const RawInboxItemSchema = z.object({ const RawInboxItemSchema = z.object({
title: z.string().min(1, "Title is required"), title: z.string().min(1, "Title is required"),
pinned: z.boolean().optional(), pinned: z.boolean().optional(),
@@ -29,31 +31,62 @@ const RawInboxItemSchema = z.object({
interface EncryptedInboxItem { interface EncryptedInboxItem {
v: 1; v: 1;
cipher: string; key: Omit<EncryptedInboxItem, "key" | "iv" | "v" | "salt">;
iv: string;
alg: string; alg: string;
cipher: string;
length: number;
salt: string;
} }
/** function encrypt(rawData: string, publicKey: string): EncryptedInboxItem {
* Encrypts raw data using OpenPGP with the recipient's public key try {
* const password = sodium.crypto_aead_xchacha20poly1305_ietf_keygen();
* @param {string} rawData - The plaintext data to encrypt const saltBytes = sodium.randombytes_buf(sodium.crypto_pwhash_SALTBYTES);
* @param {string} rawPublicKey - The recipient's OpenPGP public key const key = sodium.crypto_pwhash(
*/ sodium.crypto_aead_xchacha20poly1305_ietf_KEYBYTES,
async function encrypt( password,
rawData: string, saltBytes,
rawPublicKey: string, 3, // operations limit
): Promise<EncryptedInboxItem> { 1024 * 1024 * 8, // memory limit (8MB)
const publicKey = await openpgp.readKey({ armoredKey: rawPublicKey }); sodium.crypto_pwhash_ALG_ARGON2I13
const message = await openpgp.createMessage({ text: rawData }); );
const encrypted = await openpgp.encrypt({ const nonce = sodium.randombytes_buf(
message, sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
encryptionKeys: publicKey, );
}); const data = sodium.from_string(rawData);
return { const cipher = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
v: 1, data,
cipher: encrypted, null,
alg: "pgp-aes256", null,
}; nonce,
key
);
const inboxPublicKey = sodium.from_base64(
publicKey,
base64_variants.URLSAFE_NO_PADDING
);
const encryptedKey = sodium.crypto_box_seal(key, inboxPublicKey);
return {
v: 1,
key: {
cipher: sodium.to_base64(
encryptedKey,
base64_variants.URLSAFE_NO_PADDING
),
alg: `xsal-x25519-${base64_variants.URLSAFE_NO_PADDING}`,
length: password.length,
},
iv: sodium.to_base64(nonce, base64_variants.URLSAFE_NO_PADDING),
alg: `xcha-argon2i13-${base64_variants.URLSAFE_NO_PADDING}`,
cipher: sodium.to_base64(cipher, base64_variants.URLSAFE_NO_PADDING),
length: data.length,
salt: sodium.to_base64(saltBytes, base64_variants.URLSAFE_NO_PADDING),
};
} catch (error) {
throw new Error(`encryption failed: ${error}`);
}
} }
async function getInboxPublicEncryptionKey(apiKey: string) { async function getInboxPublicEncryptionKey(apiKey: string) {
@@ -63,11 +96,11 @@ async function getInboxPublicEncryptionKey(apiKey: string) {
headers: { headers: {
Authorization: apiKey, Authorization: apiKey,
}, },
}, }
); );
if (!response.ok) { if (!response.ok) {
throw new Error( throw new Error(
`failed to fetch inbox public encryption key: ${await response.text()}`, `failed to fetch inbox public encryption key: ${await response.text()}`
); );
} }
@@ -77,7 +110,7 @@ async function getInboxPublicEncryptionKey(apiKey: string) {
async function postEncryptedInboxItem( async function postEncryptedInboxItem(
apiKey: string, apiKey: string,
item: EncryptedInboxItem, item: EncryptedInboxItem
) { ) {
const response = await fetch(`${NOTESNOOK_API_SERVER_URL}/inbox/items`, { const response = await fetch(`${NOTESNOOK_API_SERVER_URL}/inbox/items`, {
method: "POST", method: "POST",
@@ -98,12 +131,9 @@ app.use(
rateLimit({ rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute windowMs: 1 * 60 * 1000, // 1 minute
limit: 60, limit: 60,
}), })
); );
app.get("/health", (_, res) => { app.post("/inbox", async (req, res) => {
return res.status(200).json({ status: "ok" });
});
app.post("/", async (req, res) => {
try { try {
const apiKey = req.headers["authorization"]; const apiKey = req.headers["authorization"];
if (!apiKey) { if (!apiKey) {
@@ -124,9 +154,9 @@ app.post("/", async (req, res) => {
}); });
} }
const encryptedItem = await encrypt( const encryptedItem = encrypt(
JSON.stringify(validationResult.data), JSON.stringify(validationResult.data),
inboxPublicKey, inboxPublicKey
); );
console.log("[info] encrypted item"); console.log("[info] encrypted item");
@@ -150,9 +180,14 @@ app.post("/", async (req, res) => {
} }
}); });
const PORT = Number(process.env.PORT || "5181"); (async () => {
app.listen(PORT, () => { await _sodium.ready;
console.log(`📫 notesnook inbox api server running on port ${PORT}`); sodium = _sodium;
});
const PORT = Number(process.env.PORT || "5181");
app.listen(PORT, () => {
console.log(`📫 notesnook inbox api server running on port ${PORT}`);
});
})();
export default app; export default app;
-1
View File
@@ -39,7 +39,6 @@ namespace Streetwriters.Common
AppId = ApplicationType.NOTESNOOK, AppId = ApplicationType.NOTESNOOK,
AccountRecoveryRedirectURL = $"{Constants.NOTESNOOK_APP_HOST}/account/recovery", AccountRecoveryRedirectURL = $"{Constants.NOTESNOOK_APP_HOST}/account/recovery",
EmailConfirmedRedirectURL = $"{Constants.NOTESNOOK_APP_HOST}/account/verified", EmailConfirmedRedirectURL = $"{Constants.NOTESNOOK_APP_HOST}/account/verified",
PackageName = "com.streetwriters.notesnook",
OnEmailConfirmed = async (userId) => OnEmailConfirmed = async (userId) =>
{ {
await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage
-2
View File
@@ -79,8 +79,6 @@ namespace Streetwriters.Common
public static string? SUBSCRIPTIONS_CERT_PATH => ReadSecret("SUBSCRIPTIONS_CERT_PATH"); public static string? SUBSCRIPTIONS_CERT_PATH => ReadSecret("SUBSCRIPTIONS_CERT_PATH");
public static string? SUBSCRIPTIONS_CERT_KEY_PATH => ReadSecret("SUBSCRIPTIONS_CERT_KEY_PATH"); public static string? SUBSCRIPTIONS_CERT_KEY_PATH => ReadSecret("SUBSCRIPTIONS_CERT_KEY_PATH");
public static string[] NOTESNOOK_CORS_ORIGINS => ReadSecret("NOTESNOOK_CORS")?.Split(",") ?? []; public static string[] NOTESNOOK_CORS_ORIGINS => ReadSecret("NOTESNOOK_CORS")?.Split(",") ?? [];
public static string? SIGNALR_REDIS_CONNECTION_STRING => ReadSecret("SIGNALR_REDIS_CONNECTION_STRING");
public static string MONOGRAPH_PUBLIC_URL => ReadSecret("MONOGRAPH_PUBLIC_URL") ?? "https://monogr.ph";
public static string? ReadSecret(string name) public static string? ReadSecret(string name)
{ {
-1
View File
@@ -39,7 +39,6 @@ namespace Streetwriters.Common.Models
public required string SenderName { get; set; } public required string SenderName { get; set; }
public required string EmailConfirmedRedirectURL { get; set; } public required string EmailConfirmedRedirectURL { get; set; }
public required string AccountRecoveryRedirectURL { get; set; } public required string AccountRecoveryRedirectURL { get; set; }
public required string PackageName { get; set; }
public Func<string, Task>? OnEmailConfirmed { get; set; } public Func<string, Task>? OnEmailConfirmed { get; set; }
} }
@@ -34,7 +34,6 @@ using Microsoft.AspNetCore.RateLimiting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Streetwriters.Common; using Streetwriters.Common;
using Streetwriters.Common.Enums; using Streetwriters.Common.Enums;
using Streetwriters.Common.Helpers;
using Streetwriters.Common.Interfaces; using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Messages; using Streetwriters.Common.Messages;
using Streetwriters.Common.Models; using Streetwriters.Common.Models;
@@ -53,32 +52,17 @@ namespace Streetwriters.Identity.Controllers
[Authorize(LocalApi.PolicyName)] [Authorize(LocalApi.PolicyName)]
public class AccountController : IdentityControllerBase public class AccountController : IdentityControllerBase
{ {
private static readonly string emailConfirmedPageHtml = HtmlHelper.ReadMinifiedHtmlFile("Templates/EmailConfirmedPage.html");
private static readonly string emailConfirmErrorPageHtml = HtmlHelper.ReadMinifiedHtmlFile("Templates/EmailConfirmErrorPage.html");
private IPersistedGrantStore PersistedGrantStore { get; set; } private IPersistedGrantStore PersistedGrantStore { get; set; }
private ITokenGenerationService TokenGenerationService { get; set; } private ITokenGenerationService TokenGenerationService { get; set; }
private IUserAccountService UserAccountService { get; set; } private IUserAccountService UserAccountService { get; set; }
private EmailAddressValidator EmailValidator { get; set; }
private readonly ILogger<AccountController> logger; private readonly ILogger<AccountController> logger;
public AccountController(UserManager<User> _userManager, ITemplatedEmailSender _emailSender,
public AccountController( SignInManager<User> _signInManager, RoleManager<MongoRole> _roleManager, IPersistedGrantStore store,
UserManager<User> _userManager, ITokenGenerationService tokenGenerationService, IMFAService _mfaService, IUserAccountService userAccountService, ILogger<AccountController> logger) : base(_userManager, _emailSender, _signInManager, _roleManager, _mfaService)
ITemplatedEmailSender _emailSender,
SignInManager<User> _signInManager,
RoleManager<MongoRole> _roleManager,
IPersistedGrantStore store,
ITokenGenerationService tokenGenerationService,
IMFAService _mfaService,
IUserAccountService userAccountService,
ILogger<AccountController> logger,
EmailAddressValidator emailValidator
) : base(_userManager, _emailSender, _signInManager, _roleManager, _mfaService)
{ {
PersistedGrantStore = store; PersistedGrantStore = store;
TokenGenerationService = tokenGenerationService; TokenGenerationService = tokenGenerationService;
UserAccountService = userAccountService; UserAccountService = userAccountService;
EmailValidator = emailValidator;
this.logger = logger; this.logger = logger;
} }
@@ -97,22 +81,10 @@ namespace Streetwriters.Identity.Controllers
{ {
case TokenType.CONFRIM_EMAIL: case TokenType.CONFRIM_EMAIL:
{ {
if (await UserManager.IsEmailConfirmedAsync(user)) if (await UserManager.IsEmailConfirmedAsync(user)) return Ok("Email already verified.");
{
return Content(
emailConfirmedPageHtml.Replace("{{subheading}}", "Your email is already verified."),
"text/html"
);
}
var result = await UserManager.ConfirmEmailAsync(user, code); var result = await UserManager.ConfirmEmailAsync(user, code);
if (!result.Succeeded) if (!result.Succeeded) return BadRequest(result.Errors.ToErrors());
{
return Content(
emailConfirmErrorPageHtml.Replace("{{errors}}", string.Join(" ", result.Errors.ToErrors())),
"text/html"
);
}
if (await UserManager.IsInRoleAsync(user, client.Id) && client.OnEmailConfirmed != null) if (await UserManager.IsInRoleAsync(user, client.Id) && client.OnEmailConfirmed != null)
{ {
@@ -122,10 +94,8 @@ namespace Streetwriters.Identity.Controllers
if (!await UserManager.GetTwoFactorEnabledAsync(user)) if (!await UserManager.GetTwoFactorEnabledAsync(user))
await MFAService.EnableMFAAsync(user, MFAMethods.Email); await MFAService.EnableMFAAsync(user, MFAMethods.Email);
return Content( var redirectUrl = $"{client.EmailConfirmedRedirectURL}?userId={userId}";
emailConfirmedPageHtml.Replace("{{subheading}}", "Your email has been confirmed."), return RedirectPermanent(redirectUrl);
"text/html"
);
} }
case TokenType.RESET_PASSWORD: case TokenType.RESET_PASSWORD:
{ {
@@ -161,11 +131,6 @@ namespace Streetwriters.Identity.Controllers
} }
else else
{ {
if (!await EmailValidator.IsEmailAddressValidAsync(newEmail.ToLowerInvariant()))
{
return BadRequest("Invalid email address.");
}
var code = await UserManager.GenerateChangeEmailTokenAsync(user, newEmail); var code = await UserManager.GenerateChangeEmailTokenAsync(user, newEmail);
await EmailSender.SendChangeEmailConfirmationAsync(newEmail, code, client); await EmailSender.SendChangeEmailConfirmationAsync(newEmail, code, client);
} }
@@ -190,8 +155,8 @@ namespace Streetwriters.Identity.Controllers
var client = Clients.FindClientById(form.ClientId); var client = Clients.FindClientById(form.ClientId);
if (client == null) return BadRequest("Invalid client_id."); if (client == null) return BadRequest("Invalid client_id.");
var user = await UserManager.FindByEmailAsync(form.Email); var user = await UserManager.FindByEmailAsync(form.Email) ?? throw new Exception("User not found.");
if (user == null || !await UserService.IsUserValidAsync(UserManager, user, form.ClientId)) return Ok(); if (!await UserService.IsUserValidAsync(UserManager, user, form.ClientId)) return Ok();
var code = await UserManager.GenerateUserTokenAsync(user, TokenOptions.DefaultProvider, "ResetPassword"); var code = await UserManager.GenerateUserTokenAsync(user, TokenOptions.DefaultProvider, "ResetPassword");
var callbackUrl = UrlExtensions.TokenLink(user.Id.ToString(), code, client.Id, TokenType.RESET_PASSWORD); var callbackUrl = UrlExtensions.TokenLink(user.Id.ToString(), code, client.Id, TokenType.RESET_PASSWORD);
@@ -185,14 +185,7 @@ namespace Streetwriters.Identity.Services
}; };
} }
var otherErrors = result.Errors return SignupResponse.Error(result.Errors.ToErrors());
.Where(e => e.Code != "DuplicateUserName" && e.Code != "DuplicateEmail")
.ToErrors();
var hasDuplicate = result.Errors.Any(e => e.Code == "DuplicateUserName" || e.Code == "DuplicateEmail");
var errors = hasDuplicate
? ["Unable to create an account on this email.", .. otherErrors]
: otherErrors;
return SignupResponse.Error(errors);
} }
catch (System.Exception ex) catch (System.Exception ex)
{ {
@@ -1,128 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Email Confirmation Failed - Notesnook</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family:
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu,
Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
background: #ffffff;
height: 100vh;
display: flex;
flex-direction: column;
overflow-y: auto;
font-size: 16px;
}
.main {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2rem;
}
.icon-wrapper {
background: #fdecea;
border-radius: 100px;
padding: 20px;
display: flex;
align-items: center;
justify-content: center;
}
.icon-wrapper svg {
width: 72px;
height: 72px;
fill: #c0392b;
}
.heading {
font-size: 2.5em;
font-weight: 700;
text-align: center;
margin-top: 20px;
color: #000;
}
.subheading {
font-size: 1.5em;
font-weight: 600;
text-align: center;
margin-top: 8px;
color: #5b5b5b;
}
.body-text {
font-size: 1.2em;
text-align: center;
margin-top: 8px;
color: #808080;
word-wrap: break-word;
}
.footer {
background: #f0f0f0;
padding: 40px 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.footer-heading {
font-size: 1.2em;
font-weight: bold;
text-align: center;
}
.footer-subtext {
font-size: 1em;
text-align: center;
margin-top: 8px;
color: #5b5b5b;
}
@media (max-width: 480px) {
body {
font-size: 14px;
}
}
@media (min-width: 769px) {
body {
font-size: 18px;
}
}
</style>
</head>
<body>
<div class="main">
<div class="icon-wrapper">
<!-- Mail X Icon -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
d="M13 19C13 18.66 13.04 18.33 13.09 18H4V8L12 13L20 8V13.09C20.72 13.21 21.39 13.46 22 13.81V6C22 4.9 21.1 4 20 4H4C2.9 4 2 4.9 2 6V18C2 19.1 2.9 20 4 20H13.09C13.04 19.67 13 19.34 13 19M20 6L12 11L4 6H20M21.12 15.46L19 17.59L16.88 15.46L15.47 16.88L17.59 19L15.47 21.12L16.88 22.54L19 20.41L21.12 22.54L22.54 21.12L20.41 19L22.54 16.88L21.12 15.46Z"
/>
</svg>
</div>
<h1 class="heading">Uh oh!</h1>
<p class="subheading">Email confirmation failed. Please try again!</p>
<p class="body-text">{{errors}}</p>
</div>
<div class="footer">
<h2 class="footer-heading">Notesnook</h2>
<p class="footer-subtext">Privacy for everyone</p>
</div>
</body>
</html>
@@ -1,213 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Email Confirmed - Notesnook</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family:
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu,
Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
background: #ffffff;
height: 100vh;
display: flex;
flex-direction: column;
overflow-y: auto;
font-size: 16px;
}
.main {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2rem;
}
.icon-wrapper {
background: #e8f5e9;
border-radius: 100px;
padding: 20px;
display: flex;
align-items: center;
justify-content: center;
}
.icon-wrapper svg {
width: 72px;
height: 72px;
fill: #008837;
}
.heading {
font-size: 2.5em;
font-weight: 700;
text-align: center;
margin-top: 20px;
color: #000;
}
.subheading {
font-size: 1.5em;
font-weight: 600;
text-align: center;
margin-top: 8px;
color: #5b5b5b;
}
.body-text {
font-size: 1.2em;
text-align: center;
margin-top: 8px;
color: #808080;
word-wrap: break-word;
}
.footer {
background: #f0f0f0;
padding: 40px 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.footer-heading {
font-size: 1.2em;
font-weight: bold;
text-align: center;
}
.footer-subtext {
font-size: 1em;
text-align: center;
margin-top: 8px;
color: #5b5b5b;
}
.social-icons {
display: flex;
gap: 8px;
margin-top: 20px;
}
.social-icons a {
display: flex;
align-items: center;
justify-content: center;
color: #5b5b5b;
cursor: pointer;
text-decoration: none;
}
.social-icons a:hover svg {
opacity: 0.8;
}
.social-icons svg {
width: 30px;
height: 30px;
}
.promo-text {
font-size: 0.85em;
text-align: center;
margin-top: 8px;
color: #5b5b5b;
}
.promo-text .hashtag {
font-weight: bold;
color: #008837;
}
@media (max-width: 480px) {
body {
font-size: 14px;
}
}
@media (min-width: 769px) {
body {
font-size: 18px;
}
}
</style>
</head>
<body>
<div class="main">
<div class="icon-wrapper">
<!-- Mail Check Icon -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
d="M13 19C13 18.66 13.04 18.33 13.09 18H4V8L12 13L20 8V13.09C20.72 13.21 21.39 13.46 22 13.81V6C22 4.9 21.1 4 20 4H4C2.9 4 2 4.9 2 6V18C2 19.1 2.9 20 4 20H13.09C13.04 19.67 13 19.34 13 19M20 6L12 11L4 6H20M17.75 22.16L15 19.16L16.16 18L17.75 19.59L21.34 16L22.5 17.41L17.75 22.16"
/>
</svg>
</div>
<h1 class="heading">Huzzah!</h1>
<p class="subheading">{{subheading}}</p>
<p class="body-text">
Thank you for choosing end-to-end encrypted note taking. Now you can
sync your notes to unlimited devices.
</p>
</div>
<div class="footer">
<h2 class="footer-heading">Share Notesnook with friends!</h2>
<p class="footer-subtext">Because where's the fun in nookin' alone?</p>
<div class="social-icons">
<!-- Discord -->
<a
href="https://discord.com/invite/zQBK97EE22"
target="_blank"
rel="noopener noreferrer"
title="Discord"
>
<svg viewBox="0 0 24 24" fill="currentColor">
<path
d="M20.317 4.37a19.791 19.791 0 0 0-4.885-1.515.074.074 0 0 0-.079.037c-.21.375-.444.864-.608 1.25a18.27 18.27 0 0 0-5.487 0 12.64 12.64 0 0 0-.617-1.25.077.077 0 0 0-.079-.037A19.736 19.736 0 0 0 3.677 4.37a.07.07 0 0 0-.032.027C.533 9.046-.32 13.58.099 18.057a.082.082 0 0 0 .031.057 19.9 19.9 0 0 0 5.993 3.03.078.078 0 0 0 .084-.028c.462-.63.874-1.295 1.226-1.994a.076.076 0 0 0-.041-.106 13.107 13.107 0 0 1-1.872-.892.077.077 0 0 1-.008-.128 10.2 10.2 0 0 0 .372-.292.074.074 0 0 1 .077-.01c3.928 1.793 8.18 1.793 12.062 0a.074.074 0 0 1 .078.01c.12.098.246.198.373.292a.077.077 0 0 1-.006.127 12.299 12.299 0 0 1-1.873.892.077.077 0 0 0-.041.107c.36.698.772 1.362 1.225 1.993a.076.076 0 0 0 .084.028 19.839 19.839 0 0 0 6.002-3.03.077.077 0 0 0 .032-.054c.5-5.177-.838-9.674-3.549-13.66a.061.061 0 0 0-.031-.03zM8.02 15.33c-1.183 0-2.157-1.085-2.157-2.419 0-1.333.956-2.419 2.157-2.419 1.21 0 2.176 1.096 2.157 2.42 0 1.333-.956 2.418-2.157 2.418zm7.975 0c-1.183 0-2.157-1.085-2.157-2.419 0-1.333.955-2.419 2.157-2.419 1.21 0 2.176 1.096 2.157 2.42 0 1.333-.946 2.418-2.157 2.418z"
/>
</svg>
</a>
<!-- Twitter -->
<a
href="https://twitter.com/notesnook"
target="_blank"
rel="noopener noreferrer"
title="Twitter"
>
<svg viewBox="0 0 24 24" fill="currentColor">
<path
d="M23.953 4.57a10 10 0 0 1-2.825.775 4.958 4.958 0 0 0 2.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 0 0-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 0 0-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 0 1-2.228-.616v.06a4.923 4.923 0 0 0 3.946 4.827 4.996 4.996 0 0 1-2.212.085 4.936 4.936 0 0 0 4.604 3.417 9.867 9.867 0 0 1-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 0 0 7.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0 0 24 4.59z"
/>
</svg>
</a>
<!-- Reddit -->
<a
href="https://reddit.com/r/Notesnook"
target="_blank"
rel="noopener noreferrer"
title="Reddit"
>
<svg viewBox="0 0 24 24" fill="currentColor">
<path
d="M12 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0zm5.01 4.744c.688 0 1.25.561 1.25 1.249a1.25 1.25 0 0 1-2.498.056l-2.597-.547-.8 3.747c1.824.07 3.48.632 4.674 1.488.308-.309.73-.491 1.207-.491.968 0 1.754.786 1.754 1.754 0 .716-.435 1.333-1.01 1.614a3.111 3.111 0 0 1 .042.52c0 2.694-3.13 4.87-7.004 4.87-3.874 0-7.004-2.176-7.004-4.87 0-.183.015-.366.043-.534A1.748 1.748 0 0 1 4.028 12c0-.968.786-1.754 1.754-1.754.463 0 .898.196 1.207.49 1.207-.883 2.878-1.43 4.744-1.487l.885-4.182a.342.342 0 0 1 .14-.197.35.35 0 0 1 .238-.042l2.906.617a1.214 1.214 0 0 1 1.108-.701zM9.25 12C8.561 12 8 12.562 8 13.25c0 .687.561 1.248 1.25 1.248.687 0 1.248-.561 1.248-1.249 0-.688-.561-1.249-1.249-1.249zm5.5 0c-.687 0-1.248.561-1.248 1.25 0 .687.561 1.248 1.249 1.248.688 0 1.249-.561 1.249-1.249 0-.687-.562-1.249-1.25-1.249zm-5.466 3.99a.327.327 0 0 0-.231.094.33.33 0 0 0 0 .463c.842.842 2.484.913 2.961.913.477 0 2.105-.056 2.961-.913a.361.361 0 0 0 .029-.463.33.33 0 0 0-.464 0c-.547.533-1.684.73-2.512.73-.828 0-1.979-.196-2.512-.73a.326.326 0 0 0-.232-.095z"
/>
</svg>
</a>
</div>
<p class="promo-text">
Use <span class="hashtag">#notesnook</span> and get a chance to win free
promo codes.
</p>
</div>
</body>
</html>
+9 -30
View File
@@ -18,49 +18,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Linq; using System.Linq;
using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Lib.AspNetCore.ServerSentEvents; using Lib.AspNetCore.ServerSentEvents;
using System.Security.Claims; using System.Security.Claims;
using System.Collections.Generic;
namespace Streetwriters.Messenger.Helpers namespace Streetwriters.Messenger.Helpers
{ {
public class SSEHelper public class SSEHelper
{ {
public static async Task SendEventToUserAsync(string data, IServerSentEventsService sseService, string userId, string? originTokenId = null, CancellationToken cancellationToken = default) public static async Task SendEventToUserAsync(string data, IServerSentEventsService sseService, string userId, string? originTokenId = null)
{
var clients = sseService.GetClients()
.Where(c => c.User?.FindFirstValue("sub") == userId)
.Where(c => originTokenId == null || c.User?.FindFirstValue("jti") != originTokenId);
await SendEventToClientsAsync(clients, data, cancellationToken);
}
public static async Task SendEventToAllUsersAsync(string data, IServerSentEventsService sseService, CancellationToken cancellationToken = default)
{
await SendEventToClientsAsync(sseService.GetClients(), data, cancellationToken);
}
private static async Task SendEventToClientsAsync(IEnumerable<IServerSentEventsClient> clients, string data, CancellationToken cancellationToken)
{ {
var clients = sseService.GetClients().Where(c => c.User.FindFirstValue("sub") == userId);
foreach (var client in clients) foreach (var client in clients)
{ {
if (originTokenId != null && client.User.FindFirstValue("jti") == originTokenId) continue;
if (!client.IsConnected) continue; if (!client.IsConnected) continue;
await client.SendEventAsync(data);
try
{
await client.SendEventAsync(data, cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
break;
}
catch
{
}
} }
} }
public static async Task SendEventToAllUsersAsync(string data, IServerSentEventsService sseService)
{
await sseService.SendEventAsync(data);
}
} }
} }
@@ -21,7 +21,6 @@ using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Lib.AspNetCore.ServerSentEvents; using Lib.AspNetCore.ServerSentEvents;
using Streetwriters.Messenger.Helpers; using Streetwriters.Messenger.Helpers;
using System.Text.Json; using System.Text.Json;
@@ -34,14 +33,12 @@ namespace Streetwriters.Messenger.Services
private const string HEARTBEAT_MESSAGE_FORMAT = "Streetwriters Heartbeat ({0} UTC)"; private const string HEARTBEAT_MESSAGE_FORMAT = "Streetwriters Heartbeat ({0} UTC)";
private readonly IServerSentEventsService _serverSentEventsService; private readonly IServerSentEventsService _serverSentEventsService;
private readonly ILogger<HeartbeatService> _logger;
#endregion #endregion
#region Constructor #region Constructor
public HeartbeatService(IServerSentEventsService serverSentEventsService, ILogger<HeartbeatService> logger) public HeartbeatService(IServerSentEventsService serverSentEventsService)
{ {
_serverSentEventsService = serverSentEventsService; _serverSentEventsService = serverSentEventsService;
_logger = logger;
} }
#endregion #endregion
@@ -50,28 +47,15 @@ namespace Streetwriters.Messenger.Services
{ {
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
try var message = JsonSerializer.Serialize(new
{ {
var message = JsonSerializer.Serialize(new type = "heartbeat",
data = JsonSerializer.Serialize(new
{ {
type = "heartbeat", t = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
data = JsonSerializer.Serialize(new })
{ });
t = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() await SSEHelper.SendEventToAllUsersAsync(message, _serverSentEventsService);
})
});
await SSEHelper.SendEventToAllUsersAsync(message, _serverSentEventsService, stoppingToken);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
break;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to send SSE heartbeat to one or more clients.");
}
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
} }
} }
@@ -8,7 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DotNetEnv" Version="2.3.0" /> <PackageReference Include="DotNetEnv" Version="2.3.0" />
<PackageReference Include="Lib.AspNetCore.ServerSentEvents" Version="9.1.0" /> <PackageReference Include="Lib.AspNetCore.ServerSentEvents" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0"
NoWarn="NU1605" /> NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0"
+7 -7
View File
@@ -39,7 +39,7 @@ function isValidUrl(urlString: string): boolean {
// Handle proxied request with redirect support // Handle proxied request with redirect support
async function proxyRequest( async function proxyRequest(
targetUrl: string, targetUrl: string,
redirectCount = 0, redirectCount = 0
): Promise<Response> { ): Promise<Response> {
if (redirectCount >= MAX_REDIRECTS) { if (redirectCount >= MAX_REDIRECTS) {
return new Response("Too many redirects", { return new Response("Too many redirects", {
@@ -147,7 +147,7 @@ const server = Bun.serve({
method2: "GET /?url=<encoded-url>", method2: "GET /?url=<encoded-url>",
example1: `${url.origin}/https://example.com/image.jpg`, example1: `${url.origin}/https://example.com/image.jpg`,
example2: `${url.origin}/?url=${encodeURIComponent( example2: `${url.origin}/?url=${encodeURIComponent(
"https://example.com/image.jpg", "https://example.com/image.jpg"
)}`, )}`,
}, },
endpoints: { endpoints: {
@@ -190,7 +190,7 @@ const server = Bun.serve({
{ {
status: 400, status: 400,
headers: corsHeaders, headers: corsHeaders,
}, }
); );
} }
@@ -218,8 +218,8 @@ const server = Bun.serve({
status: 200, status: 200,
headers: { headers: {
"Content-Type": "text/html; charset=utf-8", "Content-Type": "text/html; charset=utf-8",
// "Content-Security-Policy": "frame-ancestors *", "Content-Security-Policy": "frame-ancestors *",
// "X-Frame-Options": "ALLOWALL", "X-Frame-Options": "ALLOWALL",
}, },
}); });
} }
@@ -239,7 +239,7 @@ const server = Bun.serve({
}); });
console.log( console.log(
`🚀 CORS Proxy Server running on http://${server.hostname}:${server.port}`, `🚀 CORS Proxy Server running on http://${server.hostname}:${server.port}`
); );
console.log(`📋 Health check: http://${server.hostname}:${server.port}/health`); console.log(`📋 Health check: http://${server.hostname}:${server.port}/health`);
console.log(`🌍 Environment: ${Bun.env.NODE_ENV || "development"}`); console.log(`🌍 Environment: ${Bun.env.NODE_ENV || "development"}`);
@@ -280,7 +280,7 @@ function serveYouTubeEmbed(url: string) {
</head> </head>
<body> <body>
<iframe src="${transformYouTubeUrl( <iframe src="${transformYouTubeUrl(
url, url
)}" allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture;web-share" allowfullscreen referrerpolicy="strict-origin-when-cross-origin" title="Video player"></iframe> )}" allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture;web-share" allowfullscreen referrerpolicy="strict-origin-when-cross-origin" title="Video player"></iframe>
</body> </body>
</html>`; </html>`;