diff --git a/PasteBin/Program.cs b/PasteBin/Program.cs index 7e0fab8..90e8acb 100644 --- a/PasteBin/Program.cs +++ b/PasteBin/Program.cs @@ -12,6 +12,8 @@ class Program { private static string salt = string.Empty; private static string smallerSalt = string.Empty; + private static string cfTurnstileSiteKey = string.Empty; + public static string cfTurnstileSecret = string.Empty; public static Dictionary accountType = new Dictionary { @@ -156,6 +158,7 @@ class Program } var html = File.ReadAllText("assets/login.html"); html = html.Replace("{html}", "sign up"); + html = html.Replace("{sitekey}", cfTurnstileSiteKey); context.Response.ContentType = "text/html"; await context.Response.WriteAsync(html); return; @@ -169,6 +172,7 @@ class Program } var html = File.ReadAllText("assets/signup.html"); html = html.Replace("{html}", "login"); + html = html.Replace("{sitekey}", cfTurnstileSiteKey); context.Response.ContentType = "text/html"; await context.Response.WriteAsync(html); return; @@ -1503,7 +1507,7 @@ class Program await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No content uploaded" }.ToString()); return; } - if (string.IsNullOrEmpty(user.UUID)) + if (!string.IsNullOrEmpty(user.UUID)) { using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) { @@ -1996,6 +2000,23 @@ class Program var data = JObject.Parse(requestBody); var username = SanitizeInput(data["username"]?.ToString() ?? ""); var password = SanitizeInput(data["password"]?.ToString() ?? ""); + var cfToken = SanitizeInput(data["cf"]?.ToString() ?? ""); + if (string.IsNullOrEmpty(cfToken) || string.IsNullOrEmpty(cfToken)) + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid Cloudflare Turnstile Token" }.ToString()); + return; + } + bool cfValid = await Turnstile.VerifyTurnstileToken(cfToken); + if (!cfValid) + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid Cloudflare Turnstile Token" }.ToString()); + return; + } + if (username.Length > 20 || username.Length < 3 || password.Length > 50 || password.Length < 8) { @@ -2118,7 +2139,22 @@ class Program var data = JObject.Parse(requestBody); var username = SanitizeInput(data["username"]?.ToString() ?? ""); var password = SanitizeInput(data["password"]?.ToString() ?? ""); - + var cfToken = SanitizeInput(data["cf"]?.ToString() ?? ""); + if (string.IsNullOrEmpty(cfToken) || string.IsNullOrEmpty(cfToken)) + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid Cloudflare Turnstile Token" }.ToString()); + return; + } + bool cfValid = await Turnstile.VerifyTurnstileToken(cfToken); + if (!cfValid) + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid Cloudflare Turnstile Token" }.ToString()); + return; + } if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { context.Response.StatusCode = 400; @@ -2454,6 +2490,11 @@ class Program var config = JObject.Parse(File.ReadAllText("config.json")); salt = config["Salt"]?.ToString() ?? BCrypt.Net.BCrypt.GenerateSalt(13); smallerSalt = config["SmallerSalt"]?.ToString() ?? BCrypt.Net.BCrypt.GenerateSalt(8); + cfTurnstileSecret = config["CF_TurnstileSecret"]?.ToString() ?? ""; + cfTurnstileSiteKey = config["CF_TurnstileSiteKey"]?.ToString() ?? ""; + + config["Salt"] = salt; + config["SmallerSalt"] = smallerSalt; File.WriteAllText("config.json", config.ToString()); Logging.Api_CreateMessage = config["API_CreateMessage"]?.ToString() ?? ""; @@ -2467,6 +2508,15 @@ class Program smallerSalt = config["SmallerSalt"]?.ToString() ?? BCrypt.Net.BCrypt.GenerateSalt(8); File.WriteAllText("config.json", config.ToString()); } + if (cfTurnstileSiteKey == "" || cfTurnstileSecret == "") + { + Console.ForegroundColor = ConsoleColor.Red; + Console.Write("error: "); + Console.ResetColor(); + Console.WriteLine("Cloudflare Turnstile secret or site key is not set in config.json"); + Console.ReadKey(); + Environment.Exit(1); + } if (!Directory.Exists("pastes")) Directory.CreateDirectory("pastes"); } diff --git a/PasteBin/Turnstile.cs b/PasteBin/Turnstile.cs new file mode 100644 index 0000000..29baae6 --- /dev/null +++ b/PasteBin/Turnstile.cs @@ -0,0 +1,36 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Net.Http; +using System.Text; + +namespace Sharpbin +{ + public class Turnstile + { + public static async Task VerifyTurnstileToken(string token) + { + var httpClient = new HttpClient(); + JObject requestBody = new JObject + { + ["secret"] = Program.cfTurnstileSecret, + ["response"] = token + }; + var request = new HttpRequestMessage + { + Method = HttpMethod.Post, + RequestUri = new Uri("https://challenges.cloudflare.com/turnstile/v0/siteverify"), + Content = new StringContent(requestBody.ToString(), Encoding.UTF8, "application/json") + }; + using (var response = await httpClient.SendAsync(request)) + { + var jsonresponse = JObject.Parse(await response.Content.ReadAsStringAsync()); + Console.WriteLine(jsonresponse["success"]?.ToString()); + if (jsonresponse["success"]?.ToString().ToLower() == "true") + { + return true; + } + return false; + } + } + } +} diff --git a/PasteBin/assets/admin.html b/PasteBin/assets/admin.html index 1f7fb99..7b9be89 100644 --- a/PasteBin/assets/admin.html +++ b/PasteBin/assets/admin.html @@ -5,7 +5,7 @@ - create paste + dashboard diff --git a/PasteBin/assets/dash.html b/PasteBin/assets/dash.html index 5904067..f504472 100644 --- a/PasteBin/assets/dash.html +++ b/PasteBin/assets/dash.html @@ -5,7 +5,7 @@ - create paste + dashboard diff --git a/PasteBin/assets/login.html b/PasteBin/assets/login.html index fccd128..b6889bb 100644 --- a/PasteBin/assets/login.html +++ b/PasteBin/assets/login.html @@ -1,11 +1,14 @@ + - create paste + + login + -
+

