Commit Graph

65 Commits

Author SHA1 Message Date
Gregory Szorc
64e2de02bd hgweb: extract path traversal checking into standalone function
A common exploit in web applications that access paths is to insert
path separator strings like ".." to try to get the server to serve up
files it shouldn't.

We have code for detecting this in staticfile(). A subsequent commit
will need to perform this test as well. Since this is security code,
let's factor the check so we don't have to reinvent the wheel.
2017-03-31 21:47:26 -07:00
Gregory Szorc
bfa11ec1e0 hgweb: use context manager for file I/O 2017-03-31 22:30:38 -07:00
Pierre-Yves David
50e7f5d5fd hgweb: explicitly tests for None
Changeset 11e325d162fe removed the mutable default value, but did not explicitly
tested for None. Such implicit testing can introduce semantic and performance
issue. We move to an explicit testing for None as recommended by PEP8:

https://www.python.org/dev/peps/pep-0008/#programming-recommendations
2017-03-15 15:11:04 -07:00
Gregory Szorc
5cc9a634fe hgweb: don't use mutable default argument value 2016-12-26 16:55:47 -07:00
Gregory Szorc
9849c580fb hgweb: support Content Security Policy
Content-Security-Policy (CSP) is a web security feature that allows
servers to declare what loaded content is allowed to do. For example,
a policy can prevent loading of images, JavaScript, CSS, etc unless
the source of that content is whitelisted (by hostname, URI scheme,
hashes of content, etc). It's a nifty security feature that provides
extra mitigation against some attacks, notably XSS.

Mitigation against these attacks is important for Mercurial because
hgweb renders repository data, which is commonly untrusted. While we
make attempts to escape things, etc, there's the possibility that
malicious data could be injected into the site content. If this happens
today, the full power of the web browser is available to that
malicious content. A restrictive CSP policy (defined by the server
operator and sent in an HTTP header which is outside the control of
malicious content), could restrict browser capabilities and mitigate
security problems posed by malicious data.

CSP works by emitting an HTTP header declaring the policy that browsers
should apply. Ideally, this header would be emitted by a layer above
Mercurial (likely the HTTP server doing the WSGI "proxying"). This
works for some CSP policies, but not all.

For example, policies to allow inline JavaScript may require setting
a "nonce" attribute on <script>. This attribute value must be unique
and non-guessable. And, the value must be present in the HTTP header
and the HTML body. This means that coordinating the value between
Mercurial and another HTTP server could be difficult: it is much
easier to generate and emit the nonce in a central location.

This commit introduces support for emitting a
Content-Security-Policy header from hgweb. A config option defines
the header value. If present, the header is emitted. A special
"%nonce%" syntax in the value triggers generation of a nonce and
inclusion in <script> elements in templates. The inclusion of a
nonce does not occur unless "%nonce%" is present. This makes this
commit completely backwards compatible and the feature opt-in.

The nonce is a type 4 UUID, which is the flavor that is randomly
generated. It has 122 random bits, which should be plenty to satisfy
the guarantees of a nonce.
2017-01-10 23:37:08 -08:00
Pulkit Goyal
bb08d44667 py3: replace os.environ with encoding.environ (part 3 of 5) 2016-12-18 01:54:36 +05:30
Pulkit Goyal
c109648881 py3: replace os.altsep with pycompat.altsep
All the occurences of os.altsep are replaced with pycompat.altsep which
returns bytes.
2016-12-18 01:17:12 +05:30
Pulkit Goyal
5629f71f5b py3: replace os.sep with pycompat.ossep (part 3 of 4) 2016-12-17 20:14:24 +05:30
Pulkit Goyal
6b3bc52b40 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
The BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer has been merged into
http.server in python 3. All of them has been merged as util.httpserver to use
in both python 2 and 3. This patch adds a regex to check-code to warn against
the use of BaseHTTPServer. Moreover this patch also includes updates to lower
part of test-check-py3-compat.t which used to remain unchanged.
2016-07-13 23:38:29 +05:30
Anton Shestakov
662db21d2e hgweb: emit a valid, weak ETag
Previously, ETag headers from hgweb weren't correctly formed, because rfc2616
(section 14, header definitions) requires double quotes around the content of
the header. str(web.mtime) didn't do that.

