Skip to content

Commit 5e26ba8

Browse files
authored
fix(bigquery): validate universe domain on resumable upload RPCs (#15133)
Problem: `HttpBigQueryRpc.openSkipExceptionTranslation` and `writeSkipExceptionTranslation` omitted `validateRPC()`, allowing resumable upload requests with mismatched universe domain credentials to bypass client-side universe domain validation. Solution: Call `validateRPC()` at the start of both methods and add unit tests verifying mismatched universe domain rejection before any HTTP request is executed. b/588887572
1 parent e628795 commit 5e26ba8

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,7 @@ public String open(Job loadJob) {
19751975

19761976
@InternalApi("internal to java-bigquery")
19771977
public String openSkipExceptionTranslation(Job loadJob) throws IOException {
1978+
validateRPC();
19781979
String builder = options.getResolvedApiaryHost("bigquery");
19791980
if (!builder.endsWith("/")) {
19801981
builder += "/";
@@ -2011,6 +2012,7 @@ public Job write(
20112012
public Job writeSkipExceptionTranslation(
20122013
String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last)
20132014
throws IOException {
2015+
validateRPC();
20142016
if (length == 0) {
20152017
return null;
20162018
}

‎java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.google.api.services.bigquery.model.TableDataInsertAllRequest;
4747
import com.google.api.services.bigquery.model.TableReference;
4848
import com.google.cloud.NoCredentials;
49+
import com.google.cloud.bigquery.BigQueryException;
4950
import com.google.cloud.bigquery.BigQueryOptions;
5051
import com.google.cloud.bigquery.DataFormatOptions;
5152
import com.google.cloud.bigquery.DataFormatOptions.TimestampFormatOptions;
@@ -1814,4 +1815,88 @@ public void testExecuteWithSpan_IgnoresHttpResponseException() throws Exception
18141815
verifyNoSpans();
18151816
}
18161817
}
1818+
1819+
@Nested
1820+
class UniverseDomainValidationTests {
1821+
1822+
private HttpBigQueryRpc rpcWithMismatchedUniverseDomain;
1823+
1824+
/** Initializes an {@link HttpBigQueryRpc} with a mismatched universe domain configuration. */
1825+
@BeforeEach
1826+
public void setUp() {
1827+
setUpServer();
1828+
BigQueryOptions options =
1829+
BigQueryOptions.newBuilder()
1830+
.setProjectId(PROJECT_ID)
1831+
.setUniverseDomain("custom-universe.com")
1832+
.setCredentials(NoCredentials.getInstance())
1833+
.setTransportOptions(
1834+
BigQueryOptions.getDefaultHttpTransportOptions().toBuilder()
1835+
.setHttpTransportFactory(() -> mockTransport)
1836+
.build())
1837+
.build();
1838+
rpcWithMismatchedUniverseDomain = new HttpBigQueryRpc(options);
1839+
}
1840+
1841+
@Test
1842+
public void testOpenFailsOnMismatchedUniverseDomain() {
1843+
BigQueryException exception =
1844+
assertThrows(
1845+
BigQueryException.class, () -> rpcWithMismatchedUniverseDomain.open(new Job()));
1846+
1847+
assertEquals(401, exception.getCode());
1848+
assertNull(lastRequestUrl);
1849+
}
1850+
1851+
@Test
1852+
public void testOpenSkipExceptionTranslationFailsOnMismatchedUniverseDomain() {
1853+
BigQueryException exception =
1854+
assertThrows(
1855+
BigQueryException.class,
1856+
() -> rpcWithMismatchedUniverseDomain.openSkipExceptionTranslation(new Job()));
1857+
1858+
assertEquals(401, exception.getCode());
1859+
assertNull(lastRequestUrl);
1860+
}
1861+
1862+
@Test
1863+
public void testWriteFailsOnMismatchedUniverseDomain() {
1864+
BigQueryException exception =
1865+
assertThrows(
1866+
BigQueryException.class,
1867+
() ->
1868+
rpcWithMismatchedUniverseDomain.write(
1869+
"https://bigquery.googleapis.com/upload/bigquery/v2/projects/"
1870+
+ PROJECT_ID
1871+
+ "/jobs?uploadType=resumable",
1872+
new byte[] {1, 2, 3},
1873+
0,
1874+
0,
1875+
3,
1876+
true));
1877+
1878+
assertEquals(401, exception.getCode());
1879+
assertNull(lastRequestUrl);
1880+
}
1881+
1882+
@Test
1883+
public void testWriteSkipExceptionTranslationFailsOnMismatchedUniverseDomain() {
1884+
BigQueryException exception =
1885+
assertThrows(
1886+
BigQueryException.class,
1887+
() ->
1888+
rpcWithMismatchedUniverseDomain.writeSkipExceptionTranslation(
1889+
"https://bigquery.googleapis.com/upload/bigquery/v2/projects/"
1890+
+ PROJECT_ID
1891+
+ "/jobs?uploadType=resumable",
1892+
new byte[] {1, 2, 3},
1893+
0,
1894+
0,
1895+
3,
1896+
true));
1897+
1898+
assertEquals(401, exception.getCode());
1899+
assertNull(lastRequestUrl);
1900+
}
1901+
}
18171902
}

0 commit comments

Comments
 (0)
⌂ 换网址