login

@@ -21,6 +24,7 @@ +

@@ -36,13 +40,14 @@ var usernameContent = document.getElementById("usernameInput").value; var passwordContent = document.getElementById("passwordInput").value; + var cf = turnstile.getResponse(); + if (isLoggingIn) { createNotification("Please wait for the login to finish."); return; } isLoggingIn = true; - console.log(isLoggingIn); var requestUrl = "/api/accounts/login"; fetch(requestUrl, { method: "POST", @@ -51,10 +56,11 @@ }, body: JSON.stringify({ username: usernameContent, - password: passwordContent + password: passwordContent, + cf: cf }) }) - + .then(response => response.json()) .then(obj => { const result = JSON.parse(obj); @@ -72,6 +78,7 @@ errorElement.style.opacity = 0; }, 3000); isLoggingIn = false; + turnstile.reset(); } }) }); @@ -101,161 +108,179 @@ notificationDiv.remove(); }, 4900); } + document.getElementById("cf-turnstile").addEventListener("expired-callback", function () { + createNotification("Please complete the captcha."); + turnstile.reset(); + }); + + \ No newline at end of file diff --git a/PasteBin/assets/signup.html b/PasteBin/assets/signup.html index 6657b13..f95e8b9 100644 --- a/PasteBin/assets/signup.html +++ b/PasteBin/assets/signup.html @@ -5,7 +5,8 @@ - create paste + + sign-up @@ -25,6 +26,7 @@ +

@@ -41,6 +43,7 @@ var usernameContent = document.getElementById("usernameInput").value; var passwordContent = document.getElementById("passwordInput").value; + var cf = turnstile.getResponse(); if (isLoggingIn) { createNotification("Please wait for the login to finish."); @@ -56,7 +59,8 @@ }, body: JSON.stringify({ username: usernameContent, - password: passwordContent + password: passwordContent, + cf: cf }) }) .then(response => response.json()) @@ -75,6 +79,7 @@ errorElement.style.opacity = 0; }, 3000); isLoggingIn = false; + } }) }); diff --git a/PasteBin/assets/user.html b/PasteBin/assets/user.html index f93789f..56031bf 100644 --- a/PasteBin/assets/user.html +++ b/PasteBin/assets/user.html @@ -174,7 +174,7 @@ flex-direction: column; align-items: center; width: fit-content; - width: 85%; + width: calc(100% - 52px); max-width: 100%; background-color: #222222; border-radius: 5px;