Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion checks/pages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ PageEntry E(string name, string project, string agent, string? session, int crea
var lib = Path.Combine(dir, "lib");
Directory.CreateDirectory(lib);
File.WriteAllText(Path.Combine(lib, "clayo-page.css"), ".a { color: red } /* $1 </style> */");
File.WriteAllText(Path.Combine(lib, "clayo-page.js"), "var page = '$0 </script>';");
File.WriteAllText(Path.Combine(lib, "clayo-page.js"), "var page = '$0 </script>'; var c = '<!--';");
File.WriteAllText(Path.Combine(lib, "chart.umd.min.js"), "var CHART;");
File.WriteAllText(Path.Combine(lib, "mermaid.min.js"), "var MERMAID;");
const string Cdn = "https://cdn.jsdelivr.net/npm/d3@7.9.0/dist/d3.min.js";
Expand All @@ -256,6 +256,7 @@ string Bundle(string body) => PageBundle.Make(
Check("copy: sealed", plainCopy.Contains("Content-Security-Policy"), true);
Check(" the stylesheet inside, then the theme", plainCopy.IndexOf(".a { color: red }") is > 0 and var at && at < plainCopy.IndexOf("--accent: blue"), true);
Check(" the script inside", plainCopy.Contains("var page = '$0 <\\/script>';"), true);
Check(" an HTML comment opener in it is escaped", plainCopy.Contains("var c = '<\\!--';"), true);
Check(" nothing linked from Clayo", plainCopy.Contains("ccx.assets/pages/clayo-page"), false);
Check(" a closing tag in a file can't end its tag early", plainCopy.Contains("/* $1 <\\/style> */"), true);
Check(" no charts: no Chart.js, no Mermaid", (plainCopy.Contains("CHART"), plainCopy.Contains("MERMAID")), (false, false));
Expand All @@ -267,6 +268,59 @@ string Bundle(string body) => PageBundle.Make(
Check("a CDN script it can get: inside", cdnCopy.Contains("<script>var D3;</script>") && !cdnCopy.Contains(Cdn), true);
Check(" one it can't: its link kept", cdnCopy.Contains("src=\"https://unpkg.com/x@1.0.0/x.js\""), true);
Check(" another host: never fetched", fetched.Any(u => u.Contains("example.com")), false);

// Before a page leaves the PC (P7, D16): each kind is found, on its line.
string Found(string text) => string.Join("|", SecretScan.Find(text).Select(f => $"{f.Line}:{f.What}"));
Check("scan: a GitHub token", Found("<p>\nghp_" + new string('a', 36)), "2:a GitHub token");
Check(" a fine-grained one", Found("github_pat_" + new string('B', 30)), "1:a GitHub token");
Check(" an sk- key", Found("key sk-proj-" + new string('x', 30)), "1:an API key (sk-)");
Check(" an AWS key", Found("AKIAIOSFODNN7EXAMPLE"), "1:an AWS key");
Check(" a Slack token", Found("xoxb-1234567890-abcdef"), "1:a Slack token");
Check(" a private key", Found("-----BEGIN OPENSSH PRIVATE KEY-----"), "1:a private key");
Check(" KEY=value", Found("DB_PASSWORD=hunter2hunter2"), "1:a KEY=value secret");
Check(" a Windows path", Found(@"see C:\Users\yogi\notes.txt"), "1:a local path");
Check(" a Mac path", Found("at /Users/yogi/x"), "1:a local path");
Check(" localhost and private hosts", Found("http://localhost:3000\n192.168.1.20\nbuild.corp"), "1:an internal host|2:an internal host|3:an internal host");
Check(" a path as JS writes it", Found(@"{""dir"":""C:\\Users\\yogi\\x""}"), "1:a local path");
Check(" with forward slashes", Found("C:/Users/yogi/x"), "1:a local path");
Check(" a network share", Found(@"\\fileserver\team"), "1:a local path");
Check(" a Stripe key", Found("sk_live_" + new string('a', 24)), "1:a Stripe key");
Check(" a Google key", Found("AIza" + new string('b', 35)), "1:a Google API key");
Check(" a JWT", Found("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.abcdefghijklmnop"), "1:a JWT");
Check(" a bearer header", Found("Authorization: Bearer abcdefghijklmnopqrstuvwxyz"), "1:a bearer token");
Check(" a password in a URL", Found("postgres://app:s3cretpw@db.example.com/x"), "1:a password in a URL");
var slow = string.Concat(Enumerable.Repeat("a.", 200_000));
var watch = System.Diagnostics.Stopwatch.StartNew();
var slowFound = Found(slow);
Check(" a line made to be slow: done fast, and flagged or clean", watch.Elapsed.TotalSeconds < 10 && slowFound is "" or "1:a line too long to check", true);
Check(" nothing in plain words", Found("A token of thanks. The secret is good docs. Users/home pages."), "");
Check(" a placeholder value is not a secret", Found("API_KEY=<your key>"), "");
foreach (var template in Directory.GetFiles(Path.Combine(AppContext.BaseDirectory, "Assets", "pages", "templates"), "*.html"))
Check($" no false hit on {Path.GetFileName(template)}", Found(File.ReadAllText(template)), "");

// Public links: kept per page, with the login said yes to; the Shared filter.
var shares = new PageShares(Path.Combine(dir, "shares.json"));
Check("shares: none yet", (shares.Login, shares.Get(mine)), ((string?)null, (PageShare?)null));
shares.Login = "gh";
const string Gist = "0123456789abcdef0123456789abcdef";
shares.Set(mine, new PageShare(Gist, "someone", "mine.html"));
var again = new PageShares(Path.Combine(dir, "shares.json"));
Check(" read back", (again.Login, again.Get(mine.ToUpperInvariant())?.GistId), ("gh", Gist));
Check(" its link", again.Get(mine)!.Url, $"https://gist.githack.com/someone/{Gist}/raw/mine.html");
again.Set(outside, new PageShare("../repos/me/proj/git/refs/heads/main", "someone", "x.html"));
Check(" an id that isn't a gist's is never read back", again.Get(outside), (PageShare?)null);
Check(" nor sent", GitHubGist.IsGistId("../repos/x"), false);
again.Set(mine, null);
Check(" removed, the login kept", (again.Get(mine), again.Login), ((PageShare?)null, "gh"));
var pathDir = Path.Combine(dir, "bin");
Directory.CreateDirectory(pathDir);
File.WriteAllText(Path.Combine(pathDir, "gh.exe"), "");
Check("tools: found on PATH by full path", GitHubGist.OnPath("gh", $".;bin;{pathDir}"), Path.Combine(pathDir, "gh.exe"));
Check(" never in a relative folder", GitHubGist.OnPath("gh", $".;bin"), (string?)null);
File.WriteAllText(Path.Combine(dir, "shares.json"), "{\"login\":5,\"pages\":[{\"page\":1,\"gist\":\"" + Gist + "\",\"owner\":\"o\",\"file\":\"f\"}]}");
Check(" odd types in the file read as nothing", (new PageShares(Path.Combine(dir, "shares.json")).Login, new PageShares(Path.Combine(dir, "shares.json")).All().Count), ((string?)null, 0));
Check(" a broken entry is skipped", new PageShares(Path.Combine(dir, "shares.json")).All().Count, 0);
Check("filter: shared only", string.Join(",", PageIndex.Filter([fixture[0], fixture[1] with { Shared = true }], new(Shared: true)).Select(p => p.Title)), "alpha");
}
finally { Directory.Delete(dir, recursive: true); }

Expand Down
2 changes: 1 addition & 1 deletion src/Clayo/Assets/terminal.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<span id="mdname"></span>
<button id="pagepresent" title="Full screen. Arrow keys move between slides, Esc leaves.">▶ Present</button>
<button id="pageexport" title="Save it as a PDF or a PNG">Export</button>
<button id="pageshare" title="Save a copy that opens anywhere, offline">Share</button>
<button id="pageshare" title="Save a copy, or make a public link">Share</button>
<button id="mdsplit" title="Half and half">◧ Split</button>
<button id="mdfull" title="Fill the pane">⛶ Full</button>
<button id="mdclose" title="Back to the terminal">✕</button>
Expand Down
158 changes: 158 additions & 0 deletions src/Clayo/Pages/GitHubGist.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;

namespace CcxShell.Core;

/// <summary>A GitHub login found on this PC (D15): where it came from, and its token.</summary>
public sealed record GitHubLogin(string Source, string Token);

/// <summary>GitHub said no. Status is the HTTP status: 401/403 a login that can't, 404 a gist gone.</summary>
public sealed class GitHubException(HttpStatusCode status, string message) : Exception(message)
{
public HttpStatusCode Status { get; } = status;
}

/// <summary>
/// A public link to a page (D14): a secret gist with a githack preview. The login is one the
/// user already has, from gh or git's credential manager; Clayo keeps no token. No WPF.
/// </summary>
public static partial class GitHubGist
{
[GeneratedRegex("^[0-9a-f]{20,32}$")]
private static partial Regex GistIdShape();

/// <summary>
/// Whether an id has a gist id's shape. One read back from shares.json goes into an API path,
/// so "../repos/…" must never get there.
/// </summary>
public static bool IsGistId(string id) => GistIdShape().IsMatch(id);

private static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(60) };

/// <summary>The link that shows the gist's file as a page. It follows the gist as it changes.</summary>
public static string PreviewUrl(string owner, string id, string file) =>
$"https://gist.githack.com/{owner}/{id}/raw/{Uri.EscapeDataString(file)}";

/// <summary>gh's token, else git's saved one for github.com, never a prompt; null when neither has one.</summary>
public static async Task<GitHubLogin?> FindLogin()
{
// github.com's own token: gh's default host may be a GitHub Enterprise one.
if (await Run("gh", "auth token --hostname github.com", null) is { Length: > 0 } gh) return new("gh", gh.Trim());
var fill = await Run("git", "credential fill", "protocol=https\nhost=github.com\n\n");
var token = fill?.Split('\n').FirstOrDefault(l => l.StartsWith("password="))?["password=".Length..].Trim();
return token is { Length: > 0 } ? new("git", token) : null;
}

/// <summary>The account a token belongs to.</summary>
public static async Task<string> User(string token) =>
(string)(await Send(HttpMethod.Get, "user", token, null))!["login"]!;

/// <summary>A new secret gist holding the page; its id and owner.</summary>
public static async Task<(string Id, string Owner)> Create(string token, string file, string html)
{
var body = new JsonObject
{
["description"] = "A page made with Clayo",
["public"] = false,
["files"] = new JsonObject { [file] = new JsonObject { ["content"] = html } }
};
var gist = (await Send(HttpMethod.Post, "gists", token, body))!;
return ((string)gist["id"]!, (string)gist["owner"]!["login"]!);
}

/// <summary>
/// The gist's file replaced with the page as it is now; the link stays. GitHub keeps the
/// earlier versions in the gist's history, readable by anyone with the link.
/// </summary>
public static Task Update(string token, string id, string file, string html) =>
Send(HttpMethod.Patch, $"gists/{Checked(id)}", token,
new JsonObject { ["files"] = new JsonObject { [file] = new JsonObject { ["content"] = html } } });

/// <summary>
/// Deletes the gist. One already gone is fine: call it only with the login of the gist's
/// owner (User), as GitHub also says "not found" for a gist that is someone else's.
/// </summary>
public static async Task Delete(string token, string id)
{
try { await Send(HttpMethod.Delete, $"gists/{Checked(id)}", token, null); }
catch (GitHubException e) when (e.Status == HttpStatusCode.NotFound) { }
}

private static string Checked(string id) =>
IsGistId(id) ? id : throw new GitHubException(HttpStatusCode.BadRequest, "not a gist id");

private static readonly Uri Api = new("https://api.github.com/");

private static async Task<JsonNode?> Send(HttpMethod method, string path, string token, JsonObject? body)
{
using var request = new HttpRequestMessage(method, new Uri(Api, path));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
request.Headers.Accept.ParseAdd("application/vnd.github+json");
request.Headers.UserAgent.ParseAdd("Clayo");
if (body is not null) request.Content = new StringContent(body.ToJsonString(), Encoding.UTF8, "application/json");
using var response = await Http.SendAsync(request);
var text = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
string? message = null;
try { message = (string?)JsonNode.Parse(text)?["message"]; }
catch (System.Text.Json.JsonException) { }
throw new GitHubException(response.StatusCode, message ?? response.ReasonPhrase ?? response.StatusCode.ToString());
}
return text.Length > 0 ? JsonNode.Parse(text) : null;
}

/// <summary>
/// A tool's full path from PATH's absolute folders only. Started by bare name, Windows would
/// look in the current folder first, which is often the project: a repo could hold its own
/// gh.exe.
/// </summary>
public static string? OnPath(string exe, string? path = null)
{
foreach (var dir in (path ?? Environment.GetEnvironmentVariable("PATH") ?? "").Split(Path.PathSeparator))
{
var folder = dir.Trim().Trim('"');
if (folder.Length == 0 || !Path.IsPathFullyQualified(folder)) continue;
var file = Path.Combine(folder, exe + ".exe");
if (File.Exists(file)) return file;
}
return null;
}

// A tool's output, or null when it isn't there, fails or takes too long. Never a window or
// prompt. Run from Clayo's own folder, so git reads no repo's config (a credential.helper
// there could run anything).
private static async Task<string?> Run(string exe, string args, string? input)
{
if (OnPath(exe) is not { } file) return null;
var home = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Clayo");
var start = new ProcessStartInfo(file, args)
{
RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true,
UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = home
};
start.Environment["GCM_INTERACTIVE"] = "never";
start.Environment["GIT_TERMINAL_PROMPT"] = "0";
start.Environment["GH_PROMPT_DISABLED"] = "1";
try
{
Directory.CreateDirectory(home);
using var p = Process.Start(start)!;
if (input is not null) await p.StandardInput.WriteAsync(input);
p.StandardInput.Close();
var output = p.StandardOutput.ReadToEndAsync();
_ = p.StandardError.ReadToEndAsync();
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
try { await p.WaitForExitAsync(timeout.Token); }
catch (OperationCanceledException) { p.Kill(entireProcessTree: true); return null; }
return p.ExitCode == 0 ? await output : null;
}
catch (Exception e) when (e is System.ComponentModel.Win32Exception or InvalidOperationException or IOException or UnauthorizedAccessException) { return null; }
}
}
10 changes: 7 additions & 3 deletions src/Clayo/Pages/PageBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ public static async Task<string> Make(string html, string themeCss, string asset
return html;
}

