|
| 1 | +// @index CCG reference parser for cross-namespace annotation links. |
| 2 | +package ccgref |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "path" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +const Scheme = "ccg" |
| 12 | + |
| 13 | +// Ref is the structured form of a ccg:// annotation reference. |
| 14 | +// @intent represent cross-namespace @see links without coupling annotations to graph storage. |
| 15 | +type Ref struct { |
| 16 | + Raw string `json:"raw"` |
| 17 | + Namespace string `json:"namespace"` |
| 18 | + Path string `json:"path,omitempty"` |
| 19 | + Symbol string `json:"symbol,omitempty"` |
| 20 | + Scope string `json:"scope"` |
| 21 | +} |
| 22 | + |
| 23 | +// Is reports whether value uses the CCG reference scheme. |
| 24 | +// @intent let callers branch between local @see values and cross-namespace CCG refs cheaply. |
| 25 | +func Is(value string) bool { |
| 26 | + return strings.HasPrefix(strings.TrimSpace(value), Scheme+"://") |
| 27 | +} |
| 28 | + |
| 29 | +// Parse validates and normalizes a ccg:// reference. |
| 30 | +// @intent decode ccg://{namespace}/{path}#{symbol} values used by @see annotations. |
| 31 | +// @domainRule namespace is required and must be a single safe path segment. |
| 32 | +// @domainRule path and symbol are optional; a path with no symbol represents a file or package path. |
| 33 | +func Parse(value string) (*Ref, error) { |
| 34 | + raw := strings.TrimSpace(value) |
| 35 | + if raw == "" { |
| 36 | + return nil, fmt.Errorf("empty ref") |
| 37 | + } |
| 38 | + u, err := url.Parse(raw) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("parse ref: %w", err) |
| 41 | + } |
| 42 | + if u.Scheme != Scheme { |
| 43 | + return nil, fmt.Errorf("ref scheme must be %q", Scheme) |
| 44 | + } |
| 45 | + if u.User != nil || u.RawQuery != "" || u.Opaque != "" { |
| 46 | + return nil, fmt.Errorf("ccg ref must not contain userinfo, query, or opaque data") |
| 47 | + } |
| 48 | + namespace := strings.TrimSpace(u.Host) |
| 49 | + if err := validateNamespace(namespace); err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + refPath, err := normalizeRefPath(u.EscapedPath()) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + symbol, err := url.PathUnescape(u.EscapedFragment()) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("decode symbol: %w", err) |
| 59 | + } |
| 60 | + symbol = strings.TrimSpace(symbol) |
| 61 | + if strings.ContainsAny(symbol, "\r\n") { |
| 62 | + return nil, fmt.Errorf("symbol must be a single line") |
| 63 | + } |
| 64 | + return &Ref{ |
| 65 | + Raw: raw, |
| 66 | + Namespace: namespace, |
| 67 | + Path: refPath, |
| 68 | + Symbol: symbol, |
| 69 | + Scope: scopeFor(refPath, symbol), |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +// MustParse is a test helper for constructing refs. |
| 74 | +// @intent keep tests concise while still using the production parser. |
| 75 | +func MustParse(value string) Ref { |
| 76 | + ref, err := Parse(value) |
| 77 | + if err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + return *ref |
| 81 | +} |
| 82 | + |
| 83 | +// Display returns a compact human-readable form for UI labels and logs. |
| 84 | +// @intent shorten ccg refs while preserving namespace, path, and symbol identity. |
| 85 | +func (r Ref) Display() string { |
| 86 | + var b strings.Builder |
| 87 | + b.WriteString(r.Namespace) |
| 88 | + if r.Path != "" { |
| 89 | + b.WriteString("/") |
| 90 | + b.WriteString(r.Path) |
| 91 | + } |
| 92 | + if r.Symbol != "" { |
| 93 | + b.WriteString("#") |
| 94 | + b.WriteString(r.Symbol) |
| 95 | + } |
| 96 | + return b.String() |
| 97 | +} |
| 98 | + |
| 99 | +// @intent reject namespace values that could escape namespace storage roots. |
| 100 | +func validateNamespace(namespace string) error { |
| 101 | + if namespace == "" { |
| 102 | + return fmt.Errorf("namespace is required") |
| 103 | + } |
| 104 | + if namespace == "." || namespace == ".." || strings.ContainsAny(namespace, `/\`) || strings.HasPrefix(namespace, "..") { |
| 105 | + return fmt.Errorf("invalid namespace: must be a single safe name") |
| 106 | + } |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// @intent normalize the URI path part into the same slash-separated file paths used by graph nodes. |
| 111 | +func normalizeRefPath(escapedPath string) (string, error) { |
| 112 | + if escapedPath == "" || escapedPath == "/" { |
| 113 | + return "", nil |
| 114 | + } |
| 115 | + decoded, err := url.PathUnescape(escapedPath) |
| 116 | + if err != nil { |
| 117 | + return "", fmt.Errorf("decode path: %w", err) |
| 118 | + } |
| 119 | + decoded = strings.TrimPrefix(decoded, "/") |
| 120 | + if decoded == "" { |
| 121 | + return "", nil |
| 122 | + } |
| 123 | + if strings.Contains(decoded, `\`) || strings.ContainsAny(decoded, "\r\n") { |
| 124 | + return "", fmt.Errorf("invalid path: path traversal not allowed") |
| 125 | + } |
| 126 | + clean := path.Clean(decoded) |
| 127 | + if clean == "." { |
| 128 | + return "", nil |
| 129 | + } |
| 130 | + if strings.HasPrefix(clean, "../") || clean == ".." || strings.HasPrefix(clean, "/") { |
| 131 | + return "", fmt.Errorf("invalid path: path traversal not allowed") |
| 132 | + } |
| 133 | + return clean, nil |
| 134 | +} |
| 135 | + |
| 136 | +// @intent classify refs for clients that want to render namespace, path, and symbol scopes differently. |
| 137 | +func scopeFor(refPath, symbol string) string { |
| 138 | + if symbol != "" { |
| 139 | + return "symbol" |
| 140 | + } |
| 141 | + if refPath != "" { |
| 142 | + return "path" |
| 143 | + } |
| 144 | + return "namespace" |
| 145 | +} |
0 commit comments