-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add Expiring Token to Endpoint #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7311945
acae533
4313fb1
1376d1c
15c037b
5a1df6c
4571a30
2448507
1b24166
5352583
78deee4
83e188f
faf5146
bd4840c
e0625ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,39 @@ public static function query_var($vars) { | |
| return $vars; | ||
| } | ||
|
|
||
| /** | ||
| * generate a valid expire code. | ||
| * Three possible values are valid at any one time, ticks: 0, 1, or 2 | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function expire_code( $tick = 0 ) { | ||
| $action = 'web mention endpoint'; | ||
| $time_format = 'Y-m-d a'; | ||
| $time_block = 12 * HOUR_IN_SECONDS; | ||
| $tick = abs( intval( $tick ) ); | ||
| if ( 3 < $tick ) { | ||
| // something wrong, tick too high/ | ||
| // use default | ||
| $tick = 0; | ||
| } | ||
| $expire_code = date( $time_format, time() - ( $tick * $time_block ) ); | ||
|
|
||
| // always use logged out user code, endpoint may be looked up by a logged in user | ||
| // while the web mention comes from a logged out user (using curl or similar) | ||
| $uid = 0; | ||
|
|
||
| // as above, always use the lgoged out token. | ||
| $token = ''; | ||
|
|
||
| // custom hash used rather than standard nonce to prevent session data polluting the | ||
| // web mention endpoint. The endpoint needs to remain the same for all users. | ||
| $expire_code = wp_hash( $expire_code . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks very similar to the code in Ideally, long term, a patch to WordPress core that would allow a stateless
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ok, here is the problem https://core.trac.wordpress.org/browser/tags/4.1/src/wp-includes/pluggable.php#L1750 the logged out code is only called if there is no session! That is indeed a problem!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment added, pushing shortly. |
||
|
|
||
| return $expire_code; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Parse the WebMention request and render the document | ||
| * | ||
|
|
@@ -81,13 +114,34 @@ public static function parse_query($wp) { | |
| if (!array_key_exists('webmention', $wp->query_vars)) { | ||
| return; | ||
| } | ||
|
|
||
| $content = file_get_contents('php://input'); | ||
| parse_str($content); | ||
| else { | ||
| // check if the end point has expired | ||
| $valid_ticks = array( 0, -1, -2 ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are numbers negative here? And why have an array of numbers combined with a
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion would be:
What are you thinking? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the thinking, but the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WP native uses negative numbers when generating the tick, I wanted to match. Per voxpelli on session data. |
||
|
|
||
| $supplied_code = get_query_var( 'webmention' ); | ||
| $is_valid = false; | ||
|
|
||
| foreach ( $valid_ticks as $tick ) { | ||
| if ( hash_equals( WebMentionPlugin::expire_code( $tick ), $supplied_code ) ) { | ||
| $is_valid = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // plain text header | ||
| header('Content-Type: text/plain; charset=' . get_option('blog_charset')); | ||
|
|
||
| // fail if invalide endpoint | ||
| if ( false == $is_valid ) { | ||
| status_header(403); | ||
| echo "invalid endpoint"; | ||
| exit; | ||
| } | ||
|
|
||
| $content = file_get_contents('php://input'); | ||
| parse_str($content); | ||
|
|
||
| // check if source url is transmitted | ||
| if (!isset($source)) { | ||
| status_header(400); | ||
|
|
@@ -556,17 +610,19 @@ public static function discover_endpoint($url) { | |
| */ | ||
| public static function html_header() { | ||
| // backwards compatibility with v0.1 | ||
| echo '<link rel="http://webmention.org/" href="'.site_url("?webmention=endpoint").'" />'."\n"; | ||
| echo '<link rel="webmention" href="'.site_url("?webmention=endpoint").'" />'."\n"; | ||
| $endpoint_code = WebMentionPlugin::expire_code(); | ||
| echo '<link rel="http://webmention.org/" href="'.site_url("?webmention=" . $endpoint_code ).'" />'."\n"; | ||
| echo '<link rel="webmention" href="'.site_url("?webmention=" . $endpoint_code ).'" />'."\n"; | ||
| } | ||
|
|
||
| /** | ||
| * The WebMention autodicovery http-header | ||
| */ | ||
| public static function http_header() { | ||
| // backwards compatibility with v0.1 | ||
| header('Link: <'.site_url("?webmention=endpoint").'>; rel="http://webmention.org/"', false); | ||
| header('Link: <'.site_url("?webmention=endpoint").'>; rel="webmention"', false); | ||
| $endpoint_code = WebMentionPlugin::expire_code(); | ||
| header('Link: <'.site_url("?webmention=" . $endpoint_code).'>; rel="http://webmention.org/"', false); | ||
| header('Link: <'.site_url("?webmention=" . $endpoint_code).'>; rel="webmention"', false); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to not use
wp_nonce_tick()instead of calculating the tick from scratch? Not sure what's the best – but perhaps add a comment with a reason for why you chose what you did would be good?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nonce_life is filterable in wp_tick & I wanted to ensure it was kept @ twelve hours.
Would like to know your thoughts on this @pfefferle @voxpelli
P