toscho.design

WordPress: Internen User-Agent ändern

Bei Requests, die WordPress sendet, verwendet es einen eigenen User-Agent-String, der recht … gesprächig ist. Wir finden ihn in der Klasse WP_Http in wp-includes/class-http.php:

'user-agent' => apply_filters( 
    'http_headers_useragent', 
    'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )  
),

Wir können also mit dem Filter http_headers_useragent diesen String ändern.
Das hilft allerdings nichts, wenn das Objekt mit einem anderen UA-String aufgerufen wird. Die Funktion wp_version_check() tut das beispielsweise – und sie sendet einige Informationen, die man vielleicht lieber nicht anderen anvertrauen möchte.

Der besser geeignete Filter ist daher http_request_args, denn er enthält den tatsächlich verwendeten String.

GitHub: https://gist.github.com/3417285

add_filter( 'http_request_args', 't5_anonymize_ua_string' );
/**
 * Replace the UA string.
 *
 * @param  array $args Request arguments
 * @return array
 */
function t5_anonymize_ua_string( $args )
{
    global $wp_version;
    $args['user-agent'] = 'WordPress/' . $wp_version;
    // catch data set by wp_version_check()
    if ( isset ( $args['headers']['wp_install'] ) )
    {
        $args['headers']['wp_install'] = 'http://example.com';
        $args['headers']['wp_blog']    = 'http://example.com';
    }
    return $args;
}

Wozu das Ganze? wp_version_check() sendet beispielsweise neben der URL noch die Zahl der User und der Blogs einer Multisite-Installation. Betreibt man eine Website, die rechtlichen oder staatlichen Angriffen ausgesetzt sein könnte, oder bei der die Kunden als Nutzer verwaltet werden, so sind das Daten, die man vielleicht lieber nicht streut.

7 Kommentare

  1. Matthias am 21.08.2012 · 20:14

    Warum sollte man das tun, magst Du dies bitte erläutern.... =D Danke dir , gibt immer etwas interessantes bei deinem Blog.

  2. Thomas Scholz am 21.08.2012 · 20:29

    @Matthias: Ich habe noch einen Absatz angehängt.

  3. Matthias am 22.08.2012 · 12:26

    Danke Thomas :-)

  4. Moni am 04.09.2012 · 11:06

    Hello, thanks for the information. I found your post from the stackexchange community. The information was new to me and I never expected such a privacy violation from wordpress. I love that software. Can you explain me about this?

  5. Thomas Scholz am 04.09.2012 · 11:17

    @Moni: Ask that question on the Trac ticket. I have never seen an official explanation why this is really needed in such detail.

  6. Moni am 04.09.2012 · 11:30

    @Thomas: thanks for the answer. I have gone through multiple forums and discussions after seeing this news today morning. But that was not my question. I was asking your help for the code you posted on stackexchange. Can you edit your answer so that, we can see the complete t5_anonymize_ua_string() function needed to remove site URL?

  7. Thomas Scholz am 04.09.2012 · 11:58

    @Moni: That’s why I created the patch in that ticket: You can stop update checks completely, you can change the User Agent, but you cannot filter just the URL with the attached data about the blog.