mirror of
https://github.com/google/nomulus.git
synced 2025-05-02 13:07:50 +02:00
This follows up on Brian's work to transition not just to a new format with an empty scope value, but instead to replace the existing format entirely with a new one that: 1) includes a version number to support future format migrations 2) doesn't include a field for the scope at all, since scoping the tokens adds no real security benefit and just makes verification more difficult 3) replaces the raw SHA-256 hash with a SHA-256 HMAC instead, as a best practice to avoid length-extension attacks [1], even though in our particular case they would only be able to extend the timestamp and would thus be relatively innocuous The new format will be produced by calling generateToken(), and the scope-accepting version is renamed to generateLegacyToken() in addition to its existing deprecation, for maximum clarity. I changed the validateToken() logic to stop accepting a scope entirely; when validating a legacy-style token, we'll test it against the two existing legacy scope values ("admin" and "console") and accept it if it matches either one. Note that this means the xsrfScope parameter in @Action is now wholly obsolete; I'll remove it in a follow-up to avoid bringing extra files into this CL. After this CL hits production, the next one will replace all calls to generateLegacyToken() with generateToken(). Once that CL is deployed, the last step will be removing the legacy fallback in validateToken(). [1] See https://en.wikipedia.org/wiki/Length_extension_attack ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=148936805
160 lines
6 KiB
Java
160 lines
6 KiB
Java
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package google.registry.tools;
|
|
|
|
import static com.google.common.base.Suppliers.memoize;
|
|
import static com.google.common.net.HttpHeaders.X_REQUESTED_WITH;
|
|
import static com.google.common.net.MediaType.JSON_UTF_8;
|
|
import static google.registry.security.JsonHttp.JSON_SAFETY_PREFIX;
|
|
import static google.registry.security.XsrfTokenManager.X_CSRF_TOKEN;
|
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
|
|
import com.google.api.client.http.ByteArrayContent;
|
|
import com.google.api.client.http.GenericUrl;
|
|
import com.google.api.client.http.HttpHeaders;
|
|
import com.google.api.client.http.HttpRequest;
|
|
import com.google.api.client.http.HttpRequestFactory;
|
|
import com.google.api.client.http.HttpResponse;
|
|
import com.google.api.client.http.HttpUnsuccessfulResponseHandler;
|
|
import com.google.common.base.Supplier;
|
|
import com.google.common.collect.ImmutableList;
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.common.io.CharStreams;
|
|
import com.google.common.net.HostAndPort;
|
|
import com.google.common.net.MediaType;
|
|
import com.google.re2j.Matcher;
|
|
import com.google.re2j.Pattern;
|
|
import google.registry.security.XsrfTokenManager;
|
|
import google.registry.tools.ServerSideCommand.Connection;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.util.Map;
|
|
import javax.inject.Inject;
|
|
import org.json.simple.JSONValue;
|
|
|
|
/** An http connection to the appengine server. */
|
|
class AppEngineConnection implements Connection {
|
|
|
|
/** Pattern to heuristically extract title tag contents in HTML responses. */
|
|
private static final Pattern HTML_TITLE_TAG_PATTERN = Pattern.compile("<title>(.*?)</title>");
|
|
|
|
@Inject HttpRequestFactory requestFactory;
|
|
@Inject AppEngineConnectionFlags flags;
|
|
@Inject XsrfTokenManager xsrfTokenManager;
|
|
|
|
@Inject
|
|
AppEngineConnection() {}
|
|
|
|
/**
|
|
* Memoized XSRF security token.
|
|
*
|
|
* <p>Computing this is expensive since it needs to load {@code ServerSecret} so do it once.
|
|
*/
|
|
private final Supplier<String> xsrfToken =
|
|
memoize(new Supplier<String>() {
|
|
@Override
|
|
public String get() {
|
|
return xsrfTokenManager.generateLegacyToken("admin", getUserId());
|
|
}});
|
|
|
|
@Override
|
|
public void prefetchXsrfToken() throws IOException {
|
|
// Cause XSRF token to be fetched, and then stay resident in cache (since it's memoized).
|
|
xsrfToken.get();
|
|
}
|
|
|
|
/** Returns the contents of the title tag in the given HTML, or null if not found. */
|
|
private static String extractHtmlTitle(String html) {
|
|
Matcher matcher = HTML_TITLE_TAG_PATTERN.matcher(html);
|
|
return (matcher.find() ? matcher.group(1) : null);
|
|
}
|
|
|
|
/** Returns the HTML from the connection error stream, if any, otherwise the empty string. */
|
|
private static String getErrorHtmlAsString(HttpResponse response) throws IOException {
|
|
return CharStreams.toString(new InputStreamReader(response.getContent(), UTF_8));
|
|
}
|
|
|
|
@Override
|
|
public String send(
|
|
String endpoint, Map<String, ?> params, MediaType contentType, byte[] payload)
|
|
throws IOException {
|
|
GenericUrl url = new GenericUrl(String.format("%s%s", getServerUrl(), endpoint));
|
|
url.putAll(params);
|
|
HttpRequest request =
|
|
requestFactory.buildPostRequest(url, new ByteArrayContent(contentType.toString(), payload));
|
|
HttpHeaders headers = request.getHeaders();
|
|
headers.setCacheControl("no-cache");
|
|
headers.put(X_CSRF_TOKEN, ImmutableList.of(xsrfToken.get()));
|
|
headers.put(X_REQUESTED_WITH, ImmutableList.of("RegistryTool"));
|
|
request.setHeaders(headers);
|
|
request.setFollowRedirects(false);
|
|
request.setThrowExceptionOnExecuteError(false);
|
|
request.setUnsuccessfulResponseHandler(
|
|
new HttpUnsuccessfulResponseHandler() {
|
|
@Override
|
|
public boolean handleResponse(
|
|
HttpRequest request, HttpResponse response, boolean supportsRetry)
|
|
throws IOException {
|
|
String errorTitle = extractHtmlTitle(getErrorHtmlAsString(response));
|
|
throw new IOException(
|
|
String.format(
|
|
"Error from %s: %d %s%s",
|
|
request.getUrl().toString(),
|
|
response.getStatusCode(),
|
|
response.getStatusMessage(),
|
|
(errorTitle == null ? "" : ": " + errorTitle)));
|
|
}
|
|
});
|
|
HttpResponse response = null;
|
|
try {
|
|
response = request.execute();
|
|
return CharStreams.toString(new InputStreamReader(response.getContent(), UTF_8));
|
|
} finally {
|
|
if (response != null) {
|
|
response.disconnect();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("unchecked")
|
|
public Map<String, Object> sendJson(String endpoint, Map<String, ?> object) throws IOException {
|
|
String response = send(
|
|
endpoint,
|
|
ImmutableMap.<String, Object>of(),
|
|
JSON_UTF_8,
|
|
JSONValue.toJSONString(object).getBytes(UTF_8));
|
|
return (Map<String, Object>) JSONValue.parse(response.substring(JSON_SAFETY_PREFIX.length()));
|
|
}
|
|
|
|
@Override
|
|
public String getServerUrl() {
|
|
return (isLocalhost() ? "http://" : "https://") + getServer().toString();
|
|
}
|
|
|
|
HostAndPort getServer() {
|
|
return flags.getServer().withDefaultPort(443); // Default to HTTPS port if unspecified.
|
|
}
|
|
|
|
boolean isLocalhost() {
|
|
return flags.getServer().getHost().equals("localhost");
|
|
}
|
|
|
|
private String getUserId() {
|
|
return isLocalhost()
|
|
? UserIdProvider.getTestUserId()
|
|
: UserIdProvider.getProdUserId();
|
|
}
|
|
}
|