You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/09/02 22:19:12 UTC

(groovy) branch master updated (dd1fa5a88f -> 9141e9067a)

This is an automated email from the ASF dual-hosted git repository.

paulk-asert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


    from dd1fa5a88f GROOVY-12331: correct the threat model's claim that JAXP bounds element depth
     new 857d01832e GROOVY-12334: groovysh: /slurp parses content, it does not evaluate it
     new 9141e9067a GROOVY-12334: groovysh: say what -f GROOVY on a URL actually grants, and show it

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../groovy/groovysh/jline/GroovyCommands.groovy    | 119 +++++++++++++++++----
 .../apache/groovy/groovysh/jline/GroovyEngine.java |  33 ++----
 .../groovy-groovysh/src/spec/doc/groovysh.adoc     |  24 ++++-
 .../groovysh/jline/GroovyCommandsSlurpTest.groovy  |  87 +++++++++++++++
 .../groovy/groovysh/jline/GroovyEngineTest.groovy  |  36 +++++++
 5 files changed, 254 insertions(+), 45 deletions(-)
 create mode 100644 subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/jline/GroovyCommandsSlurpTest.groovy


(groovy) 02/02: GROOVY-12334: groovysh: say what -f GROOVY on a URL actually grants, and show it

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 9141e9067a240c405b39af6cc0e4937a17335aa8
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Sep 2 22:28:53 2026 +1000

    GROOVY-12334: groovysh: say what -f GROOVY on a URL actually grants, and show it
    
    Constraining AUTO left the impression that evaluating remote content was
    now the safe path rather than the authorised one. It is not safer. Asking
    for -f GROOVY against a URL runs whatever that server returns with the
    user's privileges; the only thing that changed is who chose it, the user
    rather than the payload. The guide now says that plainly instead of
    implying remoteness was what made the old sniffing wrong.
    
    Consent to run a URL's content is also weaker than it looks, because
    redirects are followed: the address typed need not be where the code came
    from. /slurp therefore prints the URL it was actually served from before
    evaluating, naming both when a redirect moved it. That changes no policy -
    the decision remains the user's - it just makes the decision visible at
    the point it is taken.
    
    The notice, the extension mapping, and the URL-path extension parsing are
    package-private so they can be tested without a terminal or a server;
    serving over HTTP is not something the test suite should need to do.
