Skip to content

Commit

Permalink
Add sensitive field to inputText to remove sensitive data from output…
Browse files Browse the repository at this point in the history
… and logs

fixes mobile-dev-inc#1226
  • Loading branch information
Fishbowler committed Oct 12, 2024
1 parent 5cb09ff commit 054bca1
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
9 changes: 7 additions & 2 deletions maestro-client/src/main/java/maestro/Maestro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,13 @@ class Maestro(
return driver.waitForAppToSettle(initialHierarchy, appId, waitToSettleTimeoutMs)
}

fun inputText(text: String) {
LOGGER.info("Inputting text: $text")
fun inputText(text: String, sensitive: Boolean = false) {
LOGGER.info("Inputting text:".let {
if (sensitive)
"$it ${"".padEnd(text.length, '*')}"
else
"$it $text"
})

driver.inputText(text)
waitForAppToSettle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,19 +426,38 @@ data class AssertWithAICommand(

data class InputTextCommand(
val text: String,
val sensitive: Boolean = false,
override val label: String? = null,
override val optional: Boolean = false,
) : Command {

override fun description(): String {
return label ?: "Input text $text"
if (label != null) {
return label
}
val defaultLabelPrefix = "Input text "
if (sensitive) {
return defaultLabelPrefix + "".padEnd(text.length, '*')
} else {
return defaultLabelPrefix + text
}
}

override fun evaluateScripts(jsEngine: JsEngine): InputTextCommand {
return copy(
text = text.evaluateScripts(jsEngine)
)
}

override fun toString(): String {
val thisText = if (sensitive) {
"".padEnd(text.length, '*')
} else {
text
}

return "InputTextCommand(text=$thisText, sensitive=$sensitive, label=$label, optional=$optional)"
}
}

data class LaunchAppCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ class Orchestra(
}
}

maestro.inputText(command.text)
maestro.inputText(command.text, command.sensitive)

return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ data class YamlFluentCommand(
addMediaCommand = addMediaCommand(addMedia, flowPath)
)
)
inputText != null -> listOf(MaestroCommand(InputTextCommand(text = inputText.text, label = inputText.label, optional = inputText.optional)))
inputText != null -> listOf(MaestroCommand(InputTextCommand(text = inputText.text, sensitive = inputText.sensitive, label = inputText.label, optional = inputText.optional)))
inputRandomText != null -> listOf(MaestroCommand(InputRandomCommand(inputType = InputRandomType.TEXT, length = inputRandomText.length, label = inputRandomText.label, optional = inputRandomText.optional)))
inputRandomNumber != null -> listOf(MaestroCommand(InputRandomCommand(inputType = InputRandomType.NUMBER, length = inputRandomNumber.length, label = inputRandomNumber.label, optional = inputRandomNumber.optional)))
inputRandomEmail != null -> listOf(MaestroCommand(InputRandomCommand(inputType = InputRandomType.TEXT_EMAIL_ADDRESS, label = inputRandomEmail.label, optional = inputRandomEmail.optional)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.lang.UnsupportedOperationException

data class YamlInputText(
val text: String,
val sensitive: Boolean = false,
val label: String? = null,
val optional: Boolean = false,
) {
Expand All @@ -18,8 +19,10 @@ data class YamlInputText(
is String -> text
is Map<*, *> -> {
val input = text.getOrDefault("text", "") as String
val sensitive = text.getOrDefault("sensitive", false) as Boolean
val label = text.getOrDefault("label", null) as String?
return YamlInputText(input, label)
val optional = text.getOrDefault("optional", false) as Boolean
return YamlInputText(text = input, sensitive = sensitive, label = label, optional = optional)
}
is Int, is Long, is Char, is Boolean, is Float, is Double -> text.toString()
else -> throw UnsupportedOperationException("Cannot deserialize input text with data type ${text.javaClass}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ internal class MaestroCommandSerializationTest {
{
"inputTextCommand" : {
"text" : "Hello, world!",
"sensitive" : false,
"optional" : false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ internal class MaestroCommandTest {
.isEqualTo("Set location with negative coordinates")
}

@Test
fun `description (sensitive text)`() {
// given
val maestroCommand = MaestroCommand(InputTextCommand("password", true))

// when
val description = maestroCommand.description()

// then
assertThat(description)
.isEqualTo("Input text ********")
}

@Test
fun `toString (no commands)`() {
// given
Expand Down

0 comments on commit 054bca1

Please sign in to comment.