Skip to content

fix(storage): omit credentials and quota project on signed URL uploads - #15132

Open
Dhriti07 wants to merge 6 commits into
mainfrom
storage-signed-url-upload
Open

Dhriti07 wants to merge 6 commits into
mainfrom
storage-signed-url-upload

Conversation

@Dhriti07

@Dhriti07 Dhriti07 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Send signed URL upload requests (Storage#writer(URL)) without client credentials (Authorization) or quota project (x-goog-user-project) headers. Other headers configured through HeaderProvider, along with timeouts and tracing, stay intact.

…aders

Send signed URL upload requests without client-level headers.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for signed URLs in resumable uploads, ensuring that client headers (such as authorization and custom headers) are excluded when writing to a signed URL. To support this, a new forSignedUrl factory method is added to HttpClientContext, and HttpStorageRpc is updated to manage a dedicated signedUrlRequestFactory. However, a critical issue was identified in HttpClientContext.forSignedUrl: the method accepts a StorageRpc parameter but attempts to call getStorage(), which is not defined on the StorageRpc interface. It is recommended to change the parameter type to HttpStorageRpc directly to ensure compile-time safety.

Dhriti07 and others added 2 commits November 23, 2026 09:50
Signed URL upload requests now keep the headers configured on the client, and omit only the client's credentials and quota project headers.
@Dhriti07
Dhriti07 requested a review from nidhiii-27 December 2, 2026 06:55
@Dhriti07
Dhriti07 marked this pull request as ready for review December 2, 2026 06:56
@Dhriti07
Dhriti07 requested a review from a team as a code owner December 2, 2026 06:56
@Dhriti07
Dhriti07 requested a review from a team as a code owner December 2, 2026 06:56
@Dhriti07
Dhriti07 force-pushed the storage-signed-url-upload branch from cd89171 to 644b865 Compare December 2, 2026 07:22

@nidhiii-27 nidhiii-27 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix — dropping Authorization/x-goog-user-project from signed URL uploads looks correct. Left a few comments inline. Also, could you update the PR title and description to reflect the latest changes? After 04cfa0b, header-provider headers are kept and only credentials and the quota project are dropped, so "without client-level headers" is no longer accurate.

Comment on lines +82 to +89
static HttpClientContext forSignedUrl(StorageRpc storageRpc) {
HttpRequestFactory requestFactory =
storageRpc instanceof HttpStorageRpc
? ((HttpStorageRpc) storageRpc).getSignedUrlRequestFactory()
: storageRpc.getStorage().getRequestFactory().getTransport().createRequestFactory();
return new HttpClientContext(
requestFactory, storageRpc.getStorage().getObjectParser(), Tracing.getTracer());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Medium] For any StorageRpc that isn't an HttpStorageRpc (e.g. one from a custom setServiceRpcFactory), the fallback builds a bare transport.createRequestFactory(). That drops the timeouts, Census tracing, invocation-id/idempotency headers, and x-goog-api-client/user-agent. Before this PR, those callers got from(storageRpc), and no test covers this branch. Could we keep the previous behavior for non-HTTP RPCs?

Suggested change
static HttpClientContext forSignedUrl(StorageRpc storageRpc) {
HttpRequestFactory requestFactory =
storageRpc instanceof HttpStorageRpc
? ((HttpStorageRpc) storageRpc).getSignedUrlRequestFactory()
: storageRpc.getStorage().getRequestFactory().getTransport().createRequestFactory();
return new HttpClientContext(
requestFactory, storageRpc.getStorage().getObjectParser(), Tracing.getTracer());
}
static HttpClientContext forSignedUrl(StorageRpc storageRpc) {
if (!(storageRpc instanceof HttpStorageRpc)) {
return from(storageRpc);
}
HttpStorageRpc httpStorageRpc = (HttpStorageRpc) storageRpc;
return new HttpClientContext(
httpStorageRpc.getSignedUrlRequestFactory(),
httpStorageRpc.getStorage().getObjectParser(),
Tracing.getTracer());
}

Comment on lines +193 to +208
Map<String, String> signedUrlHeaders = signedUrlHeaders(options);
HttpRequestInitializer signedUrlInitializer =
request -> {
if (transportOptions.getConnectTimeout() >= 0) {
request.setConnectTimeout(transportOptions.getConnectTimeout());
}
if (transportOptions.getReadTimeout() >= 0) {
request.setReadTimeout(transportOptions.getReadTimeout());
}
HttpHeaders headers = request.getHeaders();
for (Entry<String, String> e : signedUrlHeaders.entrySet()) {
// headers declared as fields on HttpHeaders (e.g. User-Agent) are list valued
boolean listValued = headers.getClassInfo().getFieldInfo(e.getKey()) != null;
headers.set(e.getKey(), listValued ? ImmutableList.of(e.getValue()) : e.getValue());
}
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Medium] This initializer replaces HttpTransportOptions#getHttpRequestInitializer(ServiceOptions) for signed-URL requests. That method is public and can be overridden; this repo's own it/RequestAuditing overrides it. Any subclass customization (auditing, logging, interceptors) now silently stops applying to Storage#writer(URL). This block also copies the timeout and header handling from core-http, and L222–227 copies getInternalHeaderProviderBuilder. Those copies can drift from the originals. If bypassing is intended, could we document it? For example:

  /**
   * Request factory for requests sent to a signed URL. Requests built from this factory carry no
   * client credentials or quota project and intentionally do not run
   * {@link HttpTransportOptions#getHttpRequestInitializer(ServiceOptions)}; customizations made by
   * subclasses of {@code HttpTransportOptions} therefore do not apply. The timeout and header
   * handling below mirrors that initializer and must be kept in sync with it.
   */
  private final HttpRequestFactory signedUrlRequestFactory;

If you'd rather keep the hook, note that deriving from the core initializer with NoCredentials would trip its universe-domain check for non-default universes.

Comment on lines +228 to +236
ImmutableMap.Builder<String, String> headers = ImmutableMap.builder();
for (Entry<String, String> e :
options.getMergedHeaderProvider(internalHeaderProvider).getHeaders().entrySet()) {
String key = e.getKey();
if (!"authorization".equalsIgnoreCase(key) && !"x-goog-user-project".equalsIgnoreCase(key)) {
headers.put(e);
}
}
return headers.build();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[High] The tests never exercise this authorization / x-goog-user-project filter. The test header provider only supplies x-custom, and the signed-URL assertions pass only because the internal provider omits the quota token and no credentials adapter is installed. Deleting the filter would leave the suite green. It also silently drops headers a user set explicitly through setHeaderProvider, and that isn't documented. Could you either (A) remove it, if dropping user-supplied headers isn't intended:

Suggested change
ImmutableMap.Builder<String, String> headers = ImmutableMap.builder();
for (Entry<String, String> e :
options.getMergedHeaderProvider(internalHeaderProvider).getHeaders().entrySet()) {
String key = e.getKey();
if (!"authorization".equalsIgnoreCase(key) && !"x-goog-user-project".equalsIgnoreCase(key)) {
headers.put(e);
}
}
return headers.build();
return options.getMergedHeaderProvider(internalHeaderProvider).getHeaders();

or (B) keep it, document it in the Javadoc, and cover it in a test (see the comment on ResumableMediaTest.java L126)?

.setProjectId("p")
.setHost("http://localhost")
.setQuotaProjectId("q")
.setHeaderProvider(FixedHeaderProvider.create("x-custom", "v"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[High] To make the filter in signedUrlHeaders pull its weight, have the header provider also supply the headers that should be stripped; assertNoCredentials will then actually exercise it. The non-signed test stays green. Keep X-Goog-User-Project mixed-case, because the merged header provider builds an ImmutableMap and throws on an exact duplicate of the internal lowercase key.

Suggested change
.setHeaderProvider(FixedHeaderProvider.create("x-custom", "v"))
.setHeaderProvider(
FixedHeaderProvider.create(
"x-custom", "v", "Authorization", "Bearer user", "X-Goog-User-Project", "up"))

.getService();
}

private static void assertNoCredentials(RecordingTransport transport) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: assertNoCredentials also checks that x-custom is present and the quota-project header is absent, so a name like assertSignedUrlHeaders would describe it better (update the call sites on L89 and L102). Optionally, the fresh and restored-writer tests could share a parameterized body.

Suggested change
private static void assertNoCredentials(RecordingTransport transport) {
private static void assertSignedUrlHeaders(RecordingTransport transport) {

Comment on lines +144 to +167
private static final class RecordingTransport extends HttpTransport {
private final List<String> methods = Collections.synchronizedList(new ArrayList<>());
private final List<Map<String, List<String>>> headers =
Collections.synchronizedList(new ArrayList<>());

@Override
protected LowLevelHttpRequest buildRequest(String method, String url) {
return new MockLowLevelHttpRequest(url) {
@Override
public MockLowLevelHttpResponse execute() {
methods.add(method);
headers.add(getHeaders());
if (method.equals("POST")) {
return new MockLowLevelHttpResponse()
.setStatusCode(url.contains("Signature=") ? 201 : 200)
.addHeader("Location", "http://localhost/upload?upload_id=u");
}
return new MockLowLevelHttpResponse()
.addHeader("Content-Type", "application/json; charset=UTF-8")
.setContent("{\"bucket\":\"b\",\"name\":\"o\",\"size\":\"10\"}");
}
};
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Medium] This custom transport largely duplicates the existing spi/v1/AuditingHttpTransport fixture. spi/v1/HttpRpcContextTest (doTestInvocationIdNotInSignedURL) already holds the signed-URL writer(URL) header tests and covers both V2 and V4 URLs. These new tests are outside ResumableMediaTest's scope (startUploadForSignedUrl validation) and only use V2. Could you give AuditingHttpTransport a (method, url) -> LowLevelHttpResponse constructor, move these tests next to HttpRpcContextTest, and run them for both V2 and V4?

@Dhriti07 Dhriti07 changed the title fix(storage): send signed URL upload requests without client-level headers fix(storage): omit credentials and quota project on signed URL uploads Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants