Commit Graph

32 Commits

Author SHA1 Message Date
Jun Wu
9dc21f8d0b codemod: import from the edenscm package
Summary:
D13853115 adds `edenscm/` to `sys.path` and code still uses `import mercurial`.
That has nasty problems if both `import mercurial` and
`import edenscm.mercurial` are used, because Python would think `mercurial.foo`
and `edenscm.mercurial.foo` are different modules so code like
`try: ... except mercurial.error.Foo: ...`, or `isinstance(x, mercurial.foo.Bar)`
would fail to handle the `edenscm.mercurial` version. There are also some
module-level states (ex. `extensions._extensions`) that would cause trouble if
they have multiple versions in a single process.

Change imports to use the `edenscm` so ideally the `mercurial` is no longer
imported at all. Add checks in extensions.py to catch unexpected extensions
importing modules from the old (wrong) locations when running tests.

Reviewed By: phillco

Differential Revision: D13868981

fbshipit-source-id: f4e2513766957fd81d85407994f7521a08e4de48
2019-01-29 17:25:32 -08:00
Jun Wu
584656dff3 codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).

Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.

Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.

An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.

As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.

Reviewed By: wez, phillco, simpkins, pkaush, singhsrb

Differential Revision: D8173629

fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-25 22:17:29 -07:00
Gregory Szorc
38db45a34e sslutil: synchronize hostname matching logic with CPython
sslutil contains its own hostname matching logic. CPython has code
for the same intent. However, it is only available to Python 2.7.9+
(or distributions that have backported 2.7.9's ssl module
improvements).

This patch effectively imports CPython's hostname matching code
from its ssl.py into sslutil.py. The hostname matching code itself
is pretty similar. However, the DNS name matching code is much more
robust and spec conformant.

As the test changes show, this changes some behavior around
wildcard handling and IDNA matching. The new behavior allows
wildcards in the middle of words (e.g. 'f*.com' matches 'foo.com')
This is spec compliant according to RFC 6125 Section 6.5.3 item 3.

There is one test where the matcher is more strict. Before,
'*.a.com' matched '.a.com'. Now it doesn't match. Strictly speaking
this is a security vulnerability.
2016-06-26 19:34:48 -07:00
Gregory Szorc
6119f3112c tests: import CPython's hostname matching tests
CPython has a more comprehensive test suite for it's built-in hostname
matching functionality. This patch adds its tests so we can improve
our hostname matching functionality.

Many of the tests have different results from CPython. These will be
addressed in a subsequent commit.
2016-06-26 19:16:54 -07:00
Pulkit Goyal
9a264b103c tests: make test-url use absolute_import 2016-04-14 00:53:35 +05:30
Pulkit Goyal
3bf3d96968 py3: make test-url use print_function 2016-03-29 16:23:40 +05:30
Matt Mackall
5e1b4ad958 urls: bulk-change primary website URLs 2015-09-30 15:43:49 -05:00
Augie Fackler
a343277143 test-url: move from dict() construction to {} literals
The latter are both faster and more consistent across Python 2 and 3.
2014-03-12 13:21:17 -04:00
Matt Mackall
e82c2e671f merge with stable 2011-12-05 17:48:40 -06:00
Patrick Mezard
3a0effcd7b util: fix url.__str__() for windows file URLs
Before:

  >>> str(url('file:///c:/tmp/foo/bar'))
  'file:c%3C/tmp/foo/bar'

After:

  >>> str(url('file:///c:/tmp/foo/bar'))
  'file:///c%3C/tmp/foo/bar'

The previous behaviour had no effect on mercurial itself (clone command for
instance) because we fortunately called .localpath() on the parsed URL.
hgsubversion was not so lucky and cloning a local subversion repository on
Windows no longer worked on the default branch (it works on stable because
2b62605189dc defeats the hasdriveletter() test in url class).

I do not know if the %3C is correct or not but svn accepts file:// URLs
containing it. Mads fixed it in 2b62605189dc, so we can always backport should
the need arise.
2011-12-04 18:22:25 +01:00
Mads Kiilerich
5d7000644a url: handle file://localhost/c:/foo "correctly"
The path was parsed correctly, but localpath prepended an extra '/' (as in
'/c:/foo') because it assumed it was an absolute unix path.
2011-11-16 00:10:56 +01:00
Matt Mackall
75db0d196a merge with stable 2011-11-17 16:53:17 -06:00
Brodie Rao
114ca03d36 tests: fix readline escape characters in heredoctest.py/test-url.py
This fix mirrors the changes made to test-doctest.py in 04cfbbc5ae97
and 39599b7929c4.

Without this change, tests running heredoctest.py can fail on certain
versions of OS X when TERM is set to xterm-256color:

   $ /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -m heredoctest <<EOF
   > >>> open('b', 'w').write('this' * 1000)
   > EOF
