|
1 | 1 | #include "test_framework.h" |
2 | 2 | #include "graph_buffer/graph_buffer.h" |
3 | 3 | #include "pipeline/pipeline_internal.h" |
| 4 | +#include "service_patterns.h" |
4 | 5 |
|
5 | 6 | #include <stdbool.h> |
6 | 7 | #include <stdint.h> |
7 | | - |
8 | | -bool cbm_service_pattern_is_http_route_literal(const char *literal, const char *callee_name); |
| 8 | +#include <stdio.h> |
9 | 9 |
|
10 | 10 | static int has_data_flow(cbm_gbuf_t *gb, int64_t source_id, int64_t target_id) { |
11 | 11 | const cbm_gbuf_edge_t **edges = NULL; |
@@ -48,6 +48,183 @@ TEST(infrascan_http_route_literal_guard_rejects_filesystem_paths) { |
48 | 48 | PASS(); |
49 | 49 | } |
50 | 50 |
|
| 51 | +/* ── Service-pattern QN boundaries (distilled from PR #1245) ───────── |
| 52 | + * A library id must sit on an identifier boundary inside the QN. A raw |
| 53 | + * substring match classified "proj.plugin.loader" as gin ("gin.") and |
| 54 | + * "proj.studio.x" as Dio ("dio"), which minted bogus Route / HTTP_CALLS |
| 55 | + * edges. The rule must stay open for CamelCase glue ("GuzzleHttp", |
| 56 | + * "FeignClient", "KafkaProducer", "IHttpClientFactory") and for version |
| 57 | + * suffixes ("urllib2", "amqp091-go"). */ |
| 58 | +typedef struct { |
| 59 | + const char *qn; |
| 60 | + cbm_svc_kind_t want; |
| 61 | +} svc_case_t; |
| 62 | + |
| 63 | +static int svc_case_mismatches(const svc_case_t *cases) { |
| 64 | + int bad = 0; |
| 65 | + for (int i = 0; cases[i].qn != NULL; i++) { |
| 66 | + cbm_svc_kind_t got = cbm_service_pattern_match(cases[i].qn); |
| 67 | + if (got != cases[i].want) { |
| 68 | + printf(" service-pattern mismatch: \"%s\" -> %d, expected %d\n", cases[i].qn, (int)got, |
| 69 | + (int)cases[i].want); |
| 70 | + bad++; |
| 71 | + } |
| 72 | + } |
| 73 | + return bad; |
| 74 | +} |
| 75 | + |
| 76 | +TEST(infrascan_service_pattern_match_rejects_ids_inside_words) { |
| 77 | + static const svc_case_t cases[] = { |
| 78 | + /* "gin." inside plugin. */ |
| 79 | + {"proj.plugins.autorun.tests.test_plugin._dispatch", CBM_SVC_NONE}, |
| 80 | + {"proj.plugin.loader", CBM_SVC_NONE}, |
| 81 | + /* "chi." / "echo." / "fiber." */ |
| 82 | + {"proj.hibachi.grill", CBM_SVC_NONE}, |
| 83 | + {"sound.reecho.apply", CBM_SVC_NONE}, |
| 84 | + {"textile.microfiber.weave", CBM_SVC_NONE}, |
| 85 | + /* "dio" inside studio / radio */ |
| 86 | + {"proj.studio.x", CBM_SVC_NONE}, |
| 87 | + {"radio.tune", CBM_SVC_NONE}, |
| 88 | + /* "surf" / "resty" / "curl" / "phin" / "hyper" / "sling" / "needle" / "treq" */ |
| 89 | + {"app.surface.draw", CBM_SVC_NONE}, |
| 90 | + {"ui.restyle.apply", CBM_SVC_NONE}, |
| 91 | + /* control: "forestry" spells "restr", never contained "resty" */ |
| 92 | + {"pkg.forestry.trees", CBM_SVC_NONE}, |
| 93 | + {"util.curly.brace", CBM_SVC_NONE}, |
| 94 | + {"sea.dolphin.swim", CBM_SVC_NONE}, |
| 95 | + {"math.hyperbolic.tanh", CBM_SVC_NONE}, |
| 96 | + {"farm.gosling.walk", CBM_SVC_NONE}, |
| 97 | + {"sewing.needles.sort", CBM_SVC_NONE}, |
| 98 | + {"util.streq", CBM_SVC_NONE}, |
| 99 | + /* "requests" glued to a lowercase prefix (upstream's case) */ |
| 100 | + {"proj.myrequests.client.get", CBM_SVC_NONE}, |
| 101 | + /* "express" / "rocket" / "tonic" / "nconf" */ |
| 102 | + {"ast.expression.eval", CBM_SVC_NONE}, |
| 103 | + {"mq.rocketmq.send", CBM_SVC_NONE}, |
| 104 | + {"geo.tectonic.plates", CBM_SVC_NONE}, |
| 105 | + {"app.unconfigured.warn", CBM_SVC_NONE}, |
| 106 | + {"cli.runconfig.load", CBM_SVC_NONE}, |
| 107 | + {"aws.s3express.session", CBM_SVC_NONE}, |
| 108 | + /* not clients of the id they contain: ASGI server, URL library, |
| 109 | + * nginx platform, Memgraph OGM, Celery settings module, a word */ |
| 110 | + {"hypercorn.run.serve", CBM_SVC_NONE}, |
| 111 | + {"hyperlink.URL.from_text", CBM_SVC_NONE}, |
| 112 | + {"openresty.core.request", CBM_SVC_NONE}, |
| 113 | + {"gqlalchemy.Memgraph.execute", CBM_SVC_NONE}, |
| 114 | + {"celeryconfig.broker_url", CBM_SVC_NONE}, |
| 115 | + {"kafkaesque.story.tell", CBM_SVC_NONE}, |
| 116 | + {NULL, CBM_SVC_NONE}, |
| 117 | + }; |
| 118 | + ASSERT_EQ(svc_case_mismatches(cases), 0); |
| 119 | + PASS(); |
| 120 | +} |
| 121 | + |
| 122 | +TEST(infrascan_service_pattern_match_keeps_real_library_qns) { |
| 123 | + static const svc_case_t cases[] = { |
| 124 | + /* Route registration: ids that end in a separator, own segment */ |
| 125 | + {"github.com/gin-gonic/gin.Default", CBM_SVC_ROUTE_REG}, |
| 126 | + {"proj.gin.router.GET", CBM_SVC_ROUTE_REG}, |
| 127 | + {"gin.GET", CBM_SVC_ROUTE_REG}, |
| 128 | + {"chi.NewRouter", CBM_SVC_ROUTE_REG}, |
| 129 | + {"github.com/labstack/echo.New", CBM_SVC_ROUTE_REG}, |
| 130 | + {"fiber.New", CBM_SVC_ROUTE_REG}, |
| 131 | + {"proj.express.router.get", CBM_SVC_ROUTE_REG}, |
| 132 | + {"@hapi/hapi.server.route", CBM_SVC_ROUTE_REG}, |
| 133 | + /* HTTP: own segment / separator-joined wrapper */ |
| 134 | + {"proj.venv.requests.api.get", CBM_SVC_HTTP}, |
| 135 | + {"proj.service.requests_get", CBM_SVC_HTTP}, |
| 136 | + {"package:dio/dio.dart", CBM_SVC_HTTP}, |
| 137 | + {"dio.Dio.get", CBM_SVC_HTTP}, |
| 138 | + {"pkg.net.curl.get", CBM_SVC_HTTP}, |
| 139 | + {"curl_exec", CBM_SVC_HTTP}, |
| 140 | + {"surf.get", CBM_SVC_HTTP}, |
| 141 | + {"hyper.Client.request", CBM_SVC_HTTP}, |
| 142 | + {"phin.promisified", CBM_SVC_HTTP}, |
| 143 | + {"needle.get", CBM_SVC_HTTP}, |
| 144 | + {"github.com/go-resty/resty.New", CBM_SVC_HTTP}, |
| 145 | + {"github.com/dghubble/sling.New", CBM_SVC_HTTP}, |
| 146 | + /* HTTP: CamelCase glue after the id */ |
| 147 | + {"GuzzleHttp\\Client", CBM_SVC_HTTP}, |
| 148 | + {"proj.GuzzleHttp.Client.get", CBM_SVC_HTTP}, |
| 149 | + {"org.springframework.cloud.openfeign.FeignClientBuilder.build", CBM_SVC_HTTP}, |
| 150 | + {"com.acme.net.HttpClientFactory.create", CBM_SVC_HTTP}, |
| 151 | + /* HTTP: CamelCase glue before the id */ |
| 152 | + {"System.Net.Http.IHttpClientFactory.CreateClient", CBM_SVC_HTTP}, |
| 153 | + {"org.asynchttpclient.AsyncHttpClient.prepareGet", CBM_SVC_HTTP}, |
| 154 | + {"Foundation.NSURLSession.dataTask", CBM_SVC_HTTP}, |
| 155 | + /* HTTP: version suffix after the id */ |
| 156 | + {"urllib2.urlopen", CBM_SVC_HTTP}, |
| 157 | + {"Mint.HTTP2.request", CBM_SVC_HTTP}, |
| 158 | + /* Async */ |
| 159 | + {"com.acme.events.KafkaProducer.send", CBM_SVC_ASYNC}, |
| 160 | + {"this.kafkaProducer.send", CBM_SVC_ASYNC}, |
| 161 | + {"org.apache.kafka.clients.producer.KafkaProducer.send", CBM_SVC_ASYNC}, |
| 162 | + {"github.com/rabbitmq/amqp091-go.Channel.Publish", CBM_SVC_ASYNC}, |
| 163 | + {"github.com/nats-io/nats.go.Conn.Publish", CBM_SVC_ASYNC}, |
| 164 | + /* Config */ |
| 165 | + {"os.getenv", CBM_SVC_CONFIG}, |
| 166 | + {"os.Getenv", CBM_SVC_CONFIG}, |
| 167 | + /* Glued-name libraries: the library's own name glues a lowercase |
| 168 | + * prefix/suffix onto another id, so each has an explicit table entry. |
| 169 | + * Every QN below contains NO other id — only the entry can match. */ |
| 170 | + {"grequests.map", CBM_SVC_HTTP}, |
| 171 | + {"txrequests.Session.send", CBM_SVC_HTTP}, |
| 172 | + {"redaxios.create", CBM_SVC_HTTP}, |
| 173 | + {"gaxios.request", CBM_SVC_HTTP}, |
| 174 | + {"libcurl.version", CBM_SVC_HTTP}, |
| 175 | + {"node-libcurl.Easy.perform", CBM_SVC_HTTP}, |
| 176 | + {"curlpp.Easy.perform", CBM_SVC_HTTP}, |
| 177 | + {"curlcpp.easy.perform", CBM_SVC_HTTP}, |
| 178 | + {"hyperlocal.UnixConnector.call", CBM_SVC_HTTP}, |
| 179 | + {"guzzlehttp/psr7.Utils.streamFor", CBM_SVC_HTTP}, |
| 180 | + {"aiokafka.helpers.create_ssl_context", CBM_SVC_ASYNC}, |
| 181 | + {"pykafka.topic.Topic.get_producer", CBM_SVC_ASYNC}, |
| 182 | + {"librdkafka.rd_version", CBM_SVC_ASYNC}, |
| 183 | + {"rdkafkacpp.version", CBM_SVC_ASYNC}, |
| 184 | + {"rskafka.client.partition.write", CBM_SVC_ASYNC}, |
| 185 | + {"aioamqp.connect", CBM_SVC_ASYNC}, |
| 186 | + {"amqpstorm.Connection.channel", CBM_SVC_ASYNC}, |
| 187 | + {"pamqp.frame.marshal", CBM_SVC_ASYNC}, |
| 188 | + {"kombu.transport.pyamqp.Transport.establish_connection", CBM_SVC_ASYNC}, |
| 189 | + {"amqprs.channel.basic_publish", CBM_SVC_ASYNC}, |
| 190 | + {"amqpcpp.TcpChannel.publish", CBM_SVC_ASYNC}, |
| 191 | + {"pynats.client.send", CBM_SVC_ASYNC}, |
| 192 | + {"jnats.options.build", CBM_SVC_ASYNC}, |
| 193 | + {"aiomqtt.client.publish", CBM_SVC_ASYNC}, |
| 194 | + {"amqtt.session.deliver", CBM_SVC_ASYNC}, |
| 195 | + {"hbmqtt.session.deliver", CBM_SVC_ASYNC}, |
| 196 | + {"umqtt.simple.publish", CBM_SVC_ASYNC}, |
| 197 | + {"mqttools.client.publish", CBM_SVC_ASYNC}, |
| 198 | + {"emqtt.publish", CBM_SVC_ASYNC}, |
| 199 | + {"rumqtt.client.publish", CBM_SVC_ASYNC}, |
| 200 | + {"github.com/256dpi/gomqtt/client.Publish", CBM_SVC_ASYNC}, |
| 201 | + {"libmosquitto.publish", CBM_SVC_ASYNC}, |
| 202 | + {"mosquittopp.publish", CBM_SVC_ASYNC}, |
| 203 | + {"google.cloud.pubsublite.admin.create_topic", CBM_SVC_ASYNC}, |
| 204 | + {"gocelery.client.delay", CBM_SVC_ASYNC}, |
| 205 | + {"_wgetenv", CBM_SVC_CONFIG}, |
| 206 | + {"_wgetenv_s", CBM_SVC_CONFIG}, |
| 207 | + {"os.getenvb", CBM_SVC_CONFIG}, |
| 208 | + {"qgetenv", CBM_SVC_CONFIG}, |
| 209 | + {"@dotenvx/dotenvx.config", CBM_SVC_CONFIG}, |
| 210 | + {"vlucas/phpdotenv.load", CBM_SVC_CONFIG}, |
| 211 | + {"apiflask.scaffold.get", CBM_SVC_ROUTE_REG}, |
| 212 | + {"honox.factory.createRoute", CBM_SVC_ROUTE_REG}, |
| 213 | + {"fasthttprouter.Router.GET", CBM_SVC_ROUTE_REG}, |
| 214 | + {"github.com/vektah/gqlparser/v2.LoadSchema", CBM_SVC_GRAPHQL}, |
| 215 | + {"github.com/Yamashou/gqlgenc.client.Post", CBM_SVC_GRAPHQL}, |
| 216 | + {"aiogqlc.client.execute", CBM_SVC_GRAPHQL}, |
| 217 | + {NULL, CBM_SVC_NONE}, |
| 218 | + }; |
| 219 | + ASSERT_EQ(svc_case_mismatches(cases), 0); |
| 220 | + ASSERT_STR_EQ(cbm_service_pattern_broker("aiokafka.helpers.create_ssl_context"), "kafka"); |
| 221 | + ASSERT_STR_EQ(cbm_service_pattern_broker("umqtt.simple.publish"), "mqtt"); |
| 222 | + ASSERT_STR_EQ(cbm_service_pattern_broker("com.acme.events.KafkaProducer.send"), "kafka"); |
| 223 | + ASSERT_STR_EQ(cbm_service_pattern_broker("github.com/rabbitmq/amqp091-go.Channel.Publish"), |
| 224 | + "rabbitmq"); |
| 225 | + PASS(); |
| 226 | +} |
| 227 | + |
51 | 228 | TEST(infrascan_route_nodes_skip_bad_http_url_paths) { |
52 | 229 | cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp/cbm_infrascan_route_guard"); |
53 | 230 | ASSERT_NOT_NULL(gb); |
@@ -128,6 +305,8 @@ TEST(infrascan_http_calls_join_matching_handler_route) { |
128 | 305 |
|
129 | 306 | SUITE(infrascan) { |
130 | 307 | RUN_TEST(infrascan_http_route_literal_guard_rejects_filesystem_paths); |
| 308 | + RUN_TEST(infrascan_service_pattern_match_rejects_ids_inside_words); |
| 309 | + RUN_TEST(infrascan_service_pattern_match_keeps_real_library_qns); |
131 | 310 | RUN_TEST(infrascan_route_nodes_skip_bad_http_url_paths); |
132 | 311 | RUN_TEST(infrascan_http_calls_join_matching_handler_route); |
133 | 312 | } |
0 commit comments