module.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package module defines the module.Version type along with support code.
  5. //
  6. // The module.Version type is a simple Path, Version pair:
  7. //
  8. // type Version struct {
  9. // Path string
  10. // Version string
  11. // }
  12. //
  13. // There are no restrictions imposed directly by use of this structure,
  14. // but additional checking functions, most notably Check, verify that
  15. // a particular path, version pair is valid.
  16. //
  17. // # Escaped Paths
  18. //
  19. // Module paths appear as substrings of file system paths
  20. // (in the download cache) and of web server URLs in the proxy protocol.
  21. // In general we cannot rely on file systems to be case-sensitive,
  22. // nor can we rely on web servers, since they read from file systems.
  23. // That is, we cannot rely on the file system to keep rsc.io/QUOTE
  24. // and rsc.io/quote separate. Windows and macOS don't.
  25. // Instead, we must never require two different casings of a file path.
  26. // Because we want the download cache to match the proxy protocol,
  27. // and because we want the proxy protocol to be possible to serve
  28. // from a tree of static files (which might be stored on a case-insensitive
  29. // file system), the proxy protocol must never require two different casings
  30. // of a URL path either.
  31. //
  32. // One possibility would be to make the escaped form be the lowercase
  33. // hexadecimal encoding of the actual path bytes. This would avoid ever
  34. // needing different casings of a file path, but it would be fairly illegible
  35. // to most programmers when those paths appeared in the file system
  36. // (including in file paths in compiler errors and stack traces)
  37. // in web server logs, and so on. Instead, we want a safe escaped form that
  38. // leaves most paths unaltered.
  39. //
  40. // The safe escaped form is to replace every uppercase letter
  41. // with an exclamation mark followed by the letter's lowercase equivalent.
  42. //
  43. // For example,
  44. //
  45. // github.com/Azure/azure-sdk-for-go -> github.com/!azure/azure-sdk-for-go.
  46. // github.com/GoogleCloudPlatform/cloudsql-proxy -> github.com/!google!cloud!platform/cloudsql-proxy
  47. // github.com/Sirupsen/logrus -> github.com/!sirupsen/logrus.
  48. //
  49. // Import paths that avoid upper-case letters are left unchanged.
  50. // Note that because import paths are ASCII-only and avoid various
  51. // problematic punctuation (like : < and >), the escaped form is also ASCII-only
  52. // and avoids the same problematic punctuation.
  53. //
  54. // Import paths have never allowed exclamation marks, so there is no
  55. // need to define how to escape a literal !.
  56. //
  57. // # Unicode Restrictions
  58. //
  59. // Today, paths are disallowed from using Unicode.
  60. //
  61. // Although paths are currently disallowed from using Unicode,
  62. // we would like at some point to allow Unicode letters as well, to assume that
  63. // file systems and URLs are Unicode-safe (storing UTF-8), and apply
  64. // the !-for-uppercase convention for escaping them in the file system.
  65. // But there are at least two subtle considerations.
  66. //
  67. // First, note that not all case-fold equivalent distinct runes
  68. // form an upper/lower pair.
  69. // For example, U+004B ('K'), U+006B ('k'), and U+212A ('K' for Kelvin)
  70. // are three distinct runes that case-fold to each other.
  71. // When we do add Unicode letters, we must not assume that upper/lower
  72. // are the only case-equivalent pairs.
  73. // Perhaps the Kelvin symbol would be disallowed entirely, for example.
  74. // Or perhaps it would escape as "!!k", or perhaps as "(212A)".
  75. //
  76. // Second, it would be nice to allow Unicode marks as well as letters,
  77. // but marks include combining marks, and then we must deal not
  78. // only with case folding but also normalization: both U+00E9 ('é')
  79. // and U+0065 U+0301 ('e' followed by combining acute accent)
  80. // look the same on the page and are treated by some file systems
  81. // as the same path. If we do allow Unicode marks in paths, there
  82. // must be some kind of normalization to allow only one canonical
  83. // encoding of any character used in an import path.
  84. package module
  85. // IMPORTANT NOTE
  86. //
  87. // This file essentially defines the set of valid import paths for the go command.
  88. // There are many subtle considerations, including Unicode ambiguity,
  89. // security, network, and file system representations.
  90. //
  91. // This file also defines the set of valid module path and version combinations,
  92. // another topic with many subtle considerations.
  93. //
  94. // Changes to the semantics in this file require approval from rsc.
  95. import (
  96. "errors"
  97. "fmt"
  98. "path"
  99. "sort"
  100. "strings"
  101. "unicode"
  102. "unicode/utf8"
  103. "golang.org/x/mod/semver"
  104. )
  105. // A Version (for clients, a module.Version) is defined by a module path and version pair.
  106. // These are stored in their plain (unescaped) form.
  107. type Version struct {
  108. // Path is a module path, like "golang.org/x/text" or "rsc.io/quote/v2".
  109. Path string
  110. // Version is usually a semantic version in canonical form.
  111. // There are three exceptions to this general rule.
  112. // First, the top-level target of a build has no specific version
  113. // and uses Version = "".
  114. // Second, during MVS calculations the version "none" is used
  115. // to represent the decision to take no version of a given module.
  116. // Third, filesystem paths found in "replace" directives are
  117. // represented by a path with an empty version.
  118. Version string `json:",omitempty"`
  119. }
  120. // String returns a representation of the Version suitable for logging
  121. // (Path@Version, or just Path if Version is empty).
  122. func (m Version) String() string {
  123. if m.Version == "" {
  124. return m.Path
  125. }
  126. return m.Path + "@" + m.Version
  127. }
  128. // A ModuleError indicates an error specific to a module.
  129. type ModuleError struct {
  130. Path string
  131. Version string
  132. Err error
  133. }
  134. // VersionError returns a ModuleError derived from a Version and error,
  135. // or err itself if it is already such an error.
  136. func VersionError(v Version, err error) error {
  137. var mErr *ModuleError
  138. if errors.As(err, &mErr) && mErr.Path == v.Path && mErr.Version == v.Version {
  139. return err
  140. }
  141. return &ModuleError{
  142. Path: v.Path,
  143. Version: v.Version,
  144. Err: err,
  145. }
  146. }
  147. func (e *ModuleError) Error() string {
  148. if v, ok := e.Err.(*InvalidVersionError); ok {
  149. return fmt.Sprintf("%s@%s: invalid %s: %v", e.Path, v.Version, v.noun(), v.Err)
  150. }
  151. if e.Version != "" {
  152. return fmt.Sprintf("%s@%s: %v", e.Path, e.Version, e.Err)
  153. }
  154. return fmt.Sprintf("module %s: %v", e.Path, e.Err)
  155. }
  156. func (e *ModuleError) Unwrap() error { return e.Err }
  157. // An InvalidVersionError indicates an error specific to a version, with the
  158. // module path unknown or specified externally.
  159. //
  160. // A ModuleError may wrap an InvalidVersionError, but an InvalidVersionError
  161. // must not wrap a ModuleError.
  162. type InvalidVersionError struct {
  163. Version string
  164. Pseudo bool
  165. Err error
  166. }
  167. // noun returns either "version" or "pseudo-version", depending on whether
  168. // e.Version is a pseudo-version.
  169. func (e *InvalidVersionError) noun() string {
  170. if e.Pseudo {
  171. return "pseudo-version"
  172. }
  173. return "version"
  174. }
  175. func (e *InvalidVersionError) Error() string {
  176. return fmt.Sprintf("%s %q invalid: %s", e.noun(), e.Version, e.Err)
  177. }
  178. func (e *InvalidVersionError) Unwrap() error { return e.Err }
  179. // An InvalidPathError indicates a module, import, or file path doesn't
  180. // satisfy all naming constraints. See CheckPath, CheckImportPath,
  181. // and CheckFilePath for specific restrictions.
  182. type InvalidPathError struct {
  183. Kind string // "module", "import", or "file"
  184. Path string
  185. Err error
  186. }
  187. func (e *InvalidPathError) Error() string {
  188. return fmt.Sprintf("malformed %s path %q: %v", e.Kind, e.Path, e.Err)
  189. }
  190. func (e *InvalidPathError) Unwrap() error { return e.Err }
  191. // Check checks that a given module path, version pair is valid.
  192. // In addition to the path being a valid module path
  193. // and the version being a valid semantic version,
  194. // the two must correspond.
  195. // For example, the path "yaml/v2" only corresponds to
  196. // semantic versions beginning with "v2.".
  197. func Check(path, version string) error {
  198. if err := CheckPath(path); err != nil {
  199. return err
  200. }
  201. if !semver.IsValid(version) {
  202. return &ModuleError{
  203. Path: path,
  204. Err: &InvalidVersionError{Version: version, Err: errors.New("not a semantic version")},
  205. }
  206. }
  207. _, pathMajor, _ := SplitPathVersion(path)
  208. if err := CheckPathMajor(version, pathMajor); err != nil {
  209. return &ModuleError{Path: path, Err: err}
  210. }
  211. return nil
  212. }
  213. // firstPathOK reports whether r can appear in the first element of a module path.
  214. // The first element of the path must be an LDH domain name, at least for now.
  215. // To avoid case ambiguity, the domain name must be entirely lower case.
  216. func firstPathOK(r rune) bool {
  217. return r == '-' || r == '.' ||
  218. '0' <= r && r <= '9' ||
  219. 'a' <= r && r <= 'z'
  220. }
  221. // modPathOK reports whether r can appear in a module path element.
  222. // Paths can be ASCII letters, ASCII digits, and limited ASCII punctuation: - . _ and ~.
  223. //
  224. // This matches what "go get" has historically recognized in import paths,
  225. // and avoids confusing sequences like '%20' or '+' that would change meaning
  226. // if used in a URL.
  227. //
  228. // TODO(rsc): We would like to allow Unicode letters, but that requires additional
  229. // care in the safe encoding (see "escaped paths" above).
  230. func modPathOK(r rune) bool {
  231. if r < utf8.RuneSelf {
  232. return r == '-' || r == '.' || r == '_' || r == '~' ||
  233. '0' <= r && r <= '9' ||
  234. 'A' <= r && r <= 'Z' ||
  235. 'a' <= r && r <= 'z'
  236. }
  237. return false
  238. }
  239. // importPathOK reports whether r can appear in a package import path element.
  240. //
  241. // Import paths are intermediate between module paths and file paths: we allow
  242. // disallow characters that would be confusing or ambiguous as arguments to
  243. // 'go get' (such as '@' and ' ' ), but allow certain characters that are
  244. // otherwise-unambiguous on the command line and historically used for some
  245. // binary names (such as '++' as a suffix for compiler binaries and wrappers).
  246. func importPathOK(r rune) bool {
  247. return modPathOK(r) || r == '+'
  248. }
  249. // fileNameOK reports whether r can appear in a file name.
  250. // For now we allow all Unicode letters but otherwise limit to pathOK plus a few more punctuation characters.
  251. // If we expand the set of allowed characters here, we have to
  252. // work harder at detecting potential case-folding and normalization collisions.
  253. // See note about "escaped paths" above.
  254. func fileNameOK(r rune) bool {
  255. if r < utf8.RuneSelf {
  256. // Entire set of ASCII punctuation, from which we remove characters:
  257. // ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
  258. // We disallow some shell special characters: " ' * < > ? ` |
  259. // (Note that some of those are disallowed by the Windows file system as well.)
  260. // We also disallow path separators / : and \ (fileNameOK is only called on path element characters).
  261. // We allow spaces (U+0020) in file names.
  262. const allowed = "!#$%&()+,-.=@[]^_{}~ "
  263. if '0' <= r && r <= '9' || 'A' <= r && r <= 'Z' || 'a' <= r && r <= 'z' {
  264. return true
  265. }
  266. return strings.ContainsRune(allowed, r)
  267. }
  268. // It may be OK to add more ASCII punctuation here, but only carefully.
  269. // For example Windows disallows < > \, and macOS disallows :, so we must not allow those.
  270. return unicode.IsLetter(r)
  271. }
  272. // CheckPath checks that a module path is valid.
  273. // A valid module path is a valid import path, as checked by CheckImportPath,
  274. // with three additional constraints.
  275. // First, the leading path element (up to the first slash, if any),
  276. // by convention a domain name, must contain only lower-case ASCII letters,
  277. // ASCII digits, dots (U+002E), and dashes (U+002D);
  278. // it must contain at least one dot and cannot start with a dash.
  279. // Second, for a final path element of the form /vN, where N looks numeric
  280. // (ASCII digits and dots) must not begin with a leading zero, must not be /v1,
  281. // and must not contain any dots. For paths beginning with "gopkg.in/",
  282. // this second requirement is replaced by a requirement that the path
  283. // follow the gopkg.in server's conventions.
  284. // Third, no path element may begin with a dot.
  285. func CheckPath(path string) (err error) {
  286. defer func() {
  287. if err != nil {
  288. err = &InvalidPathError{Kind: "module", Path: path, Err: err}
  289. }
  290. }()
  291. if err := checkPath(path, modulePath); err != nil {
  292. return err
  293. }
  294. i := strings.Index(path, "/")
  295. if i < 0 {
  296. i = len(path)
  297. }
  298. if i == 0 {
  299. return fmt.Errorf("leading slash")
  300. }
  301. if !strings.Contains(path[:i], ".") {
  302. return fmt.Errorf("missing dot in first path element")
  303. }
  304. if path[0] == '-' {
  305. return fmt.Errorf("leading dash in first path element")
  306. }
  307. for _, r := range path[:i] {
  308. if !firstPathOK(r) {
  309. return fmt.Errorf("invalid char %q in first path element", r)
  310. }
  311. }
  312. if _, _, ok := SplitPathVersion(path); !ok {
  313. return fmt.Errorf("invalid version")
  314. }
  315. return nil
  316. }
  317. // CheckImportPath checks that an import path is valid.
  318. //
  319. // A valid import path consists of one or more valid path elements
  320. // separated by slashes (U+002F). (It must not begin with nor end in a slash.)
  321. //
  322. // A valid path element is a non-empty string made up of
  323. // ASCII letters, ASCII digits, and limited ASCII punctuation: - . _ and ~.
  324. // It must not end with a dot (U+002E), nor contain two dots in a row.
  325. //
  326. // The element prefix up to the first dot must not be a reserved file name
  327. // on Windows, regardless of case (CON, com1, NuL, and so on). The element
  328. // must not have a suffix of a tilde followed by one or more ASCII digits
  329. // (to exclude paths elements that look like Windows short-names).
  330. //
  331. // CheckImportPath may be less restrictive in the future, but see the
  332. // top-level package documentation for additional information about
  333. // subtleties of Unicode.
  334. func CheckImportPath(path string) error {
  335. if err := checkPath(path, importPath); err != nil {
  336. return &InvalidPathError{Kind: "import", Path: path, Err: err}
  337. }
  338. return nil
  339. }
  340. // pathKind indicates what kind of path we're checking. Module paths,
  341. // import paths, and file paths have different restrictions.
  342. type pathKind int
  343. const (
  344. modulePath pathKind = iota
  345. importPath
  346. filePath
  347. )
  348. // checkPath checks that a general path is valid. kind indicates what
  349. // specific constraints should be applied.
  350. //
  351. // checkPath returns an error describing why the path is not valid.
  352. // Because these checks apply to module, import, and file paths,
  353. // and because other checks may be applied, the caller is expected to wrap
  354. // this error with InvalidPathError.
  355. func checkPath(path string, kind pathKind) error {
  356. if !utf8.ValidString(path) {
  357. return fmt.Errorf("invalid UTF-8")
  358. }
  359. if path == "" {
  360. return fmt.Errorf("empty string")
  361. }
  362. if path[0] == '-' && kind != filePath {
  363. return fmt.Errorf("leading dash")
  364. }
  365. if strings.Contains(path, "//") {
  366. return fmt.Errorf("double slash")
  367. }
  368. if path[len(path)-1] == '/' {
  369. return fmt.Errorf("trailing slash")
  370. }
  371. elemStart := 0
  372. for i, r := range path {
  373. if r == '/' {
  374. if err := checkElem(path[elemStart:i], kind); err != nil {
  375. return err
  376. }
  377. elemStart = i + 1
  378. }
  379. }
  380. if err := checkElem(path[elemStart:], kind); err != nil {
  381. return err
  382. }
  383. return nil
  384. }
  385. // checkElem checks whether an individual path element is valid.
  386. func checkElem(elem string, kind pathKind) error {
  387. if elem == "" {
  388. return fmt.Errorf("empty path element")
  389. }
  390. if strings.Count(elem, ".") == len(elem) {
  391. return fmt.Errorf("invalid path element %q", elem)
  392. }
  393. if elem[0] == '.' && kind == modulePath {
  394. return fmt.Errorf("leading dot in path element")
  395. }
  396. if elem[len(elem)-1] == '.' {
  397. return fmt.Errorf("trailing dot in path element")
  398. }
  399. for _, r := range elem {
  400. ok := false
  401. switch kind {
  402. case modulePath:
  403. ok = modPathOK(r)
  404. case importPath:
  405. ok = importPathOK(r)
  406. case filePath:
  407. ok = fileNameOK(r)
  408. default:
  409. panic(fmt.Sprintf("internal error: invalid kind %v", kind))
  410. }
  411. if !ok {
  412. return fmt.Errorf("invalid char %q", r)
  413. }
  414. }
  415. // Windows disallows a bunch of path elements, sadly.
  416. // See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
  417. short := elem
  418. if i := strings.Index(short, "."); i >= 0 {
  419. short = short[:i]
  420. }
  421. for _, bad := range badWindowsNames {
  422. if strings.EqualFold(bad, short) {
  423. return fmt.Errorf("%q disallowed as path element component on Windows", short)
  424. }
  425. }
  426. if kind == filePath {
  427. // don't check for Windows short-names in file names. They're
  428. // only an issue for import paths.
  429. return nil
  430. }
  431. // Reject path components that look like Windows short-names.
  432. // Those usually end in a tilde followed by one or more ASCII digits.
  433. if tilde := strings.LastIndexByte(short, '~'); tilde >= 0 && tilde < len(short)-1 {
  434. suffix := short[tilde+1:]
  435. suffixIsDigits := true
  436. for _, r := range suffix {
  437. if r < '0' || r > '9' {
  438. suffixIsDigits = false
  439. break
  440. }
  441. }
  442. if suffixIsDigits {
  443. return fmt.Errorf("trailing tilde and digits in path element")
  444. }
  445. }
  446. return nil
  447. }
  448. // CheckFilePath checks that a slash-separated file path is valid.
  449. // The definition of a valid file path is the same as the definition
  450. // of a valid import path except that the set of allowed characters is larger:
  451. // all Unicode letters, ASCII digits, the ASCII space character (U+0020),
  452. // and the ASCII punctuation characters
  453. // “!#$%&()+,-.=@[]^_{}~”.
  454. // (The excluded punctuation characters, " * < > ? ` ' | / \ and :,
  455. // have special meanings in certain shells or operating systems.)
  456. //
  457. // CheckFilePath may be less restrictive in the future, but see the
  458. // top-level package documentation for additional information about
  459. // subtleties of Unicode.
  460. func CheckFilePath(path string) error {
  461. if err := checkPath(path, filePath); err != nil {
  462. return &InvalidPathError{Kind: "file", Path: path, Err: err}
  463. }
  464. return nil
  465. }
  466. // badWindowsNames are the reserved file path elements on Windows.
  467. // See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
  468. var badWindowsNames = []string{
  469. "CON",
  470. "PRN",
  471. "AUX",
  472. "NUL",
  473. "COM1",
  474. "COM2",
  475. "COM3",
  476. "COM4",
  477. "COM5",
  478. "COM6",
  479. "COM7",
  480. "COM8",
  481. "COM9",
  482. "LPT1",
  483. "LPT2",
  484. "LPT3",
  485. "LPT4",
  486. "LPT5",
  487. "LPT6",
  488. "LPT7",
  489. "LPT8",
  490. "LPT9",
  491. }
  492. // SplitPathVersion returns prefix and major version such that prefix+pathMajor == path
  493. // and version is either empty or "/vN" for N >= 2.
  494. // As a special case, gopkg.in paths are recognized directly;
  495. // they require ".vN" instead of "/vN", and for all N, not just N >= 2.
  496. // SplitPathVersion returns with ok = false when presented with
  497. // a path whose last path element does not satisfy the constraints
  498. // applied by CheckPath, such as "example.com/pkg/v1" or "example.com/pkg/v1.2".
  499. func SplitPathVersion(path string) (prefix, pathMajor string, ok bool) {
  500. if strings.HasPrefix(path, "gopkg.in/") {
  501. return splitGopkgIn(path)
  502. }
  503. i := len(path)
  504. dot := false
  505. for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') {
  506. if path[i-1] == '.' {
  507. dot = true
  508. }
  509. i--
  510. }
  511. if i <= 1 || i == len(path) || path[i-1] != 'v' || path[i-2] != '/' {
  512. return path, "", true
  513. }
  514. prefix, pathMajor = path[:i-2], path[i-2:]
  515. if dot || len(pathMajor) <= 2 || pathMajor[2] == '0' || pathMajor == "/v1" {
  516. return path, "", false
  517. }
  518. return prefix, pathMajor, true
  519. }
  520. // splitGopkgIn is like SplitPathVersion but only for gopkg.in paths.
  521. func splitGopkgIn(path string) (prefix, pathMajor string, ok bool) {
  522. if !strings.HasPrefix(path, "gopkg.in/") {
  523. return path, "", false
  524. }
  525. i := len(path)
  526. if strings.HasSuffix(path, "-unstable") {
  527. i -= len("-unstable")
  528. }
  529. for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9') {
  530. i--
  531. }
  532. if i <= 1 || path[i-1] != 'v' || path[i-2] != '.' {
  533. // All gopkg.in paths must end in vN for some N.
  534. return path, "", false
  535. }
  536. prefix, pathMajor = path[:i-2], path[i-2:]
  537. if len(pathMajor) <= 2 || pathMajor[2] == '0' && pathMajor != ".v0" {
  538. return path, "", false
  539. }
  540. return prefix, pathMajor, true
  541. }
  542. // MatchPathMajor reports whether the semantic version v
  543. // matches the path major version pathMajor.
  544. //
  545. // MatchPathMajor returns true if and only if CheckPathMajor returns nil.
  546. func MatchPathMajor(v, pathMajor string) bool {
  547. return CheckPathMajor(v, pathMajor) == nil
  548. }
  549. // CheckPathMajor returns a non-nil error if the semantic version v
  550. // does not match the path major version pathMajor.
  551. func CheckPathMajor(v, pathMajor string) error {
  552. // TODO(jayconrod): return errors or panic for invalid inputs. This function
  553. // (and others) was covered by integration tests for cmd/go, and surrounding
  554. // code protected against invalid inputs like non-canonical versions.
  555. if strings.HasPrefix(pathMajor, ".v") && strings.HasSuffix(pathMajor, "-unstable") {
  556. pathMajor = strings.TrimSuffix(pathMajor, "-unstable")
  557. }
  558. if strings.HasPrefix(v, "v0.0.0-") && pathMajor == ".v1" {
  559. // Allow old bug in pseudo-versions that generated v0.0.0- pseudoversion for gopkg .v1.
  560. // For example, gopkg.in/yaml.v2@v2.2.1's go.mod requires gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405.
  561. return nil
  562. }
  563. m := semver.Major(v)
  564. if pathMajor == "" {
  565. if m == "v0" || m == "v1" || semver.Build(v) == "+incompatible" {
  566. return nil
  567. }
  568. pathMajor = "v0 or v1"
  569. } else if pathMajor[0] == '/' || pathMajor[0] == '.' {
  570. if m == pathMajor[1:] {
  571. return nil
  572. }
  573. pathMajor = pathMajor[1:]
  574. }
  575. return &InvalidVersionError{
  576. Version: v,
  577. Err: fmt.Errorf("should be %s, not %s", pathMajor, semver.Major(v)),
  578. }
  579. }
  580. // PathMajorPrefix returns the major-version tag prefix implied by pathMajor.
  581. // An empty PathMajorPrefix allows either v0 or v1.
  582. //
  583. // Note that MatchPathMajor may accept some versions that do not actually begin
  584. // with this prefix: namely, it accepts a 'v0.0.0-' prefix for a '.v1'
  585. // pathMajor, even though that pathMajor implies 'v1' tagging.
  586. func PathMajorPrefix(pathMajor string) string {
  587. if pathMajor == "" {
  588. return ""
  589. }
  590. if pathMajor[0] != '/' && pathMajor[0] != '.' {
  591. panic("pathMajor suffix " + pathMajor + " passed to PathMajorPrefix lacks separator")
  592. }
  593. if strings.HasPrefix(pathMajor, ".v") && strings.HasSuffix(pathMajor, "-unstable") {
  594. pathMajor = strings.TrimSuffix(pathMajor, "-unstable")
  595. }
  596. m := pathMajor[1:]
  597. if m != semver.Major(m) {
  598. panic("pathMajor suffix " + pathMajor + "passed to PathMajorPrefix is not a valid major version")
  599. }
  600. return m
  601. }
  602. // CanonicalVersion returns the canonical form of the version string v.
  603. // It is the same as semver.Canonical(v) except that it preserves the special build suffix "+incompatible".
  604. func CanonicalVersion(v string) string {
  605. cv := semver.Canonical(v)
  606. if semver.Build(v) == "+incompatible" {
  607. cv += "+incompatible"
  608. }
  609. return cv
  610. }
  611. // Sort sorts the list by Path, breaking ties by comparing Version fields.
  612. // The Version fields are interpreted as semantic versions (using semver.Compare)
  613. // optionally followed by a tie-breaking suffix introduced by a slash character,
  614. // like in "v0.0.1/go.mod".
  615. func Sort(list []Version) {
  616. sort.Slice(list, func(i, j int) bool {
  617. mi := list[i]
  618. mj := list[j]
  619. if mi.Path != mj.Path {
  620. return mi.Path < mj.Path
  621. }
  622. // To help go.sum formatting, allow version/file.
  623. // Compare semver prefix by semver rules,
  624. // file by string order.
  625. vi := mi.Version
  626. vj := mj.Version
  627. var fi, fj string
  628. if k := strings.Index(vi, "/"); k >= 0 {
  629. vi, fi = vi[:k], vi[k:]
  630. }
  631. if k := strings.Index(vj, "/"); k >= 0 {
  632. vj, fj = vj[:k], vj[k:]
  633. }
  634. if vi != vj {
  635. return semver.Compare(vi, vj) < 0
  636. }
  637. return fi < fj
  638. })
  639. }
  640. // EscapePath returns the escaped form of the given module path.
  641. // It fails if the module path is invalid.
  642. func EscapePath(path string) (escaped string, err error) {
  643. if err := CheckPath(path); err != nil {
  644. return "", err
  645. }
  646. return escapeString(path)
  647. }
  648. // EscapeVersion returns the escaped form of the given module version.
  649. // Versions are allowed to be in non-semver form but must be valid file names
  650. // and not contain exclamation marks.
  651. func EscapeVersion(v string) (escaped string, err error) {
  652. if err := checkElem(v, filePath); err != nil || strings.Contains(v, "!") {
  653. return "", &InvalidVersionError{
  654. Version: v,
  655. Err: fmt.Errorf("disallowed version string"),
  656. }
  657. }
  658. return escapeString(v)
  659. }
  660. func escapeString(s string) (escaped string, err error) {
  661. haveUpper := false
  662. for _, r := range s {
  663. if r == '!' || r >= utf8.RuneSelf {
  664. // This should be disallowed by CheckPath, but diagnose anyway.
  665. // The correctness of the escaping loop below depends on it.
  666. return "", fmt.Errorf("internal error: inconsistency in EscapePath")
  667. }
  668. if 'A' <= r && r <= 'Z' {
  669. haveUpper = true
  670. }
  671. }
  672. if !haveUpper {
  673. return s, nil
  674. }
  675. var buf []byte
  676. for _, r := range s {
  677. if 'A' <= r && r <= 'Z' {
  678. buf = append(buf, '!', byte(r+'a'-'A'))
  679. } else {
  680. buf = append(buf, byte(r))
  681. }
  682. }
  683. return string(buf), nil
  684. }
  685. // UnescapePath returns the module path for the given escaped path.
  686. // It fails if the escaped path is invalid or describes an invalid path.
  687. func UnescapePath(escaped string) (path string, err error) {
  688. path, ok := unescapeString(escaped)
  689. if !ok {
  690. return "", fmt.Errorf("invalid escaped module path %q", escaped)
  691. }
  692. if err := CheckPath(path); err != nil {
  693. return "", fmt.Errorf("invalid escaped module path %q: %v", escaped, err)
  694. }
  695. return path, nil
  696. }
  697. // UnescapeVersion returns the version string for the given escaped version.
  698. // It fails if the escaped form is invalid or describes an invalid version.
  699. // Versions are allowed to be in non-semver form but must be valid file names
  700. // and not contain exclamation marks.
  701. func UnescapeVersion(escaped string) (v string, err error) {
  702. v, ok := unescapeString(escaped)
  703. if !ok {
  704. return "", fmt.Errorf("invalid escaped version %q", escaped)
  705. }
  706. if err := checkElem(v, filePath); err != nil {
  707. return "", fmt.Errorf("invalid escaped version %q: %v", v, err)
  708. }
  709. return v, nil
  710. }
  711. func unescapeString(escaped string) (string, bool) {
  712. var buf []byte
  713. bang := false
  714. for _, r := range escaped {
  715. if r >= utf8.RuneSelf {
  716. return "", false
  717. }
  718. if bang {
  719. bang = false
  720. if r < 'a' || 'z' < r {
  721. return "", false
  722. }
  723. buf = append(buf, byte(r+'A'-'a'))
  724. continue
  725. }
  726. if r == '!' {
  727. bang = true
  728. continue
  729. }
  730. if 'A' <= r && r <= 'Z' {
  731. return "", false
  732. }
  733. buf = append(buf, byte(r))
  734. }
  735. if bang {
  736. return "", false
  737. }
  738. return string(buf), true
  739. }
  740. // MatchPrefixPatterns reports whether any path prefix of target matches one of
  741. // the glob patterns (as defined by path.Match) in the comma-separated globs
  742. // list. This implements the algorithm used when matching a module path to the
  743. // GOPRIVATE environment variable, as described by 'go help module-private'.
  744. //
  745. // It ignores any empty or malformed patterns in the list.
  746. // Trailing slashes on patterns are ignored.
  747. func MatchPrefixPatterns(globs, target string) bool {
  748. for globs != "" {
  749. // Extract next non-empty glob in comma-separated list.
  750. var glob string
  751. if i := strings.Index(globs, ","); i >= 0 {
  752. glob, globs = globs[:i], globs[i+1:]
  753. } else {
  754. glob, globs = globs, ""
  755. }
  756. glob = strings.TrimSuffix(glob, "/")
  757. if glob == "" {
  758. continue
  759. }
  760. // A glob with N+1 path elements (N slashes) needs to be matched
  761. // against the first N+1 path elements of target,
  762. // which end just before the N+1'th slash.
  763. n := strings.Count(glob, "/")
  764. prefix := target
  765. // Walk target, counting slashes, truncating at the N+1'th slash.
  766. for i := 0; i < len(target); i++ {
  767. if target[i] == '/' {
  768. if n == 0 {
  769. prefix = target[:i]
  770. break
  771. }
  772. n--
  773. }
  774. }
  775. if n > 0 {
  776. // Not enough prefix elements.
  777. continue
  778. }
  779. matched, _ := path.Match(glob, prefix)
  780. if matched {
  781. return true
  782. }
  783. }
  784. return false
  785. }