// Inside the tag, "</script" or "</style" would end it early.
private static string Script(string js) => $"<script>{js.Replace("</script", "<\\/script", StringComparison.OrdinalIgnoreCase)}</script>";
// Inside the tag, "</script" or "</style" would end it early, and "<!--" with a "<script"
// after it would swallow the markup that follows. Both escapes mean the same in JS strings
// and regexes.
private static string Script(string js) =>
$"<script>{js.Replace("</script", "<\\/script", StringComparison.OrdinalIgnoreCase).Replace("<!--", "<\\!--")}</script>";
private static string Style(string css) => $"<style>{css.Replace("</style", "<\\/style", StringComparison.OrdinalIgnoreCase)}</style>";

private static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(20) };
// Mermaid, the largest library a page might link, is about 3 MB.
private static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(20), MaxResponseContentBufferSize = 16 * 1024 * 1024 };

/// <summary>A CDN file from the cache, else fetched and kept there; null when it can't be had.</summary>
public static async Task<string?> FetchCached(string url)
Expand Down
10 changes: 6 additions & 4 deletions src/Clayo/Pages/PageIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ namespace CcxShell.Core;
/// <summary>
/// One page an agent wrote, as the index keeps it. Times are UTC. SessionId is null and Agent
/// empty for a page found in a project's folder that no session was seen writing. Deck is
/// read from the file, not kept.
/// read from the file and Shared from PageShares, not kept here.
/// </summary>
public sealed record PageEntry(
string Path, string Title, string Project, string? SessionId, string Agent, DateTime Created, DateTime Changed, bool Deck = false);
string Path, string Title, string Project, string? SessionId, string Agent, DateTime Created, DateTime Changed,
bool Deck = false, bool Shared = false);

public enum PageSort { Newest, Changed, Name, Project }

/// <summary>What the Pages view shows: a search of titles, the filters (null: any) and the order.</summary>
public sealed record PageFilter(
string Search = "", string? Project = null, string? Agent = null, string? SessionId = null, bool Slides = false,
PageSort Sort = PageSort.Newest);
PageSort Sort = PageSort.Newest, bool Shared = false);

/// <summary>
/// Every page from every session, in pages.json, so the Pages list can show them with no pane
Expand Down Expand Up @@ -115,7 +116,8 @@ public static IReadOnlyList<PageEntry> Filter(IEnumerable<PageEntry> pages, Page
&& (f.Project is null || Same(p.Project, f.Project))
&& (f.Agent is null || p.Agent == f.Agent)
&& (f.SessionId is null || p.SessionId == f.SessionId)
&& (!f.Slides || p.Deck));
&& (!f.Slides || p.Deck)
&& (!f.Shared || p.Shared));
return f.Sort switch
{
PageSort.Changed => [.. shown.OrderByDescending(p => p.Changed)],
Expand Down
Loading