Skip to content

Commit

Permalink
[lgwebos] Mask access key
Browse files Browse the repository at this point in the history
Fix #7758

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed May 24, 2020
1 parent a7f6768 commit 982c68c
Showing 1 changed file with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,13 @@ public void onError(String message) {
}
};

// Mask the key in the saved request
JsonObject packetWithMaskedToken = packet.deepCopy();
payload = packetWithMaskedToken.getAsJsonObject("payload");
payload.remove("client-key");
payload.addProperty("client-key", "***");
this.requests.put(id, new ServiceSubscription<>("dummy", payload, x -> x, dummyListener));
sendMessage(packet);
sendMessage(packet, packetWithMaskedToken);
}

private int nextRequestId() {
Expand Down Expand Up @@ -361,14 +366,19 @@ public void unsubscribe(ServiceSubscription<?> subscription) {
}

private void sendMessage(JsonObject json) {
sendMessage(json, null);
}

private void sendMessage(JsonObject json, @Nullable JsonObject jsonToLog) {
String msg = GSON.toJson(json);
Session s = this.session;
try {
if (s != null) {
logger.trace("Message [out]: {}", msg);
logger.trace("Message [out]: {}", jsonToLog != null ? GSON.toJson(jsonToLog) : msg);
s.getRemote().sendString(msg);
} else {
logger.warn("No Connection to TV, skipping [out]: {}", msg);
logger.warn("No Connection to TV, skipping [out]: {}",
jsonToLog != null ? GSON.toJson(jsonToLog) : msg);
}
} catch (IOException e) {
logger.warn("Unable to send message.", e);
Expand All @@ -377,8 +387,11 @@ private void sendMessage(JsonObject json) {

@OnWebSocketMessage
public void onMessage(String message) {
logger.trace("Message [in]: {}", message);
Response response = GSON.fromJson(message, Response.class);
JsonElement payload = response.getPayload();
JsonObject jsonPayload = payload == null ? null : payload.getAsJsonObject();
String messageToLog = (jsonPayload != null && jsonPayload.has("client-key")) ? "***" : message;
logger.trace("Message [in]: {}", messageToLog);
ServiceCommand<?> request = null;

if (response.getId() != null) {
Expand All @@ -398,33 +411,33 @@ public void onMessage(String message) {
switch (response.getType()) {
case "response":
if (request == null) {
logger.debug("No matching request found for response message: {}", message);
logger.debug("No matching request found for response message: {}", messageToLog);
break;
}
if (response.getPayload() == null) {
logger.debug("No payload in response message: {}", message);
if (payload == null) {
logger.debug("No payload in response message: {}", messageToLog);
break;
}
try {
request.processResponse(response.getPayload().getAsJsonObject());
request.processResponse(jsonPayload);
} catch (RuntimeException ex) {
// An uncaught runtime exception in @OnWebSocketMessage annotated method will cause the web socket
// implementation to call @OnWebSocketError callback in which we would reset the connection.
// Users have the ability to create miss-configurations in which IllegalArgumentException could be
// thrown
logger.warn("Error while processing message: {} - in response to request: {} - Error Message: {}",
message, request, ex.getMessage());
messageToLog, request, ex.getMessage());
}
break;
case "error":
logger.debug("Error: {}", message);
logger.debug("Error: {}", messageToLog);

if (request == null) {
logger.warn("No matching request found for error message: {}", message);
logger.warn("No matching request found for error message: {}", messageToLog);
break;
}
if (response.getPayload() == null) {
logger.warn("No payload in error message: {}", message);
if (payload == null) {
logger.warn("No payload in error message: {}", messageToLog);
break;
}
try {
Expand All @@ -435,39 +448,37 @@ public void onMessage(String message) {
// Users have the ability to create miss-configurations in which IllegalArgumentException could be
// thrown
logger.warn("Error while processing error: {} - in response to request: {} - Error Message: {}",
message, request, ex.getMessage());
messageToLog, request, ex.getMessage());
}
break;
case "hello":
if (state != State.CONNECTING) {
logger.debug("Skipping response {}, not in CONNECTING state, state was {}", message, state);
logger.debug("Skipping response {}, not in CONNECTING state, state was {}", messageToLog, state);
break;
}
if (response.getPayload() == null) {
logger.warn("No payload in error message: {}", message);
if (jsonPayload == null) {
logger.warn("No payload in error message: {}", messageToLog);
break;
}
JsonObject deviceDescription = response.getPayload().getAsJsonObject();
Map<String, String> map = new HashMap<>();
map.put(PROPERTY_DEVICE_OS, deviceDescription.get("deviceOS").getAsString());
map.put(PROPERTY_DEVICE_OS_VERSION, deviceDescription.get("deviceOSVersion").getAsString());
map.put(PROPERTY_DEVICE_OS_RELEASE_VERSION,
deviceDescription.get("deviceOSReleaseVersion").getAsString());
map.put(PROPERTY_DEVICE_OS, jsonPayload.get("deviceOS").getAsString());
map.put(PROPERTY_DEVICE_OS_VERSION, jsonPayload.get("deviceOSVersion").getAsString());
map.put(PROPERTY_DEVICE_OS_RELEASE_VERSION, jsonPayload.get("deviceOSReleaseVersion").getAsString());
map.put(PROPERTY_LAST_CONNECTED, Instant.now().toString());
config.storeProperties(map);
sendRegister();
break;
case "registered":
if (state != State.REGISTERING) {
logger.debug("Skipping response {}, not in REGISTERING state, state was {}", message, state);
logger.debug("Skipping response {}, not in REGISTERING state, state was {}", messageToLog, state);
break;
}
if (response.getPayload() == null) {
logger.warn("No payload in registered message: {}", message);
if (jsonPayload == null) {
logger.warn("No payload in registered message: {}", messageToLog);
break;
}
this.requests.remove(response.getId());
config.storeKey(response.getPayload().getAsJsonObject().get("client-key").getAsString());
config.storeKey(jsonPayload.get("client-key").getAsString());
setState(State.REGISTERED);
break;
}
Expand Down

0 comments on commit 982c68c

Please sign in to comment.