-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
core: Do not leak server state when application callbacks throw exceptions #3064
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a1db4c6
Make error detail transmissions opt in
zpencer d0a2ea3
Rename syncListener -> listener
zpencer ab44d1f
Move interceptor to utils package, make SynchronizedServerCall a stat…
zpencer 027e994
Move unit tests
zpencer 65e4d61
update for review comments
zpencer b65e09f
review comments
zpencer 0514d6a
fix null trailers, do test field inits in declarations
zpencer 7eb4eab
move test to util package
zpencer File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
260 changes: 260 additions & 0 deletions
260
core/src/main/java/io/grpc/util/TransmitStatusRuntimeExceptionInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
/* | ||
* Copyright 2017, gRPC 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 io.grpc.util; | ||
|
||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.google.common.util.concurrent.SettableFuture; | ||
import io.grpc.Attributes; | ||
import io.grpc.ExperimentalApi; | ||
import io.grpc.ForwardingServerCall; | ||
import io.grpc.ForwardingServerCallListener; | ||
import io.grpc.Metadata; | ||
import io.grpc.ServerCall; | ||
import io.grpc.ServerCallHandler; | ||
import io.grpc.ServerInterceptor; | ||
import io.grpc.Status; | ||
import io.grpc.StatusRuntimeException; | ||
import io.grpc.internal.SerializingExecutor; | ||
import java.util.concurrent.ExecutionException; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* A class that intercepts uncaught exceptions of type {@link StatusRuntimeException} and handles | ||
* them by closing the {@link ServerCall}, and transmitting the exception's status and metadata | ||
* to the client. | ||
* | ||
* <p>Without this interceptor, gRPC will strip all details and close the {@link ServerCall} with | ||
* a generic {@link Status#UNKNOWN} code. | ||
* | ||
* <p>Security warning: the {@link Status} and {@link Metadata} may contain sensitive server-side | ||
* state information, and generally should not be sent to clients. Only install this interceptor | ||
* if all clients are trusted. | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2189") | ||
public final class TransmitStatusRuntimeExceptionInterceptor implements ServerInterceptor { | ||
private TransmitStatusRuntimeExceptionInterceptor() { | ||
} | ||
|
||
public static ServerInterceptor instance() { | ||
return new TransmitStatusRuntimeExceptionInterceptor(); | ||
} | ||
|
||
@Override | ||
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( | ||
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { | ||
final ServerCall<ReqT, RespT> serverCall = new SerializingServerCall<ReqT, RespT>(call); | ||
ServerCall.Listener<ReqT> listener = next.startCall(serverCall, headers); | ||
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(listener) { | ||
@Override | ||
public void onMessage(ReqT message) { | ||
try { | ||
super.onMessage(message); | ||
} catch (StatusRuntimeException e) { | ||
closeWithException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onHalfClose() { | ||
try { | ||
super.onHalfClose(); | ||
} catch (StatusRuntimeException e) { | ||
closeWithException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCancel() { | ||
try { | ||
super.onCancel(); | ||
} catch (StatusRuntimeException e) { | ||
closeWithException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
try { | ||
super.onComplete(); | ||
} catch (StatusRuntimeException e) { | ||
closeWithException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onReady() { | ||
try { | ||
super.onReady(); | ||
} catch (StatusRuntimeException e) { | ||
closeWithException(e); | ||
} | ||
} | ||
|
||
private void closeWithException(StatusRuntimeException t) { | ||
serverCall.close(t.getStatus(), t.getTrailers()); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* A {@link ServerCall} that wraps around a non thread safe delegate and provides thread safe | ||
* access by serializing everything on an executor. | ||
*/ | ||
private static class SerializingServerCall<ReqT, RespT> extends | ||
ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT> { | ||
private static final String ERROR_MSG = "Encountered error during serialized access"; | ||
private final SerializingExecutor serializingExecutor = | ||
new SerializingExecutor(MoreExecutors.directExecutor()); | ||
|
||
SerializingServerCall(ServerCall<ReqT, RespT> delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public void sendMessage(final RespT message) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.sendMessage(message); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void request(final int numMessages) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.request(numMessages); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void sendHeaders(final Metadata headers) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.sendHeaders(headers); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void close(final Status status, final Metadata trailers) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.close(status, trailers); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
final SettableFuture<Boolean> retVal = SettableFuture.create(); | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
retVal.set(SerializingServerCall.super.isReady()); | ||
} | ||
}); | ||
try { | ||
return retVal.get(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isCancelled() { | ||
final SettableFuture<Boolean> retVal = SettableFuture.create(); | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
retVal.set(SerializingServerCall.super.isCancelled()); | ||
} | ||
}); | ||
try { | ||
return retVal.get(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void setMessageCompression(final boolean enabled) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.setMessageCompression(enabled); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void setCompression(final String compressor) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.setCompression(compressor); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Attributes getAttributes() { | ||
final SettableFuture<Attributes> retVal = SettableFuture.create(); | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
retVal.set(SerializingServerCall.super.getAttributes()); | ||
} | ||
}); | ||
try { | ||
return retVal.get(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getAuthority() { | ||
final SettableFuture<String> retVal = SettableFuture.create(); | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
retVal.set(SerializingServerCall.super.getAuthority()); | ||
} | ||
}); | ||
try { | ||
return retVal.get(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getTrailers may return null, but close() requires non-null Metadata