Skip to content

Commit c614ca5

Browse files
kz930claude
andcommitted
fix(visualization): agree on a string image, and escape the cell URL Viz writes
Image Visualizer read any string as ready base64, where the executor hands the same value to b64encode and shows its reason page when the value is not bytes. The exported script reads a BINARY column as the base64 text JSONL carries, so the text is the image only where the column was declared BINARY; a column of any other type now fails in the same call, on the same page. URL Visualizer wrote the cell straight into the iframe's src attribute, so a quote in it closed the attribute and left the rest of the cell standing as attributes of the iframe. Both paths escape it now, the same five characters in the same order, which is what html.escape(quote=True) writes. An ampersand in a URL is written &amp; from here on, which the browser reads back as one. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 2e90310 commit c614ca5

5 files changed

Lines changed: 226 additions & 8 deletions

File tree

‎common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/ImageViz/ImageVisualizerOpDesc.scala‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,32 @@ class ImageVisualizerOpDesc extends PythonOperatorDescriptor with StandaloneCode
105105
// The translator skips it in the leaf-DataFrame print block.
106106
override def producesDataFrame(): Boolean = false
107107

108-
override def generateStandaloneCode(): String = {
108+
override def generateStandaloneCode(): String = generateStandaloneCode(Map.empty)
109+
110+
/** The executor hands `b64encode` whatever the column holds, so a column of
111+
* text ends in the reason page: `b64encode` refuses a `str`. The exported
112+
* script reads the same column out of JSONL, where a BINARY column arrives as
113+
* the base64 text of its bytes, which is already what the tag wants.
114+
*
115+
* So the text is read as an image only where the column was declared BINARY.
116+
* A column of any other type falls through to the same `b64encode` call and
117+
* fails there, which is the page the run showed.
118+
*/
119+
override def generateStandaloneCode(inputSchemas: Map[PortIdentity, Schema]): String = {
120+
val carriesBytes = inputSchemas
121+
.get(operatorInfo.inputPorts.head.id)
122+
.flatMap(_.getAttributes.find(_.getName == binaryContent))
123+
.forall(_.getType == AttributeType.BINARY)
124+
val textIsTheImage =
125+
if (carriesBytes) "isinstance(binary_image_data, str)" else "False"
109126
s"""import base64
110127
|
111128
|LT = chr(60)
112129
|GT = chr(62)
113130
|
114131
|def encode_image_to_html(binary_image_data):
115132
| try:
116-
| if isinstance(binary_image_data, str):
133+
| if $textIsTheImage:
117134
| encoded_image_str = binary_image_data
118135
| else:
119136
| encoded_image_data = base64.b64encode(binary_image_data)

‎common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/urlviz/UrlVizOpDesc.scala‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,23 @@ class UrlVizOpDesc extends LogicalOp with StandaloneCodeGenerator {
8989
// iframe HTML document and emit it as the "html-content" column.
9090
override def generateStandaloneCode(): String = {
9191
val urlLit = pyStringLiteral(urlContentAttrName)
92-
s"""def _texera_urlviz_iframe(u):
92+
s"""import html
93+
|
94+
|def _texera_urlviz_iframe(u):
9395
| # "null", not Python's "None": the operator interpolates the field into a
9496
| # Scala string, and the JVM renders a null that way.
95-
| u = "null" if pd.isna(u) else u
97+
| u = "null" if pd.isna(u) else str(u)
98+
| # The cell lands where an attribute is expected, so a quote in it would
99+
| # close src= and leave the rest of the cell standing as attributes of the
100+
| # iframe. escapeAttribute on the executor writes the same five characters
101+
| # in the same order, so the two paths write the page the same way.
102+
| u = html.escape(u, quote=True)
96103
| return (
97104
| '<!DOCTYPE html>\\n'
98105
| '<html lang="en">\\n'
99106
| '<body>\\n'
100107
| ' <div class="modal-body">\\n'
101-
| ' <iframe src="' + str(u) + '" frameborder="0"\\n'
108+
| ' <iframe src="' + u + '" frameborder="0"\\n'
102109
| ' style="height:100vh; width:100%; border:none;">\\n'
103110
| ' </iframe>\\n'
104111
| ' </div>\\n'

‎common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/urlviz/UrlVizOpExec.scala‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ import org.apache.texera.amber.util.JSONUtils.objectMapper
2929
class UrlVizOpExec(descString: String) extends OperatorExecutor {
3030
private val desc: UrlVizOpDesc = objectMapper.readValue(descString, classOf[UrlVizOpDesc])
3131
override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = {
32+
val url = UrlVizOpExec.escapeAttribute(
33+
String.valueOf(tuple.getField[Any](desc.urlContentAttrName))
34+
)
3235
val iframe =
3336
s"""<!DOCTYPE html>
3437
|<html lang="en">
3538
|<body>
3639
| <div class="modal-body">
37-
| <iframe src="${tuple.getField(desc.urlContentAttrName)}" frameborder="0"
40+
| <iframe src="$url" frameborder="0"
3841
| style="height:100vh; width:100%; border:none;">
3942
| </iframe>
4043
| </div>
@@ -43,3 +46,23 @@ class UrlVizOpExec(descString: String) extends OperatorExecutor {
4346
Iterator(TupleLike(iframe))
4447
}
4548
}
49+
50+
object UrlVizOpExec {
51+
52+
/** The value written where an attribute is expected.
53+
*
54+
* A cell is a value the workflow carried, not markup: a quote in it used to
55+
* close `src=` and leave the rest of the cell standing as attributes of the
56+
* iframe, which is the page speaking for whoever wrote the row. The five
57+
* characters and their order are Python's `html.escape(quote=True)`, which
58+
* the exported script calls on the same cell, so both paths write the page
59+
* the same way.
60+
*/
61+
def escapeAttribute(value: String): String =
62+
value
63+
.replace("&", "&amp;")
64+
.replace("<", "&lt;")
65+
.replace(">", "&gt;")
66+
.replace("\"", "&quot;")
67+
.replace("'", "&#x27;")
68+
}

‎common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/visualization/ImageViz/ImageVisualizerOpDescSpec.scala‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@
1919

2020
package org.apache.texera.amber.operator.visualization.ImageViz
2121

22-
import org.apache.texera.amber.core.tuple.AttributeType
22+
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
2323
import org.apache.texera.amber.operator.metadata.OperatorGroupConstants
2424
import org.scalatest.BeforeAndAfter
2525
import org.scalatest.flatspec.AnyFlatSpec
2626
import org.scalatest.matchers.should.Matchers
2727

28+
import java.nio.charset.StandardCharsets
29+
import java.nio.file.Files
30+
import java.util.concurrent.TimeUnit
31+
import scala.io.Source
32+
import scala.util.Try
33+
2834
class ImageVisualizerOpDescSpec extends AnyFlatSpec with BeforeAndAfter with Matchers {
2935
var opDesc: ImageVisualizerOpDesc = _
3036
before {
@@ -116,4 +122,72 @@ class ImageVisualizerOpDescSpec extends AnyFlatSpec with BeforeAndAfter with Mat
116122
code should not include "<p"
117123
code should not include "<div"
118124
}
125+
126+
// The executor hands b64encode whatever the column holds, and b64encode refuses
127+
// a str, so a column of text ends in the reason page. The exported script reads
128+
// the same column out of JSONL, where a BINARY column arrives as the base64 text
129+
// of its bytes, so text is the image there and only there.
130+
private val inPort = new ImageVisualizerOpDesc().operatorInfo.inputPorts.head.id
131+
132+
private def standaloneFor(columnType: AttributeType): String = {
133+
opDesc.binaryContent = "image_bytes"
134+
opDesc.generateStandaloneCode(Map(inPort -> Schema().add("image_bytes", columnType)))
135+
}
136+
137+
it should "read the column's text as the image only where it was declared binary" in {
138+
standaloneFor(AttributeType.BINARY) should include("if isinstance(binary_image_data, str):")
139+
}
140+
141+
it should "let a text column fail in b64encode, the way the executor does" in {
142+
val code = standaloneFor(AttributeType.STRING)
143+
code should include("if False:")
144+
code should not include "isinstance(binary_image_data, str)"
145+
// The call the executor makes on the same value, left to refuse it here too.
146+
code should include("base64.b64encode(binary_image_data)")
147+
}
148+
149+
// Both paths run the same two lines on a string, so the answer is one both can
150+
// be held to: b64encode raises, and the reason page is what the viewer sees.
151+
it should "answer a string column with the reason page in both paths" in {
152+
val python = resolvePython().getOrElse(cancel("No runnable python executable"))
153+
154+
val body = standaloneFor(AttributeType.STRING).linesIterator
155+
.takeWhile(!_.startsWith("all_images_html"))
156+
.mkString("\n")
157+
val driver =
158+
s"""$body
159+
|print(encode_image_to_html("not an image"))
160+
|""".stripMargin
161+
162+
val script = Files.createTempFile("image-visualizer-string-", ".py")
163+
script.toFile.deleteOnExit()
164+
Files.write(script, driver.getBytes(StandardCharsets.UTF_8))
165+
val process = new ProcessBuilder(python, script.toString).redirectErrorStream(true).start()
166+
val out = Source.fromInputStream(process.getInputStream).mkString
167+
process.waitFor(120, TimeUnit.SECONDS)
168+
withClue(s"python said:\n$out\nscript:\n$driver") {
169+
process.exitValue() shouldBe 0
170+
out should include("Image is not available.")
171+
out should include("Reason: Binary input is not valid")
172+
out should not include "img src"
173+
}
174+
// The executor's own reason page, which the run showed for the same value.
175+
opDesc.generatePythonCode() should include("Image is not available.")
176+
}
177+
178+
private def resolvePython(): Option[String] = {
179+
def runnable(exe: String): Boolean =
180+
Try(new ProcessBuilder(exe, "--version").redirectErrorStream(true).start()).toOption
181+
.exists { p =>
182+
if (!p.waitFor(5, TimeUnit.SECONDS)) { p.destroyForcibly(); false }
183+
else p.exitValue() == 0
184+
}
185+
186+
(sys.env.get("UDF_PYTHON_PATH").filter(_.nonEmpty).toList ++ List(
187+
"python3",
188+
"python",
189+
"py"
190+
)).distinct
191+
.find(runnable)
192+
}
119193
}

‎common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/visualization/urlviz/UrlVizOpDescSpec.scala‎

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ package org.apache.texera.amber.operator.visualization.urlviz
2222
import com.fasterxml.jackson.annotation.JsonProperty
2323
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject
2424
import org.apache.texera.amber.core.executor.OpExecWithClassName
25-
import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema}
25+
import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema, Tuple}
2626
import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity}
2727
import org.apache.texera.amber.operator.metadata.OperatorGroupConstants
2828
import org.apache.texera.amber.operator.metadata.annotations.AutofillAttributeName
29+
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.pyStringLiteral
30+
import org.apache.texera.amber.util.JSONUtils.objectMapper
2931
import org.scalatest.flatspec.AnyFlatSpec
3032
import org.scalatest.matchers.should.Matchers
3133

