Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ object PluginManifestSchema {
/** A `@string/...` resource. */
const val ATTR_LABEL = "label"

// <engine>
/**
* `true` when the engine lets scripts use Java classes. The host then keeps a [com.xfl.msgbot.plugin.api.engine.ClassAccess]
* for each project and sends it in `LoadRequest.classAccess`.
*/
const val ATTR_JAVA = "java"

// <language>
const val ATTR_NAME = "name"
const val ATTR_EXTENSION = "extension"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
* Copyright (C) 2026 MessengerBotTeam
*/

package com.xfl.msgbot.plugin.api.engine

import com.xfl.msgbot.plugin.api.value.Value
import com.xfl.msgbot.plugin.api.value.asArrayOrNull
import com.xfl.msgbot.plugin.api.value.asBooleanOrNull
import com.xfl.msgbot.plugin.api.value.asObjectOrNull
import com.xfl.msgbot.plugin.api.value.asStringOrNull

/**
* Which Java classes a script of one project may use, for an engine that runs Java (`<engine java="true">`).
* The host keeps it with the project and sends it in [LoadRequest.classAccess].
*
* Files are never part of this: a script reaches files only through the host, under the project's file rules,
* whatever this says.
*
* @property preset the starting set, see [Preset].
* @property allow class names added to the preset: an exact name, or a package followed by `.*` (`com.example.*`).
* @property deny class names taken away, in the same form. They win over [allow] and the preset.
* @property restrictions false turns the class rules off entirely, so a script may use any class. Files still go
* through the host.
*/
data class ClassAccess(
val preset: Preset = Preset.SAFE,
val allow: List<String> = emptyList(),
val deny: List<String> = emptyList(),
val restrictions: Boolean = true,
) {
enum class Preset {
/** No Java at all. */
OFF,

/** Ordinary classes; reflection, class loaders, processes and runtime internals only through checked stand-ins. */
SAFE,

/** [SAFE] plus real reflective members (`Method`, `Field`, `Constructor`) and `java.lang.Process`. */
WIDE,

/** [SAFE] with the project's own [allow] and [deny] lists. */
CUSTOM,
}

val javaEnabled: Boolean get() = preset != Preset.OFF

fun toValue(): Value =
Value.VObject(
mapOf(
"preset" to Value.VString(preset.name.lowercase()),
"allow" to Value.VArray(allow.map(Value::VString)),
"deny" to Value.VArray(deny.map(Value::VString)),
"restrictions" to Value.VBool(restrictions),
),
)

companion object {
val DEFAULT = ClassAccess()

/** Reads what [toValue] wrote. Unknown presets become [Preset.SAFE], so an older engine keeps the safe default. */
fun fromValue(value: Value): ClassAccess {
val map = value.asObjectOrNull() ?: throw IllegalArgumentException("Class access is a map")
val preset = map["preset"]?.asStringOrNull()?.uppercase()?.let { name -> Preset.entries.firstOrNull { it.name == name } } ?: Preset.SAFE
fun names(key: String) = map[key]?.asArrayOrNull()?.mapNotNull { it.asStringOrNull() }.orEmpty()
return ClassAccess(preset, names("allow"), names("deny"), map["restrictions"]?.asBooleanOrNull() ?: true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ fun interface Cancellable {
* @property profile the language facade, run after `__api` is bound and before [entry].
* @property sources project files keyed by path relative to the project folder.
* @property options per-project settings the engine declared.
* @property classAccess the Java classes the project's scripts may use; null when the host sends none, which an
* engine that runs Java treats as [ClassAccess.DEFAULT].
*/
data class LoadRequest(
val language: String,
Expand All @@ -143,6 +145,7 @@ data class LoadRequest(
val entry: String,
val sources: Map<String, String>,
val options: Map<String, String> = emptyMap(),
val classAccess: ClassAccess? = null,
) {
init {
require(entry in sources) { "The entry '$entry' is not among the sources" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package com.xfl.msgbot.plugin.api.remote

import com.xfl.msgbot.plugin.api.call.CallException
import com.xfl.msgbot.plugin.api.call.CallResult
import com.xfl.msgbot.plugin.api.engine.ClassAccess
import com.xfl.msgbot.plugin.api.engine.EngineException
import com.xfl.msgbot.plugin.api.engine.LoadRequest
import com.xfl.msgbot.plugin.api.engine.ProfileScript
Expand Down Expand Up @@ -118,6 +119,7 @@ internal object Wire {
"entry" to request.entry,
"sources" to request.sources,
"options" to request.options,
"classAccess" to request.classAccess?.toValue(),
)

fun loadRequestOf(value: Value): LoadRequest {
Expand All @@ -133,6 +135,7 @@ internal object Wire {
v.asStringOrNull() ?: throw IllegalArgumentException("A source is text")
},
options = map["options"]?.map()?.mapValues { (_, v) -> v.asStringOrNull().orEmpty() }.orEmpty(),
classAccess = map["classAccess"]?.takeIf { it != Value.VNull }?.let(ClassAccess::fromValue),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xfl.msgbot.plugin.api.remote

import com.xfl.msgbot.plugin.api.call.CallResult
import com.xfl.msgbot.plugin.api.engine.ClassAccess
import com.xfl.msgbot.plugin.api.engine.EngineContext
import com.xfl.msgbot.plugin.api.engine.EngineException
import com.xfl.msgbot.plugin.api.engine.EngineScheduler
Expand Down Expand Up @@ -129,6 +130,7 @@ class EngineSessionTest {
when (source) {
"loaded" -> Value.of(loaded?.let { listOf(it.language, it.entry, it.options["strict"], it.api.map { spec -> spec.namespace }) })
"seen" -> Value.of(seen.toList())
"classAccess" -> loaded?.classAccess?.toValue() ?: Value.VNull
else -> Value.VNull
}

Expand Down Expand Up @@ -169,6 +171,23 @@ class EngineSessionTest {
assertEquals(Value.of(listOf("fake", "main.fake", "true", StandardApi.HOST.map { it.namespace })), engine.eval("loaded"))
}

@Test
fun `the class access arrives with the load, and its absence stays absent`() {
val plain = connect()
plain.load(request)
assertEquals(Value.VNull, plain.eval("classAccess"))
val access = ClassAccess(ClassAccess.Preset.CUSTOM, allow = listOf("com.example.*"), deny = listOf("java.net.Socket"), restrictions = false)
val guarded = connect()
guarded.load(request.copy(classAccess = access))
assertEquals(access, ClassAccess.fromValue(guarded.eval("classAccess")))
}

@Test
fun `an unknown preset reads as the safe one`() {
val value = Value.VObject(mapOf("preset" to Value.VString("future")))
assertEquals(ClassAccess.DEFAULT, ClassAccess.fromValue(value))
}

@Test
fun `a script that cannot load fails the load with its own message`() {
val engine = connect()
Expand Down
60 changes: 60 additions & 0 deletions guard/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
plugins {
id("com.android.library")
`maven-publish`
}

android {
namespace = "com.xfl.msgbot.plugin.guard"
compileSdk = 36

defaultConfig {
minSdk = 28
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

testOptions {
unitTests.isIncludeAndroidResources = false
}

publishing {
singleVariant("release") { withSourcesJar() }
}
}

kotlin {
compilerOptions { jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17) }
}

dependencies {
api(project(":android"))
testImplementation("junit:junit:4.13.2")
testImplementation(kotlin("test-junit"))
androidTestImplementation("androidx.test:runner:1.7.0")
androidTestImplementation("androidx.test.ext:junit:1.3.0")
androidTestImplementation(kotlin("test-junit"))
}

publishing {
repositories {
mavenLocal()
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/MessengerBotTeam/PluginAPI")
credentials {
username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
}
}
}
publications {
register<MavenPublication>("gpr") {
artifactId = "PluginApi-guard"
afterEvaluate { from(components["release"]) }
}
}
}
Loading
Loading