---
 .../groovy/groovysh/jline/GroovyCommands.groovy    | 54 +++++++++++++-
 .../groovy-groovysh/src/spec/doc/groovysh.adoc     | 13 +++-
 .../groovysh/jline/GroovyCommandsSlurpTest.groovy  | 87 ++++++++++++++++++++++
 3 files changed, 149 insertions(+), 5 deletions(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
index ef4e67ea26..aaea8f7d49 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
@@ -45,6 +45,8 @@ import org.jline.terminal.Terminal
 import org.jline.terminal.impl.TerminalGraphics
 import org.jline.terminal.impl.TerminalGraphicsManager
 import org.jline.utils.AttributedString
+import org.jline.utils.AttributedStringBuilder
+import org.jline.utils.AttributedStyle
 
 import javax.imageio.ImageIO
 import javax.swing.ImageIcon
@@ -691,7 +693,11 @@ class GroovyCommands extends JlineCommandRegistry implements CommandRegistry {
                     // -f GROOVY rather than inferred from the address.
                     format = formatForExtension(extensionOf(url.path), false)
                 }
-                source = new InputStreamReader(url.openStream(), encoding)
+                URLConnection connection = url.openConnection()
+                source = new InputStreamReader(connection.inputStream, encoding)
+                if (format == 'GROOVY') {
+                    noteEvaluatingRemoteContent(input, url, connection)
+                }
             }
             if (source) {
                 if (format == 'TEXT') {
@@ -729,6 +735,46 @@ class GroovyCommands extends JlineCommandRegistry implements CommandRegistry {
         out
     }
 
+    /**
+     * Reports that content fetched over the network is about to be evaluated, naming where it was
+     * actually served from.
+     * <p>
+     * Asking for {@code -f GROOVY} grants the server code execution in this session, and redirects
+     * are followed, so the address typed is not necessarily the source the code came from. The
+     * decision is the user's to make, but it should be made visible at the point it is taken.
+     *
+     * @param input the command input, for terminal access
+     * @param requested the URL the user named
+     * @param connection the open connection, whose URL reflects any redirects followed
+     */
+    private static void noteEvaluatingRemoteContent(CommandInput input, URL requested, URLConnection connection) {
+        String message = remoteEvaluationNotice(requested, connection.URL)
+        def writer = input.terminal()?.writer()
+        if (writer) {
+            writer.println(new AttributedStringBuilder()
+                .style(AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW))
+                .append("/slurp: $message")
+                .style(AttributedStyle.DEFAULT)
+                .toAnsi(input.terminal()))
+            writer.flush()
+        }
+    }
+
+    /**
+     * The notice shown before remote content is evaluated, naming the URL actually served from and,
+     * when redirects moved it, the one that was asked for.
+     *
+     * @param requested the URL the user named
+     * @param served the URL the content came from, or {@code null} if unknown
+     * @return the message to display
+     */
+    // package-private for testing
+    static String remoteEvaluationNotice(URL requested, URL served) {
+        served && served.toString() != requested.toString()
+            ? "evaluating Groovy fetched from $served (redirected from $requested)"
+            : "evaluating Groovy fetched from $requested"
+    }
+
     /**
      * Maps a file extension onto a slurp format, or {@code null} when nothing matches (leaving the
      * caller to fall back on AUTO, which parses but never evaluates).
@@ -738,7 +784,8 @@ class GroovyCommands extends JlineCommandRegistry implements CommandRegistry {
      *                  local file the user named, false for content fetched over the network
      * @return the format name, or {@code null}
      */
-    private static String formatForExtension(String ext, boolean allowCode) {
+    // package-private for testing
+    static String formatForExtension(String ext, boolean allowCode) {
         if (!ext) return null
         switch (ext.toLowerCase(Locale.ROOT)) {
             case 'json': return 'JSON'
@@ -760,7 +807,8 @@ class GroovyCommands extends JlineCommandRegistry implements CommandRegistry {
      * @param path the URL path
      * @return the extension without the dot, or {@code null} when the last segment has none
      */
-    private static String extensionOf(String path) {
+    // package-private for testing
+    static String extensionOf(String path) {
         if (!path) return null
         int slash = path.lastIndexOf('/')
         String name = slash < 0 ? path : path.substring(slash + 1)
diff --git a/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc b/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
index 23fed24bca..a115e96e98 100644
--- a/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
+++ b/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
@@ -1203,8 +1203,17 @@ request: pass `-f GROOVY`, or name a local `.groovy` file.
 For a URL, the extension of the URL path selects the format in the same way
 as a filename does, but only ever a data format &#8212; `GROOVY` is not inferred
 from an address, because the extension is part of the URL you typed while the
-bytes are whatever the server chooses to return. Use `-f GROOVY` if you really
-do want to evaluate what a URL serves.
+bytes are whatever the server chooses to return.
+
+WARNING: `-f GROOVY` against a URL runs whatever that server returns, with your
+privileges, in this session. It is not a safer way to evaluate remote content
+than the old content sniffing was &#8212; it is the *authorised* way, and the
+difference is only that you chose it rather than the payload choosing for you.
+Redirects are followed, so the address you typed is not necessarily where the
+code came from; `/slurp` prints the URL it was actually served from before
+evaluating it. Treat it as you would piping a download straight into a shell, and prefer
+fetching the content first (without `-f`) so you can read it before deciding
+to run it.
 
 Although the details of the exact object returned are an implementation
 detail which may change in the future, the current behavior is as follows:
diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/jline/GroovyCommandsSlurpTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/jline/GroovyCommandsSlurpTest.groovy
new file mode 100644
index 0000000000..1763cda8fd
--- /dev/null
+++ b/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/jline/GroovyCommandsSlurpTest.groovy
@@ -0,0 +1,87 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.groovy.groovysh.jline
+
+import org.junit.jupiter.api.Test
+
+/**
+ * Format selection and the remote-evaluation notice for {@code /slurp}. Lives in the same package so
+ * the helpers can be exercised without standing up a terminal or an HTTP server.
+ */
+class GroovyCommandsSlurpTest {
+
+    // GROOVY-12334
+    @Test
+    void extensionSelectsADataFormat() {
+        assert GroovyCommands.formatForExtension('json', false) == 'JSON'
+        assert GroovyCommands.formatForExtension('YML', false) == 'YAML'
+        assert GroovyCommands.formatForExtension('markdown', false) == 'MARKDOWN'
+        assert GroovyCommands.formatForExtension('csv', true) == 'CSV'
+    }
+
+    // GROOVY-12334
+    @Test
+    void groovyIsSelectableForALocalFileButNeverForAUrl() {
+        assert GroovyCommands.formatForExtension('groovy', true) == 'GROOVY'
+        assert GroovyCommands.formatForExtension('groovy', false) == null
+    }
+
+    // GROOVY-12334
+    @Test
+    void unknownOrAbsentExtensionSelectsNothing() {
+        // null means "fall back to AUTO", which parses but never evaluates
+        assert GroovyCommands.formatForExtension('dat', true) == null
+        assert GroovyCommands.formatForExtension('', true) == null
+        assert GroovyCommands.formatForExtension(null, true) == null
+    }
+
+    // GROOVY-12334
+    @Test
+    void urlPathExtensionIsTakenFromTheLastSegmentOnly() {
+        assert GroovyCommands.extensionOf('/data/feed.json') == 'json'
+        assert GroovyCommands.extensionOf('/a.b.c/feed.yaml') == 'yaml'
+        assert GroovyCommands.extensionOf('/no/extension/here') == null
+        assert GroovyCommands.extensionOf('/trailing/slash/') == null
+        assert GroovyCommands.extensionOf('') == null
+        assert GroovyCommands.extensionOf(null) == null
+        // a dotfile has no extension to speak of
+        assert GroovyCommands.extensionOf('/dir/.hidden') == null
+    }
+
+    // GROOVY-12334
+    @Test
+    void noticeNamesTheUrlEvaluatedFrom() {
+        def url = new URL('http://example.test/x.groovy')
+        assert GroovyCommands.remoteEvaluationNotice(url, url) ==
+                'evaluating Groovy fetched from http://example.test/x.groovy'
+        assert GroovyCommands.remoteEvaluationNotice(url, null) ==
+                'evaluating Groovy fetched from http://example.test/x.groovy'
+    }
+
+    // GROOVY-12334
+    @Test
+    void noticeReportsBothUrlsWhenARedirectMovedTheContent() {
+        // the address typed is not necessarily where the code came from
+        def asked = new URL('http://example.test/x.groovy')
+        def served = new URL('http://elsewhere.test/payload.groovy')
+        assert GroovyCommands.remoteEvaluationNotice(asked, served) ==
+                'evaluating Groovy fetched from http://elsewhere.test/payload.groovy' +
+                ' (redirected from http://example.test/x.groovy)'
+    }
+}


(groovy) 01/02: GROOVY-12334: groovysh: /slurp parses content, it does not evaluate it

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 857d01832e4034cb983349d059e2df7c1d80aa3f
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Sep 2 22:16:36 2026 +1000

    GROOVY-12334: groovysh: /slurp parses content, it does not evaluate it
    
    The AUTO fallback decided whether a document was data or code from the
    shape of the document. Content that trimmed to "[...]" with no braces in
    it went straight to execute(); with braces it was tried as JSON and then
    executed if that failed. A "{...}" document was only ever parsed. So the
    choice between parsing and evaluating was made by whoever supplied the
    bytes, in a command documented as a parsing convenience.
    
    That fallback is where URL content always landed. The local-file branch
    derives a format from the file extension, but the URL branch derived
    nothing, so every http(s) slurp reached AUTO no matter what it named.
    
    AUTO now recognises JSON and otherwise returns the text unchanged, and a
    URL path's extension selects a format the same way a filename does.
    Evaluating content is unchanged but has to be asked for: -f GROOVY, or a
    local .groovy file. A URL extension deliberately cannot select GROOVY -
    the extension is part of an address the user typed, while the bytes are
    whatever the server chose to return, and those are not the same thing.
    
    The behaviour that goes away is Groovy literal syntax that is not also
    JSON: /slurp '[a:1]' returned a Map because AUTO evaluated it, and now
    returns the text. -f GROOVY covers that case and always did.
    
    The documentation described the fallback as determining the format from
    the content without saying it could evaluate, and its format table did
    not list GROOVY at all, so both are corrected here.
---
 .../groovy/groovysh/jline/GroovyCommands.groovy    | 69 +++++++++++++++-------
 .../apache/groovy/groovysh/jline/GroovyEngine.java | 33 ++++-------
 .../groovy-groovysh/src/spec/doc/groovysh.adoc     | 15 ++++-
 .../groovy/groovysh/jline/GroovyEngineTest.groovy  | 36 +++++++++++
 4 files changed, 109 insertions(+), 44 deletions(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
index 2348ebdc02..ef4e67ea26 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
@@ -679,30 +679,18 @@ class GroovyCommands extends JlineCommandRegistry implements CommandRegistry {
             if (path && Files.exists(path)) {
                 source = Files.newBufferedReader(path, encoding)
                 if (!format) {
-                    def ext = path.extension
-                    if (ext.equalsIgnoreCase('json')) {
-                        format = 'JSON'
-                    } else if (ext.equalsIgnoreCase('yaml') || ext.equalsIgnoreCase('yml')) {
-                        format = 'YAML'
-                    } else if (ext.equalsIgnoreCase('xml') || ext.equalsIgnoreCase('rdf')) {
-                        format = 'XML'
-                    } else if (ext.equalsIgnoreCase('groovy')) {
-                        format = 'GROOVY'
-                    } else if (ext.equalsIgnoreCase('toml')) {
-                        format = 'TOML'
-                    } else if (ext.equalsIgnoreCase('properties')) {
-                        format = 'PROPERTIES'
-                    } else if (ext.equalsIgnoreCase('csv')) {
-                        format = 'CSV'
-                    } else if (ext.equalsIgnoreCase('md') || ext.equalsIgnoreCase('markdown')) {
-                        format = 'MARKDOWN'
-                    } else if (ext.equalsIgnoreCase('txt') || ext.equalsIgnoreCase('text')) {
-                        format = 'TEXT'
-                    }
+                    format = formatForExtension(path.extension, true)
                 }
             }
             if (!source && (arg.startsWith('http://') || arg.startsWith('https://'))) {
                 URL url = new URL(arg)
+                if (!format) {
+                    // A remote extension names a data format only. GROOVY is deliberately not
+                    // derivable here: the extension is part of a URL the user typed, but the bytes
+                    // it returns are the server's, so evaluating them has to be asked for with
+                    // -f GROOVY rather than inferred from the address.
+                    format = formatForExtension(extensionOf(url.path), false)
+                }
                 source = new InputStreamReader(url.openStream(), encoding)
             }
             if (source) {
@@ -741,6 +729,45 @@ class GroovyCommands extends JlineCommandRegistry implements CommandRegistry {
         out
     }
 
+    /**
+     * Maps a file extension onto a slurp format, or {@code null} when nothing matches (leaving the
+     * caller to fall back on AUTO, which parses but never evaluates).
+     *
+     * @param ext the extension, without the dot
+     * @param allowCode whether {@code .groovy} may select the evaluating GROOVY format; true for a
+     *                  local file the user named, false for content fetched over the network
+     * @return the format name, or {@code null}
+     */
+    private static String formatForExtension(String ext, boolean allowCode) {
+        if (!ext) return null
+        switch (ext.toLowerCase(Locale.ROOT)) {
+            case 'json': return 'JSON'
+            case 'yaml': case 'yml': return 'YAML'
+            case 'xml': case 'rdf': return 'XML'
+            case 'groovy': return allowCode ? 'GROOVY' : null
+            case 'toml': return 'TOML'
+            case 'properties': return 'PROPERTIES'
+            case 'csv': return 'CSV'
+            case 'md': case 'markdown': return 'MARKDOWN'
+            case 'txt': case 'text': return 'TEXT'
+            default: return null
+        }
+    }
+
+    /**
+     * The extension of a URL path, ignoring any query or fragment already stripped by {@code URL}.
+     *
+     * @param path the URL path
+     * @return the extension without the dot, or {@code null} when the last segment has none
+     */
+    private static String extensionOf(String path) {
+        if (!path) return null
+        int slash = path.lastIndexOf('/')
+        String name = slash < 0 ? path : path.substring(slash + 1)
+        int dot = name.lastIndexOf('.')
+        return dot > 0 ? name.substring(dot + 1) : null
+    }
+
     private static final String GROOVY_CSV_SLURPER = 'groovy.csv.CsvSlurper'
     private static final String COMMONS_CSV_FORMAT = 'org.apache.commons.csv.CSVFormat'
 
@@ -1065,7 +1092,7 @@ class GroovyCommands extends JlineCommandRegistry implements CommandRegistry {
         ], [], [
             '-? --help'               : doDescription('Displays command help'),
             '-e --encoding=ENCODING'  : doDescription('Encoding (default UTF-8)'),
-            '-f --format=FORMAT'      : doDescription('Serialization format')
+            '-f --format=FORMAT'      : doDescription('Parse format; GROOVY evaluates the content, the default never does')
         ])
     }
 
diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java
index 2b83faa509..2f26c7674a 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java
@@ -374,7 +374,9 @@ public class GroovyEngine implements ScriptEngine {
      * Deserializes a string value according to the specified format.
      *
      * @param value the string to deserialize
-     * @param formatStr the format name (JSON, GROOVY, NONE, or AUTO)
+     * @param formatStr the format name (JSON, GROOVY, NONE, or AUTO). {@code AUTO} parses only:
+     *                  it recognises JSON and otherwise returns the text unchanged, and never
+     *                  evaluates the value as Groovy. Use {@code GROOVY} to evaluate.
      * @return the deserialized object
      */
     @Override
@@ -392,32 +394,19 @@ public class GroovyEngine implements ScriptEngine {
                 throw new IllegalArgumentException(e.getMessage());
             }
         } else {
+            // AUTO parses, it does not evaluate. Whether a document is data or code would otherwise
+            // be decided by the shape of the document itself -- a "[...]" payload was evaluated as
+            // Groovy where a "{...}" one was only ever parsed as JSON -- which hands that choice to
+            // whoever supplied the bytes. Evaluating content stays available, but only when the user
+            // asks for it: -f GROOVY, or a .groovy file named explicitly.
             value = value.trim();
-            boolean hasCurly = value.contains("{") && value.contains("}");
             try {
-                if (value.startsWith("[") && value.endsWith("]")) {
-                    try {
-                        if (hasCurly) {
-                            out = Utils.toObject(value); // try json
-                        } else {
-                            out = execute(value);
-                        }
-                    } catch (Exception e) {
-                        if (hasCurly) {
-                            try {
-                                out = execute(value);
-                            } catch (Exception e2) {
-                                // ignore
-                            }
-                        } else {
-                            out = Utils.toObject(value); // try json
-                        }
-                    }
-                } else if (value.startsWith("{") && value.endsWith("}")) {
+                if ((value.startsWith("[") && value.endsWith("]"))
+                        || (value.startsWith("{") && value.endsWith("}"))) {
                     out = Utils.toObject(value);
                 }
             } catch (Exception e) {
-                // ignore
+                // not JSON either; the raw text is the best answer available
             }
         }
         return out;
diff --git a/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc b/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
index cf311f46c7..23fed24bca 100644
--- a/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
+++ b/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
@@ -1186,7 +1186,7 @@ It supports most of the common formats, including JSON, XML, YAML, CSV, TOML, Ma
 image:{reldir_groovysh}/assets/img/repl_slurp.png[Usage of the /slurp command, width=80%]
 
 As you can see in the usage information at the end of the above image,
-you can also provide an encoding and a format. If no format is given,
+you can also provide an encoding and a format. If no encoding is given,
 `UTF_8` is the default. If no format is given, the shell will try to
 determine the format from the extension of the file (if given).
 If no format or file extension is given, the `/slurp` command makes
@@ -1195,6 +1195,17 @@ e.g. the json format is determined in the following example:
 
 image:{reldir_groovysh}/assets/img/repl_slurp_url.png[Usage of the /slurp command with a URL, width=75%]
 
+That content-based fallback *parses*; it never evaluates. It recognises JSON
+and otherwise hands back the text unchanged, so a document cannot decide for
+itself that it should be run. Evaluating content is a separate, explicit
+request: pass `-f GROOVY`, or name a local `.groovy` file.
+
+For a URL, the extension of the URL path selects the format in the same way
+as a filename does, but only ever a data format &#8212; `GROOVY` is not inferred
+from an address, because the extension is part of the URL you typed while the
+bytes are whatever the server chooses to return. Use `-f GROOVY` if you really
+do want to evaluate what a URL serves.
+
 Although the details of the exact object returned are an implementation
 detail which may change in the future, the current behavior is as follows:
 
@@ -1212,6 +1223,8 @@ create a list (the rows) of maps from column name to value.
 `MarkdownDocument` (an `Iterable<Map<String, Object>>` of structured elements). Triggered
 by `.md` and `.markdown` extensions.
 | TEXT | Reads the file as raw lines (or argument as a line).
+| GROOVY | *Evaluates* the content as a Groovy script and returns its result. Selected by `-f GROOVY`,
+or by a local `.groovy` file extension. Never selected automatically from content, nor from a URL.
 | NONE | If you want the raw text rather than parsed content.
 |===
 
diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/jline/GroovyEngineTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/jline/GroovyEngineTest.groovy
index 5310699410..6dda9cb3de 100644
--- a/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/jline/GroovyEngineTest.groovy
+++ b/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/jline/GroovyEngineTest.groovy
@@ -30,6 +30,42 @@ class GroovyEngineTest {
 
     private final GroovyEngine engine = new GroovyEngine()
 
+    // GROOVY-12334
+    @Test
+    void autoDeserializeNeverEvaluatesTheValue() {
+        // a bracketed payload used to reach execute(); the shape of the document must not decide
+        // whether it is treated as data or as code
+        engine.execute('sideEffect = 0')
+        engine.deserialize("[sideEffect = 1, 'x']", 'AUTO')
+        assert engine.execute('sideEffect') == 0
+
+        // and the same for the curly form, which was already parse-only
+        engine.deserialize("{'a': sideEffect = 2}", 'AUTO')
+        assert engine.execute('sideEffect') == 0
+    }
+
+    // GROOVY-12334
+    @Test
+    void autoDeserializeStillParsesJson() {
+        assert engine.deserialize('[1, 2, 3]', 'AUTO').toString() == '[1, 2, 3]'
+        assert engine.deserialize('{"a": 1}', 'AUTO')['a'] == 1
+    }
+
+    // GROOVY-12334
+    @Test
+    void autoDeserializeReturnsUnrecognisedTextUnchanged() {
+        assert engine.deserialize('just some text', 'AUTO') == 'just some text'
+    }
+
+    // GROOVY-12334
+    @Test
+    void groovyFormatStillEvaluates() {
+        // the explicit opt-in is unchanged: -f GROOVY is the documented way to evaluate content
+        engine.execute('marker = 0')
+        engine.deserialize('[marker = 7]', 'GROOVY')
+        assert engine.execute('marker') == 7
+    }
+
     @Test
     void executeReturnsLastValue() {
         assert engine.execute('1 + 1') == 2