34+
import java.nio.charset.StandardCharsets
35+
import java.nio.file.Files
36+
import java.util.concurrent.TimeUnit
3237
import javax.validation.constraints.NotNull
38+
import scala.io.Source
39+
import scala.util.Try
3340

3441
class UrlVizOpDescSpec extends AnyFlatSpec with Matchers {
3542

@@ -125,4 +132,94 @@ class UrlVizOpDescSpec extends AnyFlatSpec with Matchers {
125132
val b = new UrlVizOpDesc
126133
a.operatorIdentifier should not equal b.operatorIdentifier
127134
}
135+
136+
// ---------------------------------------------------------------------------
137+
// The cell lands where an attribute is expected
138+
// ---------------------------------------------------------------------------
139+
140+
// A quote in the cell used to close `src=` and leave the rest of it standing as
141+
// attributes of the iframe, which is the page speaking for whoever wrote the
142+
// row. Both paths are held to the same written page for that value.
143+
private val hostileUrl = """https://example.invalid/a" onload="alert(1)"""
144+
145+
/** A configured descriptor: `urlContentAttrName` is a val, so Jackson writes it. */
146+
private def descFor(urlAttr: String): UrlVizOpDesc = {
147+
val node = objectMapper.createObjectNode()
148+
node.put("operatorType", "URLVisualizer")
149+
node.put("urlContentAttrName", urlAttr)
150+
objectMapper.readValue(node.toString, classOf[UrlVizOpDesc])
151+
}
152+
153+
it should "escape a hostile cell rather than letting it add an attribute" in {
154+
val attr = new Attribute("url", AttributeType.STRING)
155+
val tuple = Tuple.builder(Schema().add(attr)).add(attr, hostileUrl).build()
156+
val page = new UrlVizOpExec(objectMapper.writeValueAsString(descFor("url")))
157+
.processTuple(tuple, port = 0)
158+
.next()
159+
.getFields
160+
.head
161+
.asInstanceOf[String]
162+
163+
page should include("""src="https://example.invalid/a&quot; onload=&quot;alert(1)"""")
164+
page should not include """onload="alert"""
165+
}
166+
167+
it should "write that cell the same way in the exported script" in {
168+
val python = resolvePython().getOrElse(cancel("No runnable python executable"))
169+
if (!canImportPandas(python)) cancel(s"'$python' cannot import pandas")
170+
171+
val desc = descFor("url")
172+
val attr = new Attribute("url", AttributeType.STRING)
173+
val tuple = Tuple.builder(Schema().add(attr)).add(attr, hostileUrl).build()
174+
val fromExecutor = new UrlVizOpExec(objectMapper.writeValueAsString(desc))
175+
.processTuple(tuple, port = 0)
176+
.next()
177+
.getFields
178+
.head
179+
.asInstanceOf[String]
180+
181+
val driver =
182+
s"""import pandas as pd
183+
|
184+
|in1df = pd.DataFrame({"url": [${pyStringLiteral(hostileUrl)}]})
185+
|${desc.generateStandaloneCode()}
186+
|
187+
|print(out1df["html-content"].iloc[0])
188+
|""".stripMargin
189+
190+
val script = Files.createTempFile("urlviz-hostile-", ".py")
191+
script.toFile.deleteOnExit()
192+
Files.write(script, driver.getBytes(StandardCharsets.UTF_8))
193+
val process = new ProcessBuilder(python, script.toString).redirectErrorStream(true).start()
194+
val out = Source.fromInputStream(process.getInputStream).mkString
195+
process.waitFor(120, TimeUnit.SECONDS)
196+
withClue(s"python said:\n$out\nscript:\n$driver") {
197+
process.exitValue() shouldBe 0
198+
out.trim shouldBe fromExecutor
199+
}
200+
}
201+
202+
private def resolvePython(): Option[String] = {
203+
def runnable(exe: String): Boolean =
204+
Try(new ProcessBuilder(exe, "--version").redirectErrorStream(true).start()).toOption
205+
.exists { p =>
206+
if (!p.waitFor(5, TimeUnit.SECONDS)) { p.destroyForcibly(); false }
207+
else p.exitValue() == 0
208+
}
209+
210+
(sys.env.get("UDF_PYTHON_PATH").filter(_.nonEmpty).toList ++ List(
211+
"python3",
212+
"python",
213+
"py"
214+
)).distinct
215+
.find(runnable)
216+
}
217+
218+
private def canImportPandas(python: String): Boolean =
219+
Try(
220+
new ProcessBuilder(python, "-c", "import pandas").redirectErrorStream(true).start()
221+
).toOption.exists { p =>
222+
if (!p.waitFor(60, TimeUnit.SECONDS)) { p.destroyForcibly(); false }
223+
else p.exitValue() == 0
224+
}
128225
}

0 commit comments

Comments
 (0)