Skip to content

RESTHeart 9.14.0

Choose a tag to compare

@github-actions github-actions released this 02 Nov 13:42
· 380 commits to master since this release
494e278

This release brings an SDK for plugins written in JavaScript and TypeScript, and changes how the brute force guard counts failed logins. The guard change alters existing behaviour: read "Before you upgrade" first.

Before you upgrade

You have bruteForceAttackGuard enabled. Failed logins are now counted per ip and user, with a second, higher threshold per ip. Five wrong passwords for alice from one address block that address for alice only. To keep the behaviour of 9.13, five failures per ip whatever the user, set:

bruteForceAttackGuard:
  max-failed-attempts: 0
  max-failed-attempts-per-ip: 5

The details are in the guard section below.

JavaScript and TypeScript plugins: the restheart:sdk module (#771 to #778)

A plugin written in JavaScript used to be Java in JavaScript syntax: Java.type() for everything, a Deque<String> for a query parameter, JSON.parse(request.getContent()) and setContentTypeAsJson() in every handler. A plugin now imports what it needs from restheart:sdk, a module served by RESTHeart:

import { defineService, mongo, logger, BadRequestError } from "restheart:sdk";

export default defineService({
  name: "usersService",
  description: "lists users",
  uri: "/users",
  contentKind: "json",
  secured: true,

  handle(req, res) {
    const limit = Number(req.query.limit ?? 100);
    if (Number.isNaN(limit)) throw new BadRequestError("wrong limit qparam");

    logger.debug("listing users for {}", req.account?.name);

    res.json(mongo.db("restheart").collection("users")
      .find({}, { limit, sort: { _id: -1 } })
      .toArray());
  },
});

The design was discussed in #756.

Request and response as data. req.query.limit is the first value of the parameter, a string, undefined when absent. req.headers["content-type"] is case-insensitive. req.account is { name, roles } or null. req.content is typed by contentKind. On the response, res.json(x) sets the content, the content type and status 200 in one call, and res.statusCode and res.headers are plain properties. Every decision about what a value is was made once, in Java, in the new org.restheart.polyglot.spi package.

contentKind. string, json, bson and bytes for services and interceptors, mongo and csv for interceptors only. A mongo interceptor reads req.mongo.dbName, collectionName, documentId, filter, isDocument() and the rest of a curated subset of MongoRequest. Services were string-based only; the json, bson and bytes variants are new. contentKind replaces pluginClass, which keeps working.

Errors. BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError and HttpError(status, message) are answered with their status and message. Any other exception is a 500: it is logged with the plugin name and the JavaScript stack trace, and nothing of it reaches the client.

Utilities, always present. GraalJS has no Node built-ins, so the npm packages that would provide these do not run.

  • logger, with {} placeholders and null-safe. LOGGER.debug("x {}", null) threw, because no Java overload could be chosen; logger.debug("x {}", null) logs x null.
  • crypto: SHA digests, HMAC, random bytes, UUID, constant-time compare. A receiver of signed webhooks (Stripe, GitHub, Twilio) can now be written in JavaScript.
  • encode for base64, base64url, hex, UTF-8 and URL encoding; resource to read a file shipped next to the plugin, inside the plugin directory only; bson for { $oid } ids.

Capabilities, from providers. http, mongo, config, mail and providers. mongo takes plain object literals in canonical extended JSON ({ _id: { $oid: id } }) and returns the same: find, findOne, aggregate, insertOne, updateMany, bulkWrite, db.command() and the rest. http calls other services with the shared client of a new provider, see below; a 4xx or a 5xx comes back with ok === false and does not throw. The import is the declaration: a plugin that imports mail where email is not configured is not deployed, and the log names the missing capability.

The execution model. Each request runs in its own virtual thread, so a blocking call is the right way to do I/O. There are no promises and no async/await in a plugin. The contexts are pooled: a variable at module level is not a server-wide cache.

TypeScript. The type declarations are on npm as @restheart/sdk, types only, versioned with RESTHeart and published with provenance from this release workflow. req and res are inferred from contentKind. Compile with tsc before deploying: RESTHeart deploys .mjs files and does not transpile. A plugin's package.json can name the SDK major it was written for in rh:spi, and RESTHeart refuses a plugin written for a later one.

Plugins written before 9.14.0 run unchanged. Their handle receives the Java request and response as before. LOGGER, mclient, pluginArgs and pluginClass are deprecated, with a replacement each in the docs. modulesReplacements is ignored: the GraalJS option it mapped to was removed in GraalVM 25.1.

Docs and examples. The documentation is now five pages: Develop Plugins in JavaScript, services, interceptors, utilities and capabilities. examples/js-plugin is rewritten in TypeScript, one file per part of the SDK. examples/node-plugin, for RESTHeart running on GraalVM Node, stays on the old API: it runs on the Node event loop, outside the contexts the SDK lives in.

Tests. A conformance suite of reference plugins, one per part of the SDK, runs in the integration tests on every build. The scenarios assert HTTP responses only, so a second guest language can reuse them.

bruteForceAttackGuard counts per ip and user (#792)

The guard counted failed logins per source ip: five in ten seconds and the address got 429 Too Many Requests on every request. Behind a carrier-grade NAT, as with several ISPs and every mobile network, or behind a corporate proxy, hundreds of people share one address. Five wrong passwords typed by five unrelated users blocked all of them, and everyone else on that address.

A brute force targets an account. The guard now counts per ip and user:

  • max-failed-attempts, default 5: failures per ip and user in 10 seconds. The address is blocked for that user only. 0 disables this count.
  • max-failed-attempts-per-ip, default 50: failures per ip whatever the user, for a password spray cycling through many usernames. 0 disables this ceiling.

The user is what the request names: the userid of a Basic challenge, the username of a Digest one, the sub of a bearer JWT, a fingerprint of any other bearer token such as an API key. A request with no credentials counts on the ip only. A mechanism whose credentials travel elsewhere can record the user with Metrics.attemptedUser(exchange, user). The log line and the alarm mail name the user when the block is per user.

A shared HTTP client: the http-client provider

A new provider in core, http-client, builds one java.net.http.HttpClient from the http-client section of the configuration: connect timeout, default request timeout, HTTP version, redirect policy, proxy. The http capability of the SDK uses it, and Java plugins get it with @Inject("http-client"). A client built inside a handler, one per request, loses connection pooling and repeats the TLS handshake on every call.

http-client:
  connect-timeout: 10        # seconds
  request-timeout: 30        # seconds
  version: http-2            # http-1.1 or http-2
  follow-redirects: normal   # never, normal, always

Other changes

  • The JavaScript plugin tests had been skipped on every build, CI included: the test runner checked for GraalVM 25.1 or later, and GraalVM 25.x reports the JDK version, 25.0.x. They now run wherever the plugin deployer runs.
  • restheart-polyglot changed its internal Java classes: JSServiceArgs is gone, and JSService and JSInterceptor are built from a PluginDefinition. This only matters to code that extended those classes.
  • The native image reachability metadata covers the new SDK classes.

Full Changelog: 9.13.1...9.14.0