{"record":{"id":"2f6a256461c4b0d6","repo":"iOfficeAI/OfficeCLI","slug":"invalid-path-path-trailing-is-not-allowed","errorCode":null,"errorMessage":"Invalid path '{path}': trailing '/' is not allowed.","messagePattern":"Invalid path '(.+?)': trailing '/' is not allowed\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"warning","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs","lineNumber":107,"sourceCode":"    /// Sheet1!A:A → /Sheet1/col[A]   (whole column)\n    /// Paths already starting with '/' are returned unchanged.\n    /// </summary>\n    // Excel-quoted sheet name: 'My Data (2024)'!A1 — strip one pair of single\n    // quotes and un-double embedded quotes ('It''s'!A1 → It's). Excel REQUIRES\n    // the quoted form for names with spaces/punctuation, so refs pasted from\n    // formulas arrive quoted. Unquoted names pass through unchanged.\n    internal static string UnquoteSheetName(string s)\n        => s.Length >= 2 && s[0] == '\\'' && s[^1] == '\\''\n            ? s[1..^1].Replace(\"''\", \"'\")\n            : s;\n\n    internal string NormalizeExcelPath(string path)\n    {\n        // Reject malformed segment separators that previously slipped past\n        // the regex matchers and exposed raw OOXML local names. DOCX already\n        // rejects these; bring XLSX up to parity.\n        if (path.Length > 1 && path != \"/\" && path.EndsWith(\"/\"))\n            throw new ArgumentException($\"Invalid path '{path}': trailing '/' is not allowed.\");\n        if (path.StartsWith(\"//\"))\n            throw new ArgumentException($\"Invalid path '{path}': leading '//' is not allowed.\");\n        if (path.Contains(\"//\"))\n            throw new ArgumentException($\"Invalid path '{path}': empty path segment ('//') is not allowed.\");\n        // Handle \"/Sheet1!A1\" — strip leading '/' when '!' is present so native\n        // notation is parsed correctly. EXCEPT when the first slash segment\n        // names an existing sheet: '!' is legal inside a sheet NAME (Excel\n        // forbids only : \\ / ? * [ ]), and reinterpreting \"/Q1!Results\" as\n        // bang notation made such a sheet permanently unaddressable.\n        if (path.StartsWith('/') && path.Contains('!'))\n        {\n            var seg0 = path[1..];\n            var seg0Slash = seg0.IndexOf('/');\n            var first = seg0Slash < 0 ? seg0 : seg0[..seg0Slash];\n            if (!GetWorksheets().Any(w => w.Name.Equals(first, StringComparison.OrdinalIgnoreCase)))\n                path = path[1..];\n        }\n        if (path.Equals(\"/workbook\", StringComparison.OrdinalIgnoreCase)) return \"/\";","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs#L89-L125","documentation":"Thrown by NormalizeExcelPath when the path has length > 1, is not the single-character '/', and ends with '/'. Excel paths must not have a trailing slash — it implies an empty final segment and previously exposed raw OOXML local names.","triggerScenarios":"Calling any path-based Excel API with '/Sheet1/', '/Sheet1/A1/', '/Sheet1/table[1]/', or any path whose last char is '/'. The root '/' alone is allowed.","commonSituations":"Concatenating a trailing '/' in URL-style path building; normalizing paths with a generic trim that adds a trailing separator; user input from a CLI that accepts a trailing slash.","solutions":["Strip a single trailing '/' from non-root paths before sending (see validationCode).","Use the documented path shape: leading '/', segments separated by '/', no trailing '/' (except root '/').","For root, send exactly '/'."],"exampleFix":"// before\nstring path = \"/Sheet1/A1/\";\n// after\nstring path = \"/Sheet1/A1\";\nif (path.Length > 1 && path.EndsWith('/')) path = path[..^1];","handlingStrategy":"validation","validationCode":"static string NormalizePath(string p)\n{\n    if (string.IsNullOrEmpty(p)) return p;\n    while (p.Length > 1 && p != \"/\" && p.EndsWith('/')) p = p[..^1];\n    return p;\n}","typeGuard":"null","tryCatchPattern":"try { NormalizeExcelPath(path); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"trailing '/'\"))\n{ path = path.TrimEnd('/'); if (path.Length == 0) path = \"/\"; }","preventionTips":["Never append a trailing '/' to an Excel node path — only root uses '/'.","Sanitize user-supplied paths at the input boundary.","Differentiate root '/' from '/Sheet/' which is invalid."],"tags":["paths","excel","input-validation","normalization"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}