Skip to content
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

Revert "Validate project-id returned from metadata server (#2822)" #2833

Merged
merged 1 commit into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,7 @@ private static String getAppEngineProjectIdFromMetadataServer() throws IOExcepti
.setReadTimeout(500)
.setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google"));
HttpResponse response = request.execute();
String projectId = response.parseAsString();
return projectId != null && isValidProjectId(projectId)? projectId : null;
return response.parseAsString();
}

protected static String getServiceAccountProjectId() {
Expand All @@ -477,29 +476,6 @@ static String getServiceAccountProjectId(String credentialsPath) {
return project;
}

/*
* Returns true if the projectId is valid. This method checks whether the projectId
* contains only lowercase letters, digits and hyphens, starts with a lowercase letter
* and does not end with a hyphen, but does not check the length of projectId. This
* method is primarily used to protect against DNS hijacking.
*/
static boolean isValidProjectId(String projectId) {
for (char c : projectId.toCharArray()) {
if (!isLowerCase(c) && !isDigit(c) && c != '-') {
return false;
}
}
return projectId.length() > 0 && isLowerCase(projectId.charAt(0))
&& !projectId.endsWith("-");
}

private static boolean isLowerCase(char c) {
return c >= 'a' && c <= 'z';
}

private static boolean isDigit(char c) {
return c >= '0' && c <= '9';
}

/**
* Returns a Service object for the current service. For instance, when using Google Cloud
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static com.google.common.truth.Truth.assertThat;

import com.google.api.core.ApiClock;
import com.google.api.core.CurrentMillisClock;
Expand Down Expand Up @@ -314,13 +313,4 @@ public void testGetServiceAccountProjectId_nonExistentFile() throws Exception {

assertNull(ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath()));
}

@Test
public void testValidateProjectId() throws Exception {
assertThat(ServiceOptions.isValidProjectId("abc-123")).isTrue();
assertThat(ServiceOptions.isValidProjectId("abc-123-ab")).isTrue();
assertThat(ServiceOptions.isValidProjectId("abc=123")).isFalse();
assertThat(ServiceOptions.isValidProjectId("abc123-")).isFalse();
assertThat(ServiceOptions.isValidProjectId("1abc-23")).isFalse();
}
}