DeclarativeExtensionRecipeTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/extensions/recipe/DeclarativeExtensionRecipeTest.kt
1package eu.friedcutlet.yokan.services.extensions.recipe23import kotlin.test.Test4import kotlin.test.assertEquals5import kotlin.test.assertIs6import kotlin.test.assertNotEquals7import kotlin.test.assertFalse89class DeclarativeExtensionRecipeTest {10 @Test11 fun acceptsEntrypointBundleFromDeclaredOrigin() {12 val accepted = assertIs<RecipeBundleDecodeResult.Accepted>(StrictRecipeBundleDecoder.decode(validBundle().encodeToByteArray()))13 assertEquals("primary", accepted.bundle.entrypoints.getValue("popular").url.origin.originId)14 assertEquals("https://example.test", accepted.bundle.manifest.declaredOrigins.single().origin)15 }1617 @Test18 fun rejectsInvalidOriginsUnsafeUrlComponentsAndMissingCapabilities() {19 listOf(20 "http://example.test", "https://*.example.test", "https://user@example.test", "https://example.test/path",21 "https://example.test?query", "https://example.test#fragment", "https://EXAMPLE.test", "https://example.test:443", "https://192.0.2.1",22 ).forEach { origin -> assertRejected(validBundle().replace("https://example.test", origin), RecipeBundleErrorCode.InvalidValue) }23 assertRejected(validBundle().replace("\"originId\":\"primary\"", "\"originId\":\"missing\""), RecipeBundleErrorCode.InvalidValue)24 assertRejected(validBundle().replace("\"text\":\"/items\"", "\"text\":\"//attacker.test\""), RecipeBundleErrorCode.InvalidValue)25 assertRejected(validBundle().replace("\"text\":\"/items\"", "\"text\":\"/items#fragment\""), RecipeBundleErrorCode.InvalidValue)26 assertRejected(validBundle().replace("[\"networkRead\",\"documentExtraction\"]", "[\"documentExtraction\"]"), RecipeBundleErrorCode.InvalidValue)27 }2829 @Test30 fun rejectsUnknownFieldsOperationsVersionsAndAuthorityDataWithoutLeakingMarkers() {31 val marker = "ATTACKER_MARKER_6af27"32 val rejected = assertIs<RecipeBundleDecodeResult.Rejected>(StrictRecipeBundleDecoder.decode(33 validBundle().replace("\"name\":\"Example\"", "\"name\":\"Example\",\"$marker\":\"$marker\"").encodeToByteArray(),34 ))35 assertEquals(RecipeBundleErrorCode.UnknownFieldOrOperation, rejected.error.code)36 assertFalse(rejected.error.detail.contains(marker))37 assertRejected(validBundle().replace("\"literal\"", "\"executeJavaScript\""), RecipeBundleErrorCode.UnknownFieldOrOperation)38 }3940 @Test41 fun scannerRejectsEscapedAndNestedDuplicatesAndDuplicateDiscriminators() {42 assertRejected(validBundle().replace("\"id\":\"example.source\"", "\"id\":\"example.source\",\"\\u0069d\":\"other\""), RecipeBundleErrorCode.DuplicateJsonKey)43 assertRejected(validBundle().replace("\"originId\":\"primary\"", "\"originId\":\"primary\",\"\\u006friginId\":\"other\""), RecipeBundleErrorCode.DuplicateJsonKey)44 assertRejected(validBundle().replaceFirst("\"value\":\"literal\"", "\"value\":\"literal\",\"value\":\"hostInput\""), RecipeBundleErrorCode.DuplicateJsonKey)45 }4647 @Test48 fun scannerRejectsInvalidEncodingMalformedTokensTrailingCommaAndMultipleRoots() {49 assertRejected(byteArrayOf('{'.code.toByte(), '"'.code.toByte(), 'x'.code.toByte(), '"'.code.toByte(), ':'.code.toByte(), 0xC3.toByte()), RecipeBundleErrorCode.InvalidJson)50 listOf("{\"x\":\"\\u12\"}", "{\"x\":\"\\q\"}", "{\"x\":01}", "{\"x\":1e}", "{\"x\":-}").forEach {51 assertRejected(it, RecipeBundleErrorCode.InvalidJson)52 }53 assertRejected(validBundle().replace("\"documentExtraction\"]", "\"documentExtraction\",]"), RecipeBundleErrorCode.InvalidJson)54 assertRejected(validBundle() + validBundle(), RecipeBundleErrorCode.InvalidJson)55 }5657 @Test58 fun scannerHonorsExactAndBeyondStringCollectionAndDepthLimits() {59 assertIs<RecipeBundleDecodeResult.Accepted>(StrictRecipeBundleDecoder.decode(validBundle().replace("\"Example\"", "\"${"a".repeat(RecipeBundleLimits.maxStringCharacters)}\"").encodeToByteArray()))60 assertRejected(validBundle().replace("\"Example\"", "\"${"a".repeat(RecipeBundleLimits.maxStringCharacters + 1)}\""), RecipeBundleErrorCode.StringLimitExceeded)6162 val exactCapabilities = List(RecipeBundleLimits.maxCollectionEntries) { if (it == 0) "\"documentExtraction\"" else "\"networkRead\"" }.joinToString(",")63 assertIs<RecipeBundleDecodeResult.Accepted>(StrictRecipeBundleDecoder.decode(validBundle().replace("\"networkRead\",\"documentExtraction\"", exactCapabilities).encodeToByteArray()))64 val beyondCapabilities = List(RecipeBundleLimits.maxCollectionEntries + 1) { "\"networkRead\"" }.joinToString(",")65 assertRejected(validBundle().replace("\"networkRead\",\"documentExtraction\"", beyondCapabilities), RecipeBundleErrorCode.CollectionLimitExceeded)6667 val exactDepth = "[".repeat(RecipeBundleLimits.maxJsonDepth) + "0" + "]".repeat(RecipeBundleLimits.maxJsonDepth)68 assertNotEquals(RecipeBundleErrorCode.NestingLimitExceeded, errorFor(exactDepth))69 val beyondDepth = "[".repeat(RecipeBundleLimits.maxJsonDepth + 1) + "0" + "]".repeat(RecipeBundleLimits.maxJsonDepth + 1)70 assertRejected(beyondDepth, RecipeBundleErrorCode.NestingLimitExceeded)71 }7273 @Test74 fun legacyStepFormatVersionsAreRejectedAtValidationInsteadOfInstallingDeadRecipes() {75 listOf("1", "2").forEach { version ->76 assertRejected(validBundle().replace("\"formatVersion\":\"2.1\"", "\"formatVersion\":\"$version\""), RecipeBundleErrorCode.UnsupportedFormatVersion)77 }78 }7980 @Test81 fun nonEmptyLegacyStepsAreRejected() {82 assertRejected(validBundle().replace("\"steps\":[]", "\"steps\":[{\"operation\":\"buildUrl\"}]"), RecipeBundleErrorCode.InvalidValue)83 }8485 private fun assertRejected(json: String, expected: RecipeBundleErrorCode) = assertRejected(json.encodeToByteArray(), expected)86 private fun assertRejected(bytes: ByteArray, expected: RecipeBundleErrorCode) = assertEquals(expected, errorFor(bytes))87 private fun errorFor(json: String): RecipeBundleErrorCode = errorFor(json.encodeToByteArray())88 private fun errorFor(bytes: ByteArray): RecipeBundleErrorCode = errorFor(StrictRecipeBundleDecoder.decode(bytes))89 private fun errorFor(result: RecipeBundleDecodeResult): RecipeBundleErrorCode = assertIs<RecipeBundleDecodeResult.Rejected>(result).error.code90}9192internal fun validBundle(id: String = "example.source"): String = """{"manifest":{"formatVersion":"2.1","id":"$id","name":"Example","declaredCapabilities":["networkRead","documentExtraction"],"declaredOrigins":[{"id":"primary","origin":"https://example.test"}]},"recipe":{"steps":[]},"entrypoints":{"popular":{"inputs":["pageIndex"],"url":{"origin":{"originId":"primary"},"path":[{"value":"literal","text":"/items"}]},"projection":{"pointer":"/data","mode":"many","fields":{"id":"/id","title":"/title"}},"terminal":{"type":"listing","pageSize":20}}}}"""93