Additionally, strong ETags signify that the resource representations are
byte-for-byte identical. That is, they can be reconstructed from byte ranges if
client so wishes. Considering ETags for all hgweb pages is just mtime of
00changelog.i and doesn't consider of e.g. .hg/hgrc with description, contact
and other fields, it's clearly shouldn't be strong. The W/ prefix marks it as
weak, which still allows caching the whole served file/page, but doesn't allow
byte-range requests.
2016-07-09 03:26:24 +08:00
Yuya Nishihara
47690f822c hgweb: use absolute_import 2015-10-31 22:07:40 +09:00
Yuya Nishihara
3a2ca88d5a hgweb: import BaseHTTPServer as module at top level
This will avoid future warning spotted by the import checker.
2015-11-01 15:07:08 +09:00
timeless@mozdev.org
9059b84547 hgweb: remove ErrorResponse.message
BaseException.message is deprecated:
https://www.python.org/dev/peps/pep-0352/#retracted-ideas
2015-09-08 14:56:29 -04:00
Pierre-Yves David
a6ec11b75b hgweb: drop the default argument for get_stat
This default argument is used twice and is making things confusing. Making it
explicit helps to clarify coming changesets
2015-07-03 10:07:51 -07:00
Gregory Szorc
5380dea2a7 global: mass rewrite to use modern exception syntax
Python 2.6 introduced the "except type as instance" syntax, replacing
the "except type, instance" syntax that came before. Python 3 dropped
support for the latter syntax. Since we no longer support Python 2.4 or
2.5, we have no need to continue supporting the "except type, instance".

This patch mass rewrites the exception syntax to be Python 2.6+ and
Python 3 compatible.

This patch was produced by running `2to3 -f except -w -n .`.
2015-06-23 22:20:08 -07:00
Anton Shestakov
1119de7215 hgweb: refresh hgweb.repo on phase change (issue4061)
Make hgweb.refresh() also look at phaseroots file (in addition to 00changelog.i
file) and reload the repo when os.stat returns different mtime or size than
cached, signifying the file was modified.

This way if user changes phase of a changeset (secret <-> draft), there's no
need to restart hg serve to see the change.
2014-09-27 21:59:55 +09:00
Mads Kiilerich
eabc047878 spelling: random spell checker fixes 2013-10-24 01:49:56 +08:00
Wagner Bruna
acdea706bb hgweb: refactor checks for granting and revoking user permissions
Provides an entry point for extensions implementing more complex
authorization schemes.

