You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

module.go 27 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. "fmt"
  97. "path"
  98. "sort"
  99. "strings"
  100. "unicode"
  101. "unicode/utf8"
  102. "golang.org/x/mod/semver"
  103. errors "golang.org/x/xerrors"
  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. // Check checks that a given module path, version pair is valid.
  180. // In addition to the path being a valid module path
  181. // and the version being a valid semantic version,
  182. // the two must correspond.
  183. // For example, the path "yaml/v2" only corresponds to
  184. // semantic versions beginning with "v2.".
  185. func Check(path, version string) error {
  186. if err := CheckPath(path); err != nil {
  187. return err
  188. }
  189. if !semver.IsValid(version) {
  190. return &ModuleError{
  191. Path: path,
  192. Err: &InvalidVersionError{Version: version, Err: errors.New("not a semantic version")},
  193. }
  194. }
  195. _, pathMajor, _ := SplitPathVersion(path)
  196. if err := CheckPathMajor(version, pathMajor); err != nil {
  197. return &ModuleError{Path: path, Err: err}
  198. }
  199. return nil
  200. }
  201. // firstPathOK reports whether r can appear in the first element of a module path.
  202. // The first element of the path must be an LDH domain name, at least for now.
  203. // To avoid case ambiguity, the domain name must be entirely lower case.
  204. func firstPathOK(r rune) bool {
  205. return r == '-' || r == '.' ||
  206. '0' <= r && r <= '9' ||
  207. 'a' <= r && r <= 'z'
  208. }
  209. // modPathOK reports whether r can appear in a module path element.
  210. // Paths can be ASCII letters, ASCII digits, and limited ASCII punctuation: - . _ and ~.
  211. //
  212. // This matches what "go get" has historically recognized in import paths,
  213. // and avoids confusing sequences like '%20' or '+' that would change meaning
  214. // if used in a URL.
  215. //
  216. // TODO(rsc): We would like to allow Unicode letters, but that requires additional
  217. // care in the safe encoding (see "escaped paths" above).
  218. func modPathOK(r rune) bool {
  219. if r < utf8.RuneSelf {
  220. return r == '-' || r == '.' || r == '_' || r == '~' ||
  221. '0' <= r && r <= '9' ||
  222. 'A' <= r && r <= 'Z' ||
  223. 'a' <= r && r <= 'z'
  224. }
  225. return false
  226. }
  227. // modPathOK reports whether r can appear in a package import path element.
  228. //
  229. // Import paths are intermediate between module paths and file paths: we allow
  230. // disallow characters that would be confusing or ambiguous as arguments to
  231. // 'go get' (such as '@' and ' ' ), but allow certain characters that are
  232. // otherwise-unambiguous on the command line and historically used for some
  233. // binary names (such as '++' as a suffix for compiler binaries and wrappers).
  234. func importPathOK(r rune) bool {
  235. return modPathOK(r) || r == '+'
  236. }
  237. // fileNameOK reports whether r can appear in a file name.
  238. // For now we allow all Unicode letters but otherwise limit to pathOK plus a few more punctuation characters.
  239. // If we expand the set of allowed characters here, we have to
  240. // work harder at detecting potential case-folding and normalization collisions.
  241. // See note about "escaped paths" above.
  242. func fileNameOK(r rune) bool {
  243. if r < utf8.RuneSelf {
  244. // Entire set of ASCII punctuation, from which we remove characters:
  245. // ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
  246. // We disallow some shell special characters: " ' * < > ? ` |
  247. // (Note that some of those are disallowed by the Windows file system as well.)
  248. // We also disallow path separators / : and \ (fileNameOK is only called on path element characters).
  249. // We allow spaces (U+0020) in file names.
  250. const allowed = "!#$%&()+,-.=@[]^_{}~ "
  251. if '0' <= r && r <= '9' || 'A' <= r && r <= 'Z' || 'a' <= r && r <= 'z' {
  252. return true
  253. }
  254. for i := 0; i < len(allowed); i++ {
  255. if rune(allowed[i]) == r {
  256. return true
  257. }
  258. }
  259. return false
  260. }
  261. // It may be OK to add more ASCII punctuation here, but only carefully.
  262. // For example Windows disallows < > \, and macOS disallows :, so we must not allow those.
  263. return unicode.IsLetter(r)
  264. }
  265. // CheckPath checks that a module path is valid.
  266. // A valid module path is a valid import path, as checked by CheckImportPath,
  267. // with three additional constraints.
  268. // First, the leading path element (up to the first slash, if any),
  269. // by convention a domain name, must contain only lower-case ASCII letters,
  270. // ASCII digits, dots (U+002E), and dashes (U+002D);
  271. // it must contain at least one dot and cannot start with a dash.
  272. // Second, for a final path element of the form /vN, where N looks numeric
  273. // (ASCII digits and dots) must not begin with a leading zero, must not be /v1,
  274. // and must not contain any dots. For paths beginning with "gopkg.in/",
  275. // this second requirement is replaced by a requirement that the path
  276. // follow the gopkg.in server's conventions.
  277. // Third, no path element may begin with a dot.
  278. func CheckPath(path string) error {
  279. if err := checkPath(path, modulePath); err != nil {
  280. return fmt.Errorf("malformed module path %q: %v", path, err)
  281. }
  282. i := strings.Index(path, "/")
  283. if i < 0 {
  284. i = len(path)
  285. }
  286. if i == 0 {
  287. return fmt.Errorf("malformed module path %q: leading slash", path)
  288. }
  289. if !strings.Contains(path[:i], ".") {
  290. return fmt.Errorf("malformed module path %q: missing dot in first path element", path)
  291. }
  292. if path[0] == '-' {
  293. return fmt.Errorf("malformed module path %q: leading dash in first path element", path)
  294. }
  295. for _, r := range path[:i] {
  296. if !firstPathOK(r) {
  297. return fmt.Errorf("malformed module path %q: invalid char %q in first path element", path, r)
  298. }
  299. }
  300. if _, _, ok := SplitPathVersion(path); !ok {
  301. return fmt.Errorf("malformed module path %q: invalid version", path)
  302. }
  303. return nil
  304. }
  305. // CheckImportPath checks that an import path is valid.
  306. //
  307. // A valid import path consists of one or more valid path elements
  308. // separated by slashes (U+002F). (It must not begin with nor end in a slash.)
  309. //
  310. // A valid path element is a non-empty string made up of
  311. // ASCII letters, ASCII digits, and limited ASCII punctuation: - . _ and ~.
  312. // It must not end with a dot (U+002E), nor contain two dots in a row.
  313. //
  314. // The element prefix up to the first dot must not be a reserved file name
  315. // on Windows, regardless of case (CON, com1, NuL, and so on). The element
  316. // must not have a suffix of a tilde followed by one or more ASCII digits
  317. // (to exclude paths elements that look like Windows short-names).
  318. //
  319. // CheckImportPath may be less restrictive in the future, but see the
  320. // top-level package documentation for additional information about
  321. // subtleties of Unicode.
  322. func CheckImportPath(path string) error {
  323. if err := checkPath(path, importPath); err != nil {
  324. return fmt.Errorf("malformed import path %q: %v", path, err)
  325. }
  326. return nil
  327. }
  328. // pathKind indicates what kind of path we're checking. Module paths,
  329. // import paths, and file paths have different restrictions.
  330. type pathKind int
  331. const (
  332. modulePath pathKind = iota
  333. importPath
  334. filePath
  335. )
  336. // checkPath checks that a general path is valid.
  337. // It returns an error describing why but not mentioning path.
  338. // Because these checks apply to both module paths and import paths,
  339. // the caller is expected to add the "malformed ___ path %q: " prefix.
  340. // fileName indicates whether the final element of the path is a file name
  341. // (as opposed to a directory name).
  342. func checkPath(path string, kind pathKind) error {
  343. if !utf8.ValidString(path) {
  344. return fmt.Errorf("invalid UTF-8")
  345. }
  346. if path == "" {
  347. return fmt.Errorf("empty string")
  348. }
  349. if path[0] == '-' {
  350. return fmt.Errorf("leading dash")
  351. }
  352. if strings.Contains(path, "//") {
  353. return fmt.Errorf("double slash")
  354. }
  355. if path[len(path)-1] == '/' {
  356. return fmt.Errorf("trailing slash")
  357. }
  358. elemStart := 0
  359. for i, r := range path {
  360. if r == '/' {
  361. if err := checkElem(path[elemStart:i], kind); err != nil {
  362. return err
  363. }
  364. elemStart = i + 1
  365. }
  366. }
  367. if err := checkElem(path[elemStart:], kind); err != nil {
  368. return err
  369. }
  370. return nil
  371. }
  372. // checkElem checks whether an individual path element is valid.
  373. func checkElem(elem string, kind pathKind) error {
  374. if elem == "" {
  375. return fmt.Errorf("empty path element")
  376. }
  377. if strings.Count(elem, ".") == len(elem) {
  378. return fmt.Errorf("invalid path element %q", elem)
  379. }
  380. if elem[0] == '.' && kind == modulePath {
  381. return fmt.Errorf("leading dot in path element")
  382. }
  383. if elem[len(elem)-1] == '.' {
  384. return fmt.Errorf("trailing dot in path element")
  385. }
  386. for _, r := range elem {
  387. ok := false
  388. switch kind {
  389. case modulePath:
  390. ok = modPathOK(r)
  391. case importPath:
  392. ok = importPathOK(r)
  393. case filePath:
  394. ok = fileNameOK(r)
  395. default:
  396. panic(fmt.Sprintf("internal error: invalid kind %v", kind))
  397. }
  398. if !ok {
  399. return fmt.Errorf("invalid char %q", r)
  400. }
  401. }
  402. // Windows disallows a bunch of path elements, sadly.
  403. // See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
  404. short := elem
  405. if i := strings.Index(short, "."); i >= 0 {
  406. short = short[:i]
  407. }
  408. for _, bad := range badWindowsNames {
  409. if strings.EqualFold(bad, short) {
  410. return fmt.Errorf("%q disallowed as path element component on Windows", short)
  411. }
  412. }
  413. if kind == filePath {
  414. // don't check for Windows short-names in file names. They're
  415. // only an issue for import paths.
  416. return nil
  417. }
  418. // Reject path components that look like Windows short-names.
  419. // Those usually end in a tilde followed by one or more ASCII digits.
  420. if tilde := strings.LastIndexByte(short, '~'); tilde >= 0 && tilde < len(short)-1 {
  421. suffix := short[tilde+1:]
  422. suffixIsDigits := true
  423. for _, r := range suffix {
  424. if r < '0' || r > '9' {
  425. suffixIsDigits = false
  426. break
  427. }
  428. }
  429. if suffixIsDigits {
  430. return fmt.Errorf("trailing tilde and digits in path element")
  431. }
  432. }
  433. return nil
  434. }
  435. // CheckFilePath checks that a slash-separated file path is valid.
  436. // The definition of a valid file path is the same as the definition
  437. // of a valid import path except that the set of allowed characters is larger:
  438. // all Unicode letters, ASCII digits, the ASCII space character (U+0020),
  439. // and the ASCII punctuation characters
  440. // “!#$%&()+,-.=@[]^_{}~”.
  441. // (The excluded punctuation characters, " * < > ? ` ' | / \ and :,
  442. // have special meanings in certain shells or operating systems.)
  443. //
  444. // CheckFilePath may be less restrictive in the future, but see the
  445. // top-level package documentation for additional information about
  446. // subtleties of Unicode.
  447. func CheckFilePath(path string) error {
  448. if err := checkPath(path, filePath); err != nil {
  449. return fmt.Errorf("malformed file path %q: %v", path, err)
  450. }
  451. return nil
  452. }
  453. // badWindowsNames are the reserved file path elements on Windows.
  454. // See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
  455. var badWindowsNames = []string{
  456. "CON",
  457. "PRN",
  458. "AUX",
  459. "NUL",
  460. "COM1",
  461. "COM2",
  462. "COM3",
  463. "COM4",
  464. "COM5",
  465. "COM6",
  466. "COM7",
  467. "COM8",
  468. "COM9",
  469. "LPT1",
  470. "LPT2",
  471. "LPT3",
  472. "LPT4",
  473. "LPT5",
  474. "LPT6",
  475. "LPT7",
  476. "LPT8",
  477. "LPT9",
  478. }
  479. // SplitPathVersion returns prefix and major version such that prefix+pathMajor == path
  480. // and version is either empty or "/vN" for N >= 2.
  481. // As a special case, gopkg.in paths are recognized directly;
  482. // they require ".vN" instead of "/vN", and for all N, not just N >= 2.
  483. // SplitPathVersion returns with ok = false when presented with
  484. // a path whose last path element does not satisfy the constraints
  485. // applied by CheckPath, such as "example.com/pkg/v1" or "example.com/pkg/v1.2".
  486. func SplitPathVersion(path string) (prefix, pathMajor string, ok bool) {
  487. if strings.HasPrefix(path, "gopkg.in/") {
  488. return splitGopkgIn(path)
  489. }
  490. i := len(path)
  491. dot := false
  492. for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') {
  493. if path[i-1] == '.' {
  494. dot = true
  495. }
  496. i--
  497. }
  498. if i <= 1 || i == len(path) || path[i-1] != 'v' || path[i-2] != '/' {
  499. return path, "", true
  500. }
  501. prefix, pathMajor = path[:i-2], path[i-2:]
  502. if dot || len(pathMajor) <= 2 || pathMajor[2] == '0' || pathMajor == "/v1" {
  503. return path, "", false
  504. }
  505. return prefix, pathMajor, true
  506. }
  507. // splitGopkgIn is like SplitPathVersion but only for gopkg.in paths.
  508. func splitGopkgIn(path string) (prefix, pathMajor string, ok bool) {
  509. if !strings.HasPrefix(path, "gopkg.in/") {
  510. return path, "", false
  511. }
  512. i := len(path)
  513. if strings.HasSuffix(path, "-unstable") {
  514. i -= len("-unstable")
  515. }
  516. for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9') {
  517. i--
  518. }
  519. if i <= 1 || path[i-1] != 'v' || path[i-2] != '.' {
  520. // All gopkg.in paths must end in vN for some N.
  521. return path, "", false
  522. }
  523. prefix, pathMajor = path[:i-2], path[i-2:]
  524. if len(pathMajor) <= 2 || pathMajor[2] == '0' && pathMajor != ".v0" {
  525. return path, "", false
  526. }
  527. return prefix, pathMajor, true
  528. }
  529. // MatchPathMajor reports whether the semantic version v
  530. // matches the path major version pathMajor.
  531. //
  532. // MatchPathMajor returns true if and only if CheckPathMajor returns nil.
  533. func MatchPathMajor(v, pathMajor string) bool {
  534. return CheckPathMajor(v, pathMajor) == nil
  535. }
  536. // CheckPathMajor returns a non-nil error if the semantic version v
  537. // does not match the path major version pathMajor.
  538. func CheckPathMajor(v, pathMajor string) error {
  539. // TODO(jayconrod): return errors or panic for invalid inputs. This function
  540. // (and others) was covered by integration tests for cmd/go, and surrounding
  541. // code protected against invalid inputs like non-canonical versions.
  542. if strings.HasPrefix(pathMajor, ".v") && strings.HasSuffix(pathMajor, "-unstable") {
  543. pathMajor = strings.TrimSuffix(pathMajor, "-unstable")
  544. }
  545. if strings.HasPrefix(v, "v0.0.0-") && pathMajor == ".v1" {
  546. // Allow old bug in pseudo-versions that generated v0.0.0- pseudoversion for gopkg .v1.
  547. // For example, gopkg.in/yaml.v2@v2.2.1's go.mod requires gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405.
  548. return nil
  549. }
  550. m := semver.Major(v)
  551. if pathMajor == "" {
  552. if m == "v0" || m == "v1" || semver.Build(v) == "+incompatible" {
  553. return nil
  554. }
  555. pathMajor = "v0 or v1"
  556. } else if pathMajor[0] == '/' || pathMajor[0] == '.' {
  557. if m == pathMajor[1:] {
  558. return nil
  559. }
  560. pathMajor = pathMajor[1:]
  561. }
  562. return &InvalidVersionError{
  563. Version: v,
  564. Err: fmt.Errorf("should be %s, not %s", pathMajor, semver.Major(v)),
  565. }
  566. }
  567. // PathMajorPrefix returns the major-version tag prefix implied by pathMajor.
  568. // An empty PathMajorPrefix allows either v0 or v1.
  569. //
  570. // Note that MatchPathMajor may accept some versions that do not actually begin
  571. // with this prefix: namely, it accepts a 'v0.0.0-' prefix for a '.v1'
  572. // pathMajor, even though that pathMajor implies 'v1' tagging.
  573. func PathMajorPrefix(pathMajor string) string {
  574. if pathMajor == "" {
  575. return ""
  576. }
  577. if pathMajor[0] != '/' && pathMajor[0] != '.' {
  578. panic("pathMajor suffix " + pathMajor + " passed to PathMajorPrefix lacks separator")
  579. }
  580. if strings.HasPrefix(pathMajor, ".v") && strings.HasSuffix(pathMajor, "-unstable") {
  581. pathMajor = strings.TrimSuffix(pathMajor, "-unstable")
  582. }
  583. m := pathMajor[1:]
  584. if m != semver.Major(m) {
  585. panic("pathMajor suffix " + pathMajor + "passed to PathMajorPrefix is not a valid major version")
  586. }
  587. return m
  588. }
  589. // CanonicalVersion returns the canonical form of the version string v.
  590. // It is the same as semver.Canonical(v) except that it preserves the special build suffix "+incompatible".
  591. func CanonicalVersion(v string) string {
  592. cv := semver.Canonical(v)
  593. if semver.Build(v) == "+incompatible" {
  594. cv += "+incompatible"
  595. }
  596. return cv
  597. }
  598. // Sort sorts the list by Path, breaking ties by comparing Version fields.
  599. // The Version fields are interpreted as semantic versions (using semver.Compare)
  600. // optionally followed by a tie-breaking suffix introduced by a slash character,
  601. // like in "v0.0.1/go.mod".
  602. func Sort(list []Version) {
  603. sort.Slice(list, func(i, j int) bool {
  604. mi := list[i]
  605. mj := list[j]
  606. if mi.Path != mj.Path {
  607. return mi.Path < mj.Path
  608. }
  609. // To help go.sum formatting, allow version/file.
  610. // Compare semver prefix by semver rules,
  611. // file by string order.
  612. vi := mi.Version
  613. vj := mj.Version
  614. var fi, fj string
  615. if k := strings.Index(vi, "/"); k >= 0 {
  616. vi, fi = vi[:k], vi[k:]
  617. }
  618. if k := strings.Index(vj, "/"); k >= 0 {
  619. vj, fj = vj[:k], vj[k:]
  620. }
  621. if vi != vj {
  622. return semver.Compare(vi, vj) < 0
  623. }
  624. return fi < fj
  625. })
  626. }
  627. // EscapePath returns the escaped form of the given module path.
  628. // It fails if the module path is invalid.
  629. func EscapePath(path string) (escaped string, err error) {
  630. if err := CheckPath(path); err != nil {
  631. return "", err
  632. }
  633. return escapeString(path)
  634. }
  635. // EscapeVersion returns the escaped form of the given module version.
  636. // Versions are allowed to be in non-semver form but must be valid file names
  637. // and not contain exclamation marks.
  638. func EscapeVersion(v string) (escaped string, err error) {
  639. if err := checkElem(v, filePath); err != nil || strings.Contains(v, "!") {
  640. return "", &InvalidVersionError{
  641. Version: v,
  642. Err: fmt.Errorf("disallowed version string"),
  643. }
  644. }
  645. return escapeString(v)
  646. }
  647. func escapeString(s string) (escaped string, err error) {
  648. haveUpper := false
  649. for _, r := range s {
  650. if r == '!' || r >= utf8.RuneSelf {
  651. // This should be disallowed by CheckPath, but diagnose anyway.
  652. // The correctness of the escaping loop below depends on it.
  653. return "", fmt.Errorf("internal error: inconsistency in EscapePath")
  654. }
  655. if 'A' <= r && r <= 'Z' {
  656. haveUpper = true
  657. }
  658. }
  659. if !haveUpper {
  660. return s, nil
  661. }
  662. var buf []byte
  663. for _, r := range s {
  664. if 'A' <= r && r <= 'Z' {
  665. buf = append(buf, '!', byte(r+'a'-'A'))
  666. } else {
  667. buf = append(buf, byte(r))
  668. }
  669. }
  670. return string(buf), nil
  671. }
  672. // UnescapePath returns the module path for the given escaped path.
  673. // It fails if the escaped path is invalid or describes an invalid path.
  674. func UnescapePath(escaped string) (path string, err error) {
  675. path, ok := unescapeString(escaped)
  676. if !ok {
  677. return "", fmt.Errorf("invalid escaped module path %q", escaped)
  678. }
  679. if err := CheckPath(path); err != nil {
  680. return "", fmt.Errorf("invalid escaped module path %q: %v", escaped, err)
  681. }
  682. return path, nil
  683. }
  684. // UnescapeVersion returns the version string for the given escaped version.
  685. // It fails if the escaped form is invalid or describes an invalid version.
  686. // Versions are allowed to be in non-semver form but must be valid file names
  687. // and not contain exclamation marks.
  688. func UnescapeVersion(escaped string) (v string, err error) {
  689. v, ok := unescapeString(escaped)
  690. if !ok {
  691. return "", fmt.Errorf("invalid escaped version %q", escaped)
  692. }
  693. if err := checkElem(v, filePath); err != nil {
  694. return "", fmt.Errorf("invalid escaped version %q: %v", v, err)
  695. }
  696. return v, nil
  697. }
  698. func unescapeString(escaped string) (string, bool) {
  699. var buf []byte
  700. bang := false
  701. for _, r := range escaped {
  702. if r >= utf8.RuneSelf {
  703. return "", false
  704. }
  705. if bang {
  706. bang = false
  707. if r < 'a' || 'z' < r {
  708. return "", false
  709. }
  710. buf = append(buf, byte(r+'A'-'a'))
  711. continue
  712. }
  713. if r == '!' {
  714. bang = true
  715. continue
  716. }
  717. if 'A' <= r && r <= 'Z' {
  718. return "", false
  719. }
  720. buf = append(buf, byte(r))
  721. }
  722. if bang {
  723. return "", false
  724. }
  725. return string(buf), true
  726. }
  727. // MatchPrefixPatterns reports whether any path prefix of target matches one of
  728. // the glob patterns (as defined by path.Match) in the comma-separated globs
  729. // list. This implements the algorithm used when matching a module path to the
  730. // GOPRIVATE environment variable, as described by 'go help module-private'.
  731. //
  732. // It ignores any empty or malformed patterns in the list.
  733. func MatchPrefixPatterns(globs, target string) bool {
  734. for globs != "" {
  735. // Extract next non-empty glob in comma-separated list.
  736. var glob string
  737. if i := strings.Index(globs, ","); i >= 0 {
  738. glob, globs = globs[:i], globs[i+1:]
  739. } else {
  740. glob, globs = globs, ""
  741. }
  742. if glob == "" {
  743. continue
  744. }
  745. // A glob with N+1 path elements (N slashes) needs to be matched
  746. // against the first N+1 path elements of target,
  747. // which end just before the N+1'th slash.
  748. n := strings.Count(glob, "/")
  749. prefix := target
  750. // Walk target, counting slashes, truncating at the N+1'th slash.
  751. for i := 0; i < len(target); i++ {
  752. if target[i] == '/' {
  753. if n == 0 {
  754. prefix = target[:i]
  755. break
  756. }
  757. n--
  758. }
  759. }
  760. if n > 0 {
  761. // Not enough prefix elements.
  762. continue
  763. }
  764. matched, _ := path.Match(glob, prefix)
  765. if matched {
  766. return true
  767. }
  768. }
  769. return false
  770. }