github-advanced-security[bot] commented on code in PR #816:
URL: 
https://github.com/apache/httpcomponents-client/pull/816#discussion_r3005035721


##########
httpclient5-jakarta-rest-client/src/test/java/org/apache/hc/client5/http/examples/RestClientExample.java:
##########
@@ -0,0 +1,249 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.examples;
+
+import java.io.IOException;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.client5.http.rest.RestClientBuilder;
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+
+/**
+ * This example demonstrates how to use {@link RestClientBuilder}
+ * to create a type-safe REST client from a Jakarta REST annotated
+ * interface. The proxy translates method calls into HTTP requests
+ * executed through Apache HttpClient.
+ */
+public class RestClientExample {
+
+    // -- Domain object --
+
+    public static class User {
+        public int id;
+        public String name;
+        public String email;
+
+        public User() {
+        }
+
+        public User(final int id, final String name,
+                    final String email) {
+            this.id = id;
+            this.name = name;
+            this.email = email;
+        }
+
+        @Override
+        public String toString() {
+            return "User{id=" + id
+                    + ", name='" + name + '\''
+                    + ", email='" + email + '\'' + '}';
+        }
+    }
+
+    // -- Jakarta REST annotated interface --
+
+    @Path("/api/users")
+    @Produces(MediaType.APPLICATION_JSON)
+    @Consumes(MediaType.APPLICATION_JSON)
+    public interface UserApi {
+
+        @GET
+        List<User> list();
+
+        @GET
+        @Path("/{id}")
+        User get(@PathParam("id") int id);
+
+        @GET
+        @Path("/search")
+        @Produces(MediaType.APPLICATION_JSON)
+        List<User> search(@QueryParam("name") String name);
+
+        @POST
+        User create(User user);
+
+        @PUT
+        @Path("/{id}")
+        User update(@PathParam("id") int id, User user);
+
+        @DELETE
+        @Path("/{id}")
+        void delete(@PathParam("id") int id);
+    }
+
+    public static void main(final String[] args) throws Exception {
+        final ObjectMapper mapper = new ObjectMapper();
+
+        // Start an embedded HTTP server for demonstration
+        final HttpServer server = ServerBootstrap.bootstrap()
+                .setCanonicalHostName("localhost")
+                .register("/api/users", new HttpRequestHandler() {
+                    @Override
+                    public void handle(
+                            final ClassicHttpRequest request,
+                            final ClassicHttpResponse response,
+                            final HttpContext context)
+                            throws HttpException, IOException {
+                        final String method = request.getMethod();
+                        if ("GET".equals(method)) {
+                            final User[] users = {
+                                    new User(1, "Alice",
+                                            "[email protected]"),
+                                    new User(2, "Bob",
+                                            "[email protected]")
+                            };
+                            response.setCode(200);
+                            response.setEntity(new 
StringEntity(mapper.writeValueAsString(users), ContentType.APPLICATION_JSON));
+                        } else if ("POST".equals(method)) {
+                            final String body = 
EntityUtils.toString(request.getEntity());
+                            final User u = mapper.readValue(body, User.class);
+                            u.id = 3;
+                            response.setCode(201);
+                            response.setEntity(new 
StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));
+                        }
+                    }
+                })
+                .register("/api/users/*",
+                        new HttpRequestHandler() {
+                            @Override
+                            public void handle(
+                                    final ClassicHttpRequest request,
+                                    final ClassicHttpResponse response,
+                                    final HttpContext context)
+                                    throws HttpException, IOException {
+                                final String path = request.getRequestUri();
+                                final String method = request.getMethod();
+
+                                if (path.contains("/search")) {
+                                    final String uri = request.getRequestUri();
+                                    final int qi = uri.indexOf("name=");
+                                    final String name = qi >= 0 ? 
uri.substring(qi + 5) : "";
+                                    final User[] results = {
+                                            new User(1, name, 
name.toLowerCase() + "@example.com")
+                                    };
+                                    response.setCode(200);
+                                    response.setEntity(new 
StringEntity(mapper.writeValueAsString(results), ContentType.APPLICATION_JSON));
+                                    return;
+                                }
+
+                                final String idStr = path.substring(
+                                        path.lastIndexOf('/') + 1);
+                                final int id = Integer.parseInt(idStr);
+
+                                if ("GET".equals(method)) {
+                                    final User u = new User(id, "User-" + id, 
"user" + id + "@example.com");
+                                    response.setCode(200);
+                                    response.setEntity(new 
StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));
+                                } else if ("PUT".equals(method)) {
+                                    final String body = 
EntityUtils.toString(request.getEntity());
+                                    final User u = mapper.readValue(body, 
User.class);
+                                    u.id = id;
+                                    response.setCode(200);
+                                    response.setEntity(new 
StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));

Review Comment:
   ## Cross-site scripting
   
   Cross-site scripting vulnerability due to a [user-provided value](1).
   
   [Show more 
details](https://github.com/apache/httpcomponents-client/security/code-scanning/21)



##########
httpclient5-jakarta-rest-client/src/test/java/org/apache/hc/client5/http/examples/RestClientExample.java:
##########
@@ -0,0 +1,249 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.examples;
+
+import java.io.IOException;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.client5.http.rest.RestClientBuilder;
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+
+/**
+ * This example demonstrates how to use {@link RestClientBuilder}
+ * to create a type-safe REST client from a Jakarta REST annotated
+ * interface. The proxy translates method calls into HTTP requests
+ * executed through Apache HttpClient.
+ */
+public class RestClientExample {
+
+    // -- Domain object --
+
+    public static class User {
+        public int id;
+        public String name;
+        public String email;
+
+        public User() {
+        }
+
+        public User(final int id, final String name,
+                    final String email) {
+            this.id = id;
+            this.name = name;
+            this.email = email;
+        }
+
+        @Override
+        public String toString() {
+            return "User{id=" + id
+                    + ", name='" + name + '\''
+                    + ", email='" + email + '\'' + '}';
+        }
+    }
+
+    // -- Jakarta REST annotated interface --
+
+    @Path("/api/users")
+    @Produces(MediaType.APPLICATION_JSON)
+    @Consumes(MediaType.APPLICATION_JSON)
+    public interface UserApi {
+
+        @GET
+        List<User> list();
+
+        @GET
+        @Path("/{id}")
+        User get(@PathParam("id") int id);
+
+        @GET
+        @Path("/search")
+        @Produces(MediaType.APPLICATION_JSON)
+        List<User> search(@QueryParam("name") String name);
+
+        @POST
+        User create(User user);
+
+        @PUT
+        @Path("/{id}")
+        User update(@PathParam("id") int id, User user);
+
+        @DELETE
+        @Path("/{id}")
+        void delete(@PathParam("id") int id);
+    }
+
+    public static void main(final String[] args) throws Exception {
+        final ObjectMapper mapper = new ObjectMapper();
+
+        // Start an embedded HTTP server for demonstration
+        final HttpServer server = ServerBootstrap.bootstrap()
+                .setCanonicalHostName("localhost")
+                .register("/api/users", new HttpRequestHandler() {
+                    @Override
+                    public void handle(
+                            final ClassicHttpRequest request,
+                            final ClassicHttpResponse response,
+                            final HttpContext context)
+                            throws HttpException, IOException {
+                        final String method = request.getMethod();
+                        if ("GET".equals(method)) {
+                            final User[] users = {
+                                    new User(1, "Alice",
+                                            "[email protected]"),
+                                    new User(2, "Bob",
+                                            "[email protected]")
+                            };
+                            response.setCode(200);
+                            response.setEntity(new 
StringEntity(mapper.writeValueAsString(users), ContentType.APPLICATION_JSON));
+                        } else if ("POST".equals(method)) {
+                            final String body = 
EntityUtils.toString(request.getEntity());
+                            final User u = mapper.readValue(body, User.class);
+                            u.id = 3;
+                            response.setCode(201);
+                            response.setEntity(new 
StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));

Review Comment:
   ## Cross-site scripting
   
   Cross-site scripting vulnerability due to a [user-provided value](1).
   
   [Show more 
details](https://github.com/apache/httpcomponents-client/security/code-scanning/20)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to