Original patch proposed by Markus Zapke-Gründemann.
2013-04-15 18:57:04 -03:00
Mads Kiilerich
e4f8deae3f hgweb: simplify internal staticfile return codes 2013-02-10 18:24:29 +01:00
Mads Kiilerich
202753eeb5 hgweb: pass the actual response body to request.response, not just the length
This makes it less likely to send a response that doesn't match Content-Length.
2013-01-15 01:07:03 +01:00
Yuya Nishihara
ee6b575238 hgweb: respond 403 forbidden for ssl required error
It's preferable to report "ssl required" as an error, so that the client
can detect error and exit with 255. Currently hg exits with 1, which is
"nothing to push."
2012-09-05 23:59:27 +09:00
Brodie Rao
92158e04de cleanup: "raise SomeException()" -> "raise SomeException" 2012-05-12 16:00:58 +02:00
Martin Geisler
e57b3a9f70 hgweb: initialize permhooks at definition time
This is simpler than creating it empty and then appending the default
checkauthz hook 50 lines below.
2011-04-30 13:47:22 +02:00
Martin Geisler
5a3f4fec03 merge with stable 2011-04-19 15:19:54 +02:00
Martin Geisler
6cd55d2029 hgweb: detect change based on changelog size too
Before, there was a race between an access and a modification made
within the same second.
2011-04-19 15:15:56 +02:00
Mads Kiilerich
4a3ec69835 hgweb: give ErrorResponse a descriptive string/Exception representation
Very handy if the exception should appear in output.
2011-02-21 00:52:26 +01:00
Dan Villiom Podlaski Christiansen
ec590d5cd4 explicitly close files
Add missing calls to close() to many places where files are
opened. Relying on reference counting to catch them soon-ish is not
portable and fails in environments with a proper GC, such as PyPy.
2010-12-24 15:23:01 +01:00
Dirkjan Ochtman
fcd515c23a hgweb: support very simple caching model (issue1845) 2010-09-08 15:23:48 +02:00
Matt Mackall
cc6de8544b hgweb: drop use of super() for Exception base class
In Python 2.4, Exception is an old-style class and doesn't work with super.
2011-03-14 15:28:56 -05:00
Dirkjan Ochtman
0af26a9d11 don't depend on features of Exception as much
BaseException.message has been deprecated, so we try to not rely on it.
2011-03-12 15:51:39 +01:00
Augie Fackler
c986a6137a hgweb: add support for 100-continue as recommended by PEP 333. 2010-02-06 04:27:28 -06:00
Matt Mackall
cd3ef170f7 Merge with stable 2010-01-19 22:45:09 -06:00
Matt Mackall
595d66f424 Update license to GPLv2+ 2010-01-19 22:20:08 -06:00
Sune Foldager
ee001cdc90 hgweb: send proper error messages to the client
Fixes a bug in protocol which caused an exception during exception handling in
some cases on Windows. Also makes sure the server error message is correctly
propagated to the client, instead of being thrown away.
2009-11-02 10:20:04 +01:00
Alejandro Santos
77d606ac51 compat: use open() instead of file() everywhere 2009-07-05 11:01:30 +02:00
timeless
fb33de67af Generally replace "file name" with "filename" in help and comments. 2009-06-09 09:25:17 -04:00
Brendan Cully
5779dfb1e0 hgweb: Make get_mtime use repository to find store path.
It was calculating it directly, which is redundant and caused
it to break with shared repositories.
2009-12-15 12:33:04 -08:00
Sune Foldager
69c1e615e4 hgweb: add support for extension-provided permission hooks
This allows extensions to hook into permission checking, providing both
authentication and authorization as needed. The existing authorization
function has been changed to a hook, which is added by default.
2009-11-23 11:03:55 +01:00
Martin Geisler
750183bdad updated license to be explicit about GPL version 2 2009-04-26 01:08:54 +02:00
Dirkjan Ochtman
0b1a57839b templater: move stylemap function from hgweb to templater 2009-04-04 17:46:11 +02:00
Sune Foldager
190a26b26d hgweb: support custom http headers in ErrorResponse 2009-02-09 11:31:52 +01:00
Dirkjan Ochtman
5e743c6696 hgweb: move the diffs() generator into webutil 2008-11-03 20:41:48 +01:00
Brendan Cully
03a69e59f7 Allow per-file shadowing of static directory in templatepath 2008-10-28 22:24:17 -07:00
Brendan Cully
34868ca5be Allow hgweb to search for templates in more than one path.
This patch is constructed to make it easy for external extensions to
provide their own templates, by updating templater.path.
2008-10-17 11:34:31 -07:00
Rocco Rutte
f70a5a0f4e hgweb: Respond with HTTP 403 for disabled archive types instead of 404
This makes it easier for clients/users to distinct between supported
but disabled and unsupported archive types.
2008-09-05 17:28:37 +02:00
Dirkjan Ochtman
b3dea85ba6 hgweb: raise ErrorResponses to communicate protocol errors 2008-07-22 18:23:20 +02:00
Dirkjan Ochtman
793235765f hgweb: generate error message only if nothing is passed 2008-07-22 18:21:37 +02:00
Thomas Arendsen Hein
ad12af151f merged Edward Lee's line anchors patch 2008-02-15 19:44:54 +01:00
Edward Lee
9f8402d3c0 Add line anchors to annotate, changeset, diff, file views for hgweb 2007-09-04 22:25:37 -05:00
Dirkjan Ochtman
3d668210f2 hgweb: explicit response status 2008-02-01 10:31:13 +01:00