diff --git a/OPTIONS.md b/OPTIONS.md index e7e68b138..38b48fdbe 100644 --- a/OPTIONS.md +++ b/OPTIONS.md @@ -279,6 +279,13 @@ These keys are also stored in `QuickLook.config`. - Example: - `False` to disable word wrap. +### `` +- Default: `True` +- Type: `Boolean` +- Description: Mask the values of `.env` files (`.env`, `.env.*`, `.envrc` and `*.env`) as `********` in text previews, so secrets are not exposed during screen shares. Click the eye button or press `Alt+M` while previewing to show or hide the values; masking is always on when a file is opened. +- Example: + - `False` to always show `.env` values. + ### `` - Default: `True` - Type: `Boolean` diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Generators/EnvMaskGenerator.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Generators/EnvMaskGenerator.cs new file mode 100644 index 000000000..b3136e759 --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Generators/EnvMaskGenerator.cs @@ -0,0 +1,453 @@ +// Copyright © 2017-2026 QL-Win Contributors +// +// This file is part of QuickLook program. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the 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 +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using ICSharpCode.AvalonEdit.Document; +using ICSharpCode.AvalonEdit.Rendering; +using QuickLook.Common.Helpers; +using System; +using System.Collections.Generic; +using System.IO; + +namespace QuickLook.Plugin.TextViewer.Generators; + +/// +/// "Privacy mode" for .env files: replaces the value of every KEY=value line with a fixed-width +/// mask on screen, so secrets are not exposed during screen shares or presentations. +/// The document itself is not modified, and the mask has a constant width so it does not reveal +/// how long a value is. +/// +/// +/// The generator must be built from the decoded text BEFORE over-long lines are truncated. +/// Truncation can cut off the closing quote of a value, and a value whose end is not visible +/// cannot be masked reliably. Masked ranges are stored as (line, column) instead of offsets: +/// truncation only shortens the end of lines and keeps every line break, so line and column +/// stay valid, and a range is clipped to the real line length when it is drawn. +/// +internal sealed class EnvMaskGenerator : VisualLineElementGenerator +{ + private const string Mask = "********"; + + private static readonly char[] LineBreaks = ['\r', '\n']; + + // One entry per masked line, sorted by line number. A line has at most one masked range, and a + // quoted value that spans several lines becomes one range per line. + // Line numbers are 1-based like DocumentLine.LineNumber, columns are 0-based [start, end). + private readonly int[] _lines; + private readonly int[] _startColumns; + private readonly int[] _endColumns; + + /// When false the real values are shown. + public bool IsMasked { get; set; } = true; + + /// The decoded file text, before any line truncation. + public EnvMaskGenerator(string text) + { + var lines = new List(); + var starts = new List(); + var ends = new List(); + FindValueRanges(text ?? string.Empty, lines, starts, ends); + + _lines = lines.ToArray(); + _startColumns = starts.ToArray(); + _endColumns = ends.ToArray(); + } + + /// + /// .env, .env.local, .env.production, .envrc, something.env ... + /// + public static bool IsEnvFile(string path) + { + if (string.IsNullOrEmpty(path)) + return false; + + string name = Path.GetFileName(path); + + return name.Equals(".env", StringComparison.OrdinalIgnoreCase) + || name.Equals(".envrc", StringComparison.OrdinalIgnoreCase) + || name.StartsWith(".env.", StringComparison.OrdinalIgnoreCase) + || name.EndsWith(".env", StringComparison.OrdinalIgnoreCase); + } + + /// + /// Masking applies to .env files unless the user switched it off with the + /// "MaskEnvValues" setting of the text viewer. + /// + public static bool IsEnabledFor(string path) + { + return IsEnvFile(path) && SettingHelper.Get("MaskEnvValues", true, "QuickLook.Plugin.TextViewer"); + } + + public override int GetFirstInterestedOffset(int startOffset) + { + if (!IsMasked || _lines.Length == 0) + return -1; + + // A visual line normally is one document line, but can be several when lines are folded. + int limit = CurrentContext.VisualLine.LastDocumentLine.EndOffset; + + for (DocumentLine line = CurrentContext.Document.GetLineByOffset(startOffset); + line != null && line.Offset <= limit; + line = line.NextLine) + { + if (TryGetRange(line, out int start, out int end) && end > startOffset) + return Math.Max(start, startOffset); + } + + return -1; + } + + public override VisualLineElement ConstructElement(int offset) + { + DocumentLine line = CurrentContext.Document.GetLineByOffset(offset); + + if (!TryGetRange(line, out int start, out int end) || offset < start || offset >= end) + return null; + + return new FormattedTextElement(Mask, end - offset); + } + + /// + /// Document offsets of the masked range on a line, clipped to the text that is really there + /// (a truncated line is shorter than the line the range was found on). + /// + private bool TryGetRange(DocumentLine line, out int start, out int end) + { + start = end = 0; + + int index = Array.BinarySearch(_lines, line.LineNumber); + if (index < 0) + return false; + + start = line.Offset + _startColumns[index]; + end = line.Offset + Math.Min(_endColumns[index], line.Length); + return end > start; + } + + private static void FindValueRanges(string text, List lines, List starts, List ends) + { + int position = 0; + int lineNumber = 1; + + while (position < text.Length) + { + int lineEnd = IndexOfLineBreak(text, position); + int nextLine = SkipLineBreak(text, lineEnd); + + // A UTF-8 BOM is decoded to U+FEFF. It stays part of the text (so columns match the + // document) but is not part of the first key. + int p = position == 0 && text[0] == '\uFEFF' ? 1 : position; + p = SkipBlanks(text, p, lineEnd); + + // Blank lines and comments have nothing to mask. + if (p < lineEnd && text[p] != '#') + { + p = SkipExport(text, p, lineEnd); + + int keyStart = p; + while (p < lineEnd && IsKeyChar(text[p])) + p++; + + if (p > keyStart) + { + p = SkipBlanks(text, p, lineEnd); + + if (p < lineEnd && text[p] == '=') + { + int valueStart = SkipBlanks(text, p + 1, lineEnd); + + // KEY= (empty value): nothing to hide. + if (valueStart < lineEnd) + { + int valueEnd = ScanValue(text, valueStart); + AddRanges(text, valueStart, valueEnd, lineNumber, position, lines, starts, ends); + + // Resume after the line that holds the end of the value. + nextLine = SkipLineBreak(text, IndexOfLineBreak(text, valueEnd)); + } + } + } + } + + lineNumber += CountLineBreaks(text, position, nextLine); + position = nextLine; + } + } + + /// + /// Returns the end (exclusive) of the value that starts at . + /// A value is a sequence of quoted and unquoted segments, like a shell word (see "Quoting" in + /// the POSIX Shell Command Language): "prefix"secret is one value, and a backslash + /// escapes the next character, so an escaped space does not end the value or start a comment, and + /// an escaped line break continues the value on the next line. + /// It ends at the end of the line, or at unescaped whitespace followed by '#' (a comment). + /// To be safe, anything that may belong to the value is masked. + /// + private static int ScanValue(string text, int start) + { + int i = start; + int end = start; + + // True at the start of the value and right after a closing quote. + bool afterQuote = true; + + // End of the line that holds the scan position, and the start of the next assignment after + // it. Both are found on first use and reused while the scan stays on that line, so a line + // with many quotes is not rescanned for each of them. + int lineEnd = -1; + int nextAssignment = -1; + + while (i < text.Length) + { + char c = text[i]; + + if (c == '\r' || c == '\n') + break; + + if (c == '"' || c == '\'' || c == '`') + { + // A quoted segment may continue over several lines (private keys, certificates...), + // also after an unquoted prefix: TOKEN=prefix"first\nsecret". In the middle of a + // word, though, a quote is usually just a character (NAME=O'Brien), so it must not + // pair with a quote that belongs to the next assignment: the search stops there. + int limit = text.Length; + + if (!afterQuote) + { + if (i > lineEnd) + { + lineEnd = IndexOfLineBreak(text, i); + nextAssignment = -1; + } + + if (nextAssignment < 0) + nextAssignment = IndexOfNextAssignment(text, lineEnd); + + limit = nextAssignment; + } + + int close = FindClosingQuote(text, i + 1, c, limit); + + if (close >= 0) + { + i = close + 1; + end = i; + afterQuote = true; + continue; + } + + // Never closed. At the start of a value hide the rest of the line, otherwise + // treat the quote as an ordinary character. + if (afterQuote) + return IndexOfLineBreak(text, i); + + i++; + end = i; + continue; + } + + afterQuote = false; + + if (c == '\\') + { + // The escaped character (even a space) belongs to the value. + // A backslash before a line break continues the value on the next line. + if (i + 1 < text.Length && text[i + 1] == '\r' && i + 2 < text.Length && text[i + 2] == '\n') + i += 3; + else + i += i + 1 < text.Length ? 2 : 1; + + end = i; + } + else if (c == ' ' || c == '\t') + { + int next = SkipBlanks(text, i, text.Length); + + // Unescaped whitespace followed by '#' starts a comment. + if (next < text.Length && text[next] == '#') + break; + + // Whitespace inside the value; it only counts when more of the value follows. + i = next; + } + else + { + i++; + end = i; + } + } + + return end; + } + + /// + /// Records [start, end) split at line breaks, so that no range crosses a line. + /// and belong to the line that holds . + /// + private static void AddRanges(string text, int start, int end, int lineNumber, int lineStart, + List lines, List starts, List ends) + { + int segment = start; + + for (int i = start; i < end; i++) + { + char c = text[i]; + if (c != '\r' && c != '\n') + continue; + + if (i > segment) + { + lines.Add(lineNumber); + starts.Add(segment - lineStart); + ends.Add(i - lineStart); + } + + if (c == '\r' && i + 1 < end && text[i + 1] == '\n') + i++; + + lineNumber++; + lineStart = i + 1; + segment = lineStart; + } + + if (end > segment) + { + lines.Add(lineNumber); + starts.Add(segment - lineStart); + ends.Add(end - lineStart); + } + } + + /// Start of the first line after the line that ends at and begins a KEY=value assignment, or the end of the text. + private static int IndexOfNextAssignment(string text, int currentLineEnd) + { + int position = SkipLineBreak(text, currentLineEnd); + + while (position < text.Length) + { + int lineEnd = IndexOfLineBreak(text, position); + int p = SkipBlanks(text, position, lineEnd); + + if (p < lineEnd && text[p] != '#') + { + p = SkipExport(text, p, lineEnd); + + int keyStart = p; + while (p < lineEnd && IsKeyChar(text[p])) + p++; + + if (p > keyStart) + { + p = SkipBlanks(text, p, lineEnd); + + if (p < lineEnd && text[p] == '=') + return position; + } + } + + position = SkipLineBreak(text, lineEnd); + } + + return text.Length; + } + + /// Index of the closing quote before , or -1. + private static int FindClosingQuote(string text, int from, char quote, int limit) + { + for (int i = from; i < limit; i++) + { + // Double quoted values may contain \" escapes. + if (quote == '"' && text[i] == '\\') + { + i++; + continue; + } + + if (text[i] == quote) + return i; + } + + return -1; + } + + /// Number of line breaks in [from, to); "\r\n" counts once, like in the editor. + private static int CountLineBreaks(string text, int from, int to) + { + int count = 0; + + for (int i = from; i < to; i++) + { + if (text[i] == '\r') + { + if (i + 1 < to && text[i + 1] == '\n') + i++; + + count++; + } + else if (text[i] == '\n') + { + count++; + } + } + + return count; + } + + private static int IndexOfLineBreak(string text, int from) + { + int index = text.IndexOfAny(LineBreaks, from); + return index < 0 ? text.Length : index; + } + + private static int SkipLineBreak(string text, int index) + { + if (index >= text.Length) + return text.Length; + + if (text[index] == '\r' && index + 1 < text.Length && text[index + 1] == '\n') + return index + 2; + + return index + 1; + } + + private static int SkipBlanks(string text, int from, int limit) + { + while (from < limit && (text[from] == ' ' || text[from] == '\t')) + from++; + + return from; + } + + /// Skips a leading "export " so that "export KEY=value" (.envrc, shell style) works too. + private static int SkipExport(string text, int p, int lineEnd) + { + const string keyword = "export"; + + if (p + keyword.Length < lineEnd + && string.CompareOrdinal(text, p, keyword, 0, keyword.Length) == 0 + && (text[p + keyword.Length] == ' ' || text[p + keyword.Length] == '\t')) + { + return SkipBlanks(text, p + keyword.Length, lineEnd); + } + + return p; + } + + private static bool IsKeyChar(char c) + { + return char.IsLetterOrDigit(c) || c == '_' || c == '.' || c == '-'; + } +} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs index 92bc33737..12c115c5d 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs @@ -93,7 +93,9 @@ public void View(string path, ContextObject context) { _tvp = new TextViewerPanel(); _tvp.LoadFileAsync(path, context); - context.ViewerContent = _tvp; + + // For .env files this adds an eye button (show / hide values) in the top right corner. + context.ViewerContent = _tvp.WrapWithEnvToggle(); } context.Title = Path.GetFileName(path); } diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.DevTools.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.DevTools.cs new file mode 100644 index 000000000..395a5a66d --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.DevTools.cs @@ -0,0 +1,153 @@ +// Copyright © 2017-2026 QL-Win Contributors +// +// This file is part of QuickLook program. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the 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 +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using QuickLook.Common.Controls; +using QuickLook.Common.Helpers; +using QuickLook.Plugin.TextViewer.Generators; +using System; +using System.IO; +using System.Reflection; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; + +namespace QuickLook.Plugin.TextViewer; + +// Privacy mode for .env files: values are masked until the user reveals them, +// with an eye button in the top right corner (and Alt+M) to show / hide them. +public partial class TextViewerPanel +{ + private bool _envMaskAvailable; + private bool _envRevealed; + private EnvMaskGenerator _envMask; + + /// Raised after the user hides or reveals the values of an .env file. + public event EventHandler EnvMaskVisibilityChanged; + + /// True when the file being previewed is an .env file and masking is enabled. + public bool IsEnvMaskAvailable => _envMaskAvailable; + + /// True while the real values of an .env file are shown. Always false after opening a file. + public bool AreEnvValuesRevealed => _envRevealed; + + /// + /// Decides synchronously, before the file is read, whether masking applies, + /// so that the toolbar button can be created right after the preview starts. + /// + private void PrepareDeveloperFeatures(string path) + { + _envMaskAvailable = EnvMaskGenerator.IsEnabledFor(path); + } + + /// + /// Installs the mask generator. is the decoded text before long lines are truncated. Called before the document is assigned to the editor, + /// so a secret is never drawn unmasked, not even for a single frame. + /// + private void InstallDeveloperFeatures(string path, string fullText) + { + if (_envMaskAvailable) + { + _envMask = new EnvMaskGenerator(fullText) { IsMasked = !_envRevealed }; + TextArea.TextView.ElementGenerators.Add(_envMask); + } + } + + /// Hides or reveals the values of an .env file. Does nothing for other files. + public void ToggleEnvMask() + { + if (!_envMaskAvailable) + return; + + _envRevealed = !_envRevealed; + + // The generator may not exist yet while the file is still loading; + // it picks up _envRevealed when it is installed. + if (_envMask is not null) + { + _envMask.IsMasked = !_envRevealed; + TextArea.TextView.Redraw(); + } + + EnvMaskVisibilityChanged?.Invoke(this, EventArgs.Empty); + } + + /// + /// Returns the element to show in the viewer window. For .env files this is the editor with an + /// eye button in its top right corner (like the buttons of the image viewer); for every other + /// file it is just the editor itself. Call it after . + /// + public FrameworkElement WrapWithEnvToggle() + { + if (!_envMaskAvailable) + return this; + + var button = new Button + { + Width = 24, + Height = 24, + FontFamily = new FontFamily("Segoe Fluent Icons, Segoe MDL2 Assets"), + Focusable = false, + }; + + // Same look as the caption buttons of the viewer window. The style is looked up when the + // button is added to the window, so it follows the current light / dark theme. + button.SetResourceReference(FrameworkElement.StyleProperty, "CaptionButtonStyle"); + + // Local values win over the style, so size and focus behavior stay as set above. + button.Width = 24; + button.Height = 24; + + button.Click += (_, _) => ToggleEnvMask(); + EnvMaskVisibilityChanged += (_, _) => UpdateEnvToggleButton(button); + UpdateEnvToggleButton(button); + + var overlay = new StackPanel + { + Orientation = Orientation.Horizontal, + HorizontalAlignment = HorizontalAlignment.Right, + VerticalAlignment = VerticalAlignment.Top, + + // Leaves room for the vertical scroll bar of the editor. + Margin = new Thickness(0, 8, 24, 0), + }; + overlay.Children.Add(button); + + var host = new Grid(); + host.Children.Add(this); + host.Children.Add(overlay); + return host; + } + + /// + /// Shows an eye while values are hidden (click to reveal) and a crossed-out eye while they are + /// visible (click to hide), with a matching tooltip. + /// + private void UpdateEnvToggleButton(Button button) + { + bool revealed = _envRevealed; + + button.Content = revealed ? FontSymbols.Hide : FontSymbols.RedEye; + + string translationFile = Path.Combine( + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Translations.config"); + + button.ToolTip = TranslationHelper.Get( + revealed ? "MW_HideSecretValues" : "MW_ShowSecretValues", + translationFile, + failsafe: revealed ? "Hide secret values" : "Show secret values"); + } +} diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.cs index 3a1ab57ce..31b40a82c 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.cs @@ -196,6 +196,11 @@ private void Viewer_KeyDown(object sender, KeyEventArgs e) TextArea.LeftMargins.OfType().FirstOrDefault()?.Stroke = Brushes.Transparent; SettingHelper.Set("ShowLineNumbers", ShowLineNumbers, "QuickLook.Plugin.TextViewer"); } + else if (Keyboard.IsKeyDown(Key.M)) + { + // Hide / reveal the values of an .env file (no effect on other files). + ToggleEnvMask(); + } } } @@ -330,6 +335,8 @@ protected override void ColorizeLine(DocumentLine line) public void LoadFileAsync(string path, ContextObject context) { + PrepareDeveloperFeatures(path); + _ = Task.Run(() => { const int maxLength = 5 * 1024 * 1024; @@ -370,6 +377,9 @@ public void LoadFileAsync(string path, ContextObject context) var encoding = EncodingDetector.DetectFromBytes(bufferCopy); var text = encoding.GetString(bufferCopy); + // .env masking needs the full lines: truncation can cut off a closing quote. + var fullText = text; + // Truncate overly long lines to prevent crashes and lag bool hasLongLines = TruncateLongLinesInText(ref text); @@ -388,6 +398,10 @@ public void LoadFileAsync(string path, ContextObject context) SyntaxHighlighting = bufferCopy.Length > maxHighlightingLength ? null : highlighting.SyntaxHighlighting; + + // .env masking; installed before the document is shown (uses the text before truncation). + InstallDeveloperFeatures(path, fullText); + Document = doc; if (SyntaxHighlighting is ICustomHighlightingDefinition custom) diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Translations.config b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Translations.config index 71bbc18ce..70acf9c42 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Translations.config +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Translations.config @@ -32,6 +32,8 @@ Reopen as HTML preview Reopen as image preview Reopen as CSV preview + Show secret values + Hide secret values Consolas