Jetpack Signature: Support X-Forwarded-Port HTTP header
In some circumstances, when php is executing on a server behind a caching layer or another intermediate server, if the server is on a different port than the public version, the Jetpack signature will be created using the php server's port instead of the public port.
Suggest supporting the X-Forwarded-Port header as one way to handle this.
Further details in 1835697-t
cc: @lezama who has been the rockstar handling signature failings lately.
Context: I am the original source of this request. Our platform puts the runtimes for WP sites in containers on a number of different ports for security reasons, then load-balances in front of them. Other tools use the X-Forwarded-Port and X-Forwarded-Proto headers for this sort of thing.
In the mean-time I'm getting a work-around in place, but it'd be a nice piece of future-proofing to have these headers work natively in Jetpack.
Some general Core discussion context: (unsure if relevant, I'm not terribly familiar with this case)
https://core.trac.wordpress.org/ticket/19337#comment:4 https://core.trac.wordpress.org/ticket/28443
Hey Guys,
For Pantheon is_https() should work as expected. We are carrying the legacy $_SERVER['HTTPS'] superglobal forward. The issue is that we currently expose the actual $_SERVER['HTTP_PORT'] to PHP, which is typically a high/crazy number since it's an ephemeral container.
I think the issue is that Jetpack's code uses that $_SERVER['HTTP_PORT'] variable in signing the request, rather than is_https(). We may be able to work around this by adding a snippit to wp-config.php:
if ( isset( $_SERVER['HTTPS']) && $_SERVER['HTTPS']) {
$_SERVER['HTTP_PORT'] = 443;
}
else {
$_SERVER['HTTP_PORT'] = 80;
}
Checking the X-Forwarded-* headers seems like the right solution, but I think we need to think it through a bit.
In the meantime, you should be able to do the following (for example, in your wp-config.php or some early loading plugin).
define( 'JETPACK_SIGNATURE__HTTP_PORT', $_SERVER['SERVER_PORT'] );
define( 'JETPACK_SIGNATURE__HTTPS_PORT', $_SERVER['SERVER_PORT'] );
I think that will trick Jetpack into checking incoming signed requests correctly in your setup. That's what we do on WordPress.com, so it should be future proof.
Oooh, that's even more perfect. Didn't realize there was a constant we could use. Thanks!
@joshkoenig Long ago, but do you have any sites setup not using the constants and using X-Forwarded that you could help test #3402 using?
I worked around this internally, but can probably re-break it in a test environment so see if it works.
Thanks for getting to this @kraftbj! I'll be able to take a look later this week and will report back.
Fixed in #3402