+  \x1b[?1034h (no-eol) (esc)

A similar problem occurs with test-url.py:

  $ ./run-tests.py test-url.py

  --- .../tests/test-url.py.out
  +++ .../tests/test-url.py.err
  @@ -0,0 +1 @@
  +
  ERROR: .../test-url.py output changed
  !
  Failed test-url.py: output changed
  # Ran 1 tests, 0 skipped, 1 failed.
2011-11-01 12:25:54 -07:00
Mads Kiilerich
6485196281 util: don't encode ':' in url paths
':' has no special meaning in paths, so there is no need for encoding it.

Not encoding ':' makes it easier to test on windows.
2011-11-07 03:25:10 +01:00
Mads Kiilerich
965df356e5 url: really handle urls of the form file:///c:/foo/bar/ correctly
8264e5172141 made sure that paths that seemed to start with a windows drive
letter would not get an extra leading slash.

localpath should thus not try to handle this case by removing a leading slash,
and this special handling is thus removed.

(The localpath handling of this case was wrong anyway, because paths that look
like they start with a windows drive letter can't have a leading slash.)

A quick verification of this is to run 'hg id file:///c:/foo/bar/'.
2011-08-04 02:51:29 +02:00
Nicolas Bareil
db42995f2e sslutil: fall back to commonName when no dNSName in subjectAltName (issue2798)
Any entries in subjectAltName would prevent fallback to using commonName, but
RFC 2818 says:

    If a subjectAltName extension of type dNSName is present, that MUST
    be used as the identity. Otherwise, the (most specific) Common Name
    field in the Subject field of the certificate MUST be used.

We now only consider dNSNames in subjectAltName.

(dNSName is known as 'DNS' in OpenSSL/Python.)
2011-06-18 01:03:03 +02:00
Peter Arrenbrecht
af91571a48 util: make str(url) return file:/// for abs paths again
str(url) was recently changed to return only file:/. However, the
canonical way to represent absolute local paths is file:/// [1], which
is also expected by at least hgsubversion.

Relative paths are returned as file:the/relative/path.

[1] http://en.wikipedia.org/wiki/File_URI_scheme
2011-05-12 16:41:56 +02:00
Augie Fackler
71eea02c91 sslutil: extracted ssl methods from httpsconnection in url.py
This makes it easier to share ssl cert validation with other http
implementations.
2011-05-04 22:08:55 -05:00
Brodie Rao
b7f0d2a103 url: move URL parsing functions into util to improve startup time
The introduction of the new URL parsing code has created a startup
time regression. This is mainly due to the use of url.hasscheme() in
the ui class. It ends up importing many libraries that the url module
requires.

This fix helps marginally, but if we can get rid of the urllib import
in the URL parser all together, startup time will go back to normal.

perfstartup time before the URL refactoring (707e4b1e8064):

! wall 0.050692 comb 0.000000 user 0.000000 sys 0.000000 (best of 100)

current startup time (9ad1dce9e7f4):

! wall 0.070685 comb 0.000000 user 0.000000 sys 0.000000 (best of 100)

after this change:

! wall 0.064667 comb 0.000000 user 0.000000 sys 0.000000 (best of 100)
2011-04-30 09:43:20 -07:00
Brodie Rao
3bbcdd41bc url: be stricter about detecting schemes
While the URL parser is very forgiving about what characters are
allowed in each component, it's useful to be strict about the scheme
so we don't accidentally interpret local paths with colons as URLs.

This restricts schemes to containing alphanumeric characters, dashes,
pluses, and dots (as specified in RFC 2396).
2011-03-31 17:37:33 -07:00
Matt Mackall
d0ca936e58 url: nuke some newly-introduced underbars in identifiers 2011-03-31 10:43:53 -05:00
Brodie Rao
80fd2713db url: abort on file:// URLs with non-localhost hosts 2011-03-30 20:01:31 -07:00
Matt Mackall
b92d665cf7 url: fix tests 2011-03-30 14:42:41 -05:00
Brodie Rao
186bc90ec4 url: provide url object
This adds a url object that re-implements urlsplit() and
unsplit(). The implementation splits out usernames, passwords, and
ports.

The implementation is based on the behavior specified by RFC
2396[1]. However, it is much more forgiving than the RFC's
specification; it places no specific restrictions on what characters
are allowed in each segment of the URL other than what is necessary to
split the URL into its constituent parts.

[1]: http://www.ietf.org/rfc/rfc2396.txt
2011-03-25 22:58:56 -07:00
Yuya Nishihara
593388c52e url: check subjectAltName when verifying ssl certificate
Now it verifies certificate in the same manner as py3k implementation:
http://svn.python.org/view/python/branches/py3k/Lib/ssl.py?view=markup#match_hostname
2011-01-09 00:35:36 +09:00
Yuya Nishihara
ecde2415b3 url: fix UnicodeDecodeError on certificate verification error
SSLSocket.getpeercert() returns tuple containing unicode for 'subject'.
Since Mercurial does't support IDN at all, it just returns error for non-ascii
certname.
2011-01-08 21:52:25 +09:00
Dan Villiom Podlaski Christiansen
f385faac7a *: kill all unnecessary shebangs. 2010-10-26 12:18:39 +02:00
Mads Kiilerich
70b420d9b9 url: validity (notBefore/notAfter) is checked by OpenSSL (issue2407)
Removing the check from our code makes https with cacerts check work with
Python < 2.6.
2010-10-17 04:14:06 +02:00
Augie Fackler
605d8c9578 test-url: skip test when ssl module is unavailable 2010-10-12 11:02:45 -05:00
Augie Fackler
d9ce40564e test-url: remove trailing whitespace 2010-10-12 11:02:05 -05:00
Martin Geisler
1edc738611 test-url: refactor with shorter lines 2010-10-01 16:39:55 +02:00
Mads Kiilerich
916b2a0e20 url: verify correctness of https server certificates (issue2407)
Pythons SSL module verifies that certificates received for HTTPS are valid
according to the specified cacerts, but it doesn't verify that the certificate
is for the host we connect to.

We now explicitly verify that the commonName in the received certificate
matches the requested hostname and is valid for the time being.

This is a minimal patch where we try to fail to the safe side, but we do still
rely on Python's SSL functionality and do not try to implement the standards
fully and correctly. CRLs and subjectAltName are not handled and proxies
haven't been considered.

This change might break connections to some sites if cacerts is specified and
the certificates (by our definition) isn't correct. The workaround is to
disable cacerts which in most cases isn't much worse than it was before with
cacerts.
2010-10-01 00:46:59 +02:00