Skip to content

Commit

Permalink
Replace ISE with VertxException when failing context with information…
Browse files Browse the repository at this point in the history
…al message (#2496)

Related to #2486
Follows-up on #1857

ISE creates a stack trace which isn't really useful. In these cases, we only care about the message.

This change makes the application log a single line (or more if the message is long). Besides, it saves the cost of creating the ISE stack trace.

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
  • Loading branch information
tsegismont committed Oct 25, 2023
1 parent 39b731a commit a3c1907
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.vertx.ext.web.handler.impl;

import io.vertx.core.Vertx;
import io.vertx.core.VertxException;
import io.vertx.core.http.Cookie;
import io.vertx.core.http.CookieSameSite;
import io.vertx.core.http.HttpMethod;
Expand Down Expand Up @@ -200,7 +201,7 @@ private boolean isValidRequest(RoutingContext ctx) {
if (ctx.body().available()) {
header = ctx.request().getFormAttribute(headerName);
} else {
ctx.fail(new IllegalStateException("BodyHandler is required to process POST requests"));
ctx.fail(new VertxException("BodyHandler is required to process POST requests", true));
return false;
}
}
Expand Down Expand Up @@ -308,7 +309,7 @@ public void handle(RoutingContext ctx) {
// if we're being strict with the origin
// ensure that they are always valid
if (!Origin.check(origin, ctx)) {
ctx.fail(403, new IllegalStateException("Invalid Origin"));
ctx.fail(403, new VertxException("Invalid Origin", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.vertx.ext.web.handler.impl;

import io.vertx.core.VertxException;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
Expand Down Expand Up @@ -250,7 +251,7 @@ public void handle(RoutingContext context) {
.response()
.setStatusMessage("CORS Rejected - Invalid origin");
context
.fail(403, new IllegalStateException("CORS Rejected - Invalid origin"));
.fail(403, new VertxException("CORS Rejected - Invalid origin", true));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
Expand Down Expand Up @@ -146,7 +147,7 @@ private void mountRegister() {
.handler(ctx -> {
final User user = ctx.user();
if (user == null || user.get("username") == null) {
ctx.fail(new IllegalStateException("User object misses 'username' attribute"));
ctx.fail(new VertxException("User object misses 'username' attribute", true));
return;
}

Expand All @@ -170,7 +171,7 @@ private void mountVerify() {
.handler(ctx -> {
final User user = ctx.user();
if (user == null || user.get("username") == null) {
ctx.fail(new IllegalStateException("User object misses 'username' attribute"));
ctx.fail(new VertxException("User object misses 'username' attribute", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.VertxException;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.authentication.TokenCredentials;
Expand Down Expand Up @@ -115,19 +116,19 @@ public void postAuthentication(RoutingContext ctx) {
final User user = ctx.user();
if (user == null) {
// bad state
ctx.fail(403, new IllegalStateException("no user in the context"));
ctx.fail(403, new VertxException("no user in the context", true));
return;
}
// the user is authenticated, however the user may not have all the required scopes
if (scopes.size() > 0) {
final JsonObject jwt = user.get("accessToken");
if (jwt == null) {
ctx.fail(403, new IllegalStateException("Invalid JWT: null"));
ctx.fail(403, new VertxException("Invalid JWT: null", true));
return;
}

if(jwt.getValue("scope") == null) {
ctx.fail(403, new IllegalStateException("Invalid JWT: scope claim is required"));
if (jwt.getValue("scope") == null) {
ctx.fail(403, new VertxException("Invalid JWT: scope claim is required", true));
return;
}

Expand All @@ -144,7 +145,7 @@ public void postAuthentication(RoutingContext ctx) {
if (target != null) {
for (String scope : scopes) {
if (!target.contains(scope)) {
ctx.fail(403, new IllegalStateException("JWT scopes != handler scopes"));
ctx.fail(403, new VertxException("JWT scopes != handler scopes", true));
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package io.vertx.ext.web.handler.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.*;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.impl.logging.Logger;
Expand All @@ -43,7 +40,10 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author <a href="http://pmlopes@gmail.com">Paulo Lopes</a>
Expand Down Expand Up @@ -326,7 +326,7 @@ public void postAuthentication(RoutingContext ctx) {
final User user = ctx.user();
if (user == null) {
// bad state
ctx.fail(403, new IllegalStateException("no user in the context"));
ctx.fail(403, new VertxException("no user in the context", true));
return;
}

Expand All @@ -348,12 +348,12 @@ public void postAuthentication(RoutingContext ctx) {
(idx != 0 && scopes.charAt(idx -1) != ' ') ||
(idx + scope.length() != scopes.length() && scopes.charAt(idx + scope.length()) != ' ')) {
// invalid scope assignment
ctx.fail(403, new IllegalStateException("principal scope != handler scopes"));
ctx.fail(403, new VertxException("principal scope != handler scopes", true));
return;
}
} else {
// invalid scope assignment
ctx.fail(403, new IllegalStateException("principal scope != handler scopes"));
ctx.fail(403, new VertxException("principal scope != handler scopes", true));
return;
}
}
Expand Down Expand Up @@ -420,9 +420,9 @@ private void mountCallback() {

String errorDescription = ctx.request().getParam("error_description");
if (errorDescription != null) {
ctx.fail(errorCode, new IllegalStateException(error + ": " + errorDescription));
ctx.fail(errorCode, new VertxException(error + ": " + errorDescription, true));
} else {
ctx.fail(errorCode, new IllegalStateException(error));
ctx.fail(errorCode, new VertxException(error, true));
}
return;
}
Expand All @@ -432,7 +432,7 @@ private void mountCallback() {

// code is a require value
if (code == null) {
ctx.fail(400, new IllegalStateException("Missing code parameter"));
ctx.fail(400, new VertxException("Missing code parameter", true));
return;
}

Expand All @@ -448,7 +448,7 @@ private void mountCallback() {

// state is a required field
if (state == null) {
ctx.fail(400, new IllegalStateException("Missing IdP state parameter to the callback endpoint"));
ctx.fail(400, new VertxException("Missing IdP state parameter to the callback endpoint", true));
return;
}

Expand All @@ -462,7 +462,7 @@ private void mountCallback() {
// if there's a state in the context they must match
if (!state.equals(ctxState)) {
// forbidden, the state is not valid (this is a replay attack)
ctx.fail(401, new IllegalStateException("Invalid oauth2 state"));
ctx.fail(401, new VertxException("Invalid oauth2 state", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
Expand Down Expand Up @@ -144,7 +145,7 @@ private void mountRegister() {
.handler(ctx -> {
final User user = ctx.user();
if (user == null || user.get("username") == null) {
ctx.fail(new IllegalStateException("User object misses 'username' attribute"));
ctx.fail(new VertxException("User object misses 'username' attribute", true));
return;
}
final OtpKey key = otpKeyGen.generate();
Expand All @@ -168,7 +169,7 @@ private void mountVerify() {
.handler(ctx -> {
final User user = ctx.user();
if (user == null || user.get("username") == null) {
ctx.fail(new IllegalStateException("User object misses 'username' attribute"));
ctx.fail(new VertxException("User object misses 'username' attribute", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
Expand Down Expand Up @@ -201,7 +202,7 @@ private void mountRegister() {
// input basic validation is OK

if (session == null) {
ctx.fail(500, new IllegalStateException("No session or session handler is missing."));
ctx.fail(500, new VertxException("No session or session handler is missing.", true));
return;
}

Expand Down Expand Up @@ -245,7 +246,7 @@ private void mountLogin() {
// input basic validation is OK

if (session == null) {
ctx.fail(500, new IllegalStateException("No session or session handler is missing."));
ctx.fail(500, new VertxException("No session or session handler is missing.", true));
return;
}

Expand Down Expand Up @@ -300,7 +301,7 @@ private void mountResponse() {
final Session session = ctx.session();

if (session == null) {
ctx.fail(500, new IllegalStateException("No session or session handler is missing."));
ctx.fail(500, new VertxException("No session or session handler is missing.", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import io.vertx.ext.web.handler.sockjs.SockJSSocket;
import io.vertx.ext.web.impl.Origin;

import static io.vertx.core.http.HttpHeaders.*;
import static io.vertx.core.http.HttpHeaders.ALLOW;
import static io.vertx.ext.web.impl.Utils.canUpgradeToWebsocket;

/**
Expand Down Expand Up @@ -83,7 +83,7 @@ private void handleGet(RoutingContext ctx) {
}

if (!Origin.check(origin, ctx)) {
ctx.fail(403, new IllegalStateException("Invalid Origin"));
ctx.fail(403, new VertxException("Invalid Origin", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@

package io.vertx.ext.web.handler.sockjs.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.*;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.impl.logging.Logger;
Expand All @@ -50,7 +47,7 @@
import io.vertx.ext.web.handler.sockjs.SockJSSocket;
import io.vertx.ext.web.impl.Origin;

import static io.vertx.core.http.HttpHeaders.*;
import static io.vertx.core.http.HttpHeaders.ALLOW;
import static io.vertx.ext.web.impl.Utils.canUpgradeToWebsocket;

/**
Expand Down Expand Up @@ -88,7 +85,7 @@ private void handleGet(RoutingContext ctx) {
}

if (!Origin.check(origin, ctx)) {
ctx.fail(403, new IllegalStateException("Invalid Origin"));
ctx.fail(403, new VertxException("Invalid Origin", true));
return;
}

Expand Down

0 comments on commit a3c1907

Please sign in to comment.