Skip to content

Sanitize .lnk/.ico filename leaf#1472

Open
shuaiyuanxx wants to merge 1 commit into
microsoft:mainfrom
shuaiyuanxx:fix/wslg-1009-sanitize-shortcut-filename
Open

Sanitize .lnk/.ico filename leaf#1472
shuaiyuanxx wants to merge 1 commit into
microsoft:mainfrom
shuaiyuanxx:fix/wslg-1009-sanitize-shortcut-filename

Conversation

@shuaiyuanxx

Copy link
Copy Markdown

Fixed: #1009

An app whose Name= contains a Windows-reserved filename character (e.g. "Code::Blocks") produced an illegal .lnk path; IPersistFile::Save failed and the error was swallowed, so the app was silently missing from the Start Menu with no diagnostic.

Add SanitizeFileName() and apply it to the filename leaves derived from appDesc (.lnk) and appId (.ico). appDesc is kept intact for SetDescription.

Design:

  • Reserved chars (\ / : * ? " < > |) and control chars map to '_'. This is predictable and renders in any font, unlike look-alike glyph substitution.
  • A legal name is left byte-for-byte unchanged (no suffix), so the common case is identical to today.
  • '_' substitution can map distinct names ("A:B" and "A/B") onto the same leaf; to avoid two apps' files overwriting each other, append a short stable FNV-1a hash of the app's unique id (~XXXXXXXX) whenever the name was altered. The hash is derived from appId, so it is unique per app and deterministic across syncs.

The FileDB key remains appGroup\appId, so add/delete/sync bookkeeping is unaffected. Trailing space/dot (illegal on Win32) are trimmed.

Copilot AI review requested due to automatic review settings June 22, 2026 07:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses start-menu shortcut/icon generation failures when app-provided names/IDs contain Windows-reserved filename characters (e.g., :), by sanitizing the .lnk and .ico filename leaf components while keeping the full app description intact for shortcut metadata.

Changes:

  • Introduces SanitizeFileName() to replace reserved/control characters, trim trailing space/dot, and (when modified) append a stable short hash to avoid collisions.
  • Applies sanitization to the .ico leaf derived from appId and the .lnk leaf derived from appDesc.
  • Updates inline documentation/comments around Start Menu link naming behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
WSLDVCPlugin/WSLDVCCallback.cpp Uses sanitized filename leaves when composing .ico and .lnk paths.
WSLDVCPlugin/utils.h Declares the new SanitizeFileName() helper with documentation.
WSLDVCPlugin/utils.cpp Implements SanitizeFileName() (reserved/control char mapping, trimming, collision-avoidance hash suffix).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread WSLDVCPlugin/WSLDVCCallback.cpp Outdated
Comment thread WSLDVCPlugin/utils.cpp
An app whose Name= contains a Windows-reserved filename character
(e.g. "Code::Blocks") produced an illegal .lnk path; IPersistFile::Save
failed and the error was swallowed, so the app was silently missing
from the Start Menu with no diagnostic.

Add SanitizeFileName() and apply it to the filename leaves derived
from appDesc (.lnk) and appId (.ico). appDesc is kept intact for
SetDescription. Design:

- Reserved chars (\ / : * ? " < > |) and control chars map to '_'.
  This is predictable and renders in any font, unlike look-alike
  glyph substitution.
- Reserved DOS device names (CON, PRN, AUX, NUL, COM1..9, LPT1..9) are
  illegal as a filename base even with an extension (e.g. "NUL.txt");
  Windows keys this on the token before the first '.', so prepend '_'
  to neutralize the base rather than relying on the hash suffix.
- A legal name is left byte-for-byte unchanged (no suffix), so the
  common case is identical to today.
- '_' substitution can map distinct names ("A:B" and "A/B") onto the
  same leaf; to avoid two apps' files overwriting each other, append a
  short stable FNV-1a hash of the app's unique id (~XXXXXXXX) whenever
  the name was altered. The hash is derived from appId, so it is unique
  per app and deterministic across syncs.

The FileDB key remains appGroup\appId, so add/delete/sync bookkeeping
is unaffected. Trailing space/dot (illegal on Win32) are trimmed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@shuaiyuanxx shuaiyuanxx force-pushed the fix/wslg-1009-sanitize-shortcut-filename branch from 5b1531a to 08132a3 Compare June 22, 2026 07:42
@chemwolf6922

Copy link
Copy Markdown

https://github.com/microsoft/WSL/blob/master/src/linux/init/escape.cpp
Please consider use the escaping method used here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Looks like WSLg doesn't create a start menu shortcut if : or :: is in the Name field of .desktop file

3 participants