首页 文章

Spotify Apps Api - encodeURI / escape

提问于
浏览
0

使用JavaScript函数encodeURI / escape和encodeURIComponent时似乎存在一个错误 . 例:

escape( 'The Frames' )             // The            0.000000rames
encodeURI( 'The Frames' )          // The            0.000000rames
encodeURIComponent( 'The Frames' ) // The            0.000000rames

评论显示输出 . 在任何浏览器中按照预期的方式在Spotify之外执行此代码(用%或%20替换空格) .

其他人可以证实这是一个错误吗?或者我在这里做错了什么......?有没有地方报告Spotify应用程序的错误?

EDIT: 显然上面的例子按预期工作 . 但是,将它们合并到alert()中会显示混乱的字符串,而实际上它是可以的 .

1 回答

  • 2

    来自guidelines

    编码字符串为了确保应用程序不会以可能不安全的方式使用字符串,Spotify API提供的所有字符串都经过编码,以便意外误用不会导致注入漏洞 . 如果应用程序不解码这些字符串,则使用下面描述的两种方法,字符串将显示为用户的垃圾 . 唯一的例外是URI,它从不编码,因此不需要解码 . API文档说明了每个方法必须解码哪些字符串 . JavaScript字符串中添加了两种方法:decodeForText()和decodeForHTML() . 如果要以安全的方式使用该字符串,例如设置innerText或使用document.createTextNode()创建文本节点,则应使用decodeForText() . 它将返回一个原始的非转义字符串,因此请确保它永远不会插入任何将被解释为HTML的上下文中 . 如果字符串打算进入innerHTML或任何将被解释为HTML的代码片段,则必须使用decodeForHTML() . 它将确保<和>编码为<和>等 . 例如:

    getElementById('song-title').innerHTML = track.title.decodeForHTML(); getElementById('song-title').innerText = track.title.decodeForText(); getElementById('song-title').appendChild(document.createTextNode(track.title.decodeForText()));

    不使用这些方法的应用程序将a)无法显示Spotify API中的元数据或任何其他数据,并且b)将在上载过程中被拒绝 . 还要确保您能够从其中发生的任何地方(例如您的后端服务器)逃脱不安全的HTML字符串 .


    和源代码,以防你好奇:

    String.prototype.decodeForText = function() {
        var result = "";
        for (var i = 0; i < this.length; ++i) {
            if (this.charAt(i) !== "&") {
                result += this.charAt(i);
                continue;
            } else if (this.substring(i, i + 5) === "&amp;") {
                result += "&";
                i += 4;
                continue;
            } else if (this.substring(i, i + 4) === "&lt;") {
                result += "<";
                i += 3;
                continue;
            } else if (this.substring(i, i + 4) === "&gt;") {
                result += ">";
                i += 3;
                continue;
            } else if (this.substring(i, i + 6) === "&quot;") {
                result += "\"";
                i += 5;
                continue;
            } else if (this.substring(i, i + 6) === "&apos;") {
                result += "'";
                i += 5;
                continue;
            } else if (this.substring(i, i + 8) === "&equals;") {
                result += "=";
                i += 7;
                continue;
            }
        }
        return result;
    };
    
    String.prototype.decodeForHTML = function() {
        return this;
    };
    
    String.prototype.decodeForLink = function() {
        return encodeURI(this.decodeForText());
    }
    
    String.prototype.encodeToHTML = function() {
        var result = "";
        for (var i = 0; i < this.length; ++i) {
            if (this.charAt(i) === "&") {
                result += "&amp;";
            } else if (this.charAt(i) === "<") {
                result += "&lt;";
            } else if (this.charAt(i) === ">") {
                result += "&gt;";
            } else if (this.charAt(i) === "\"") {
                result += "&quot;";
            } else if (this.charAt(i) === "'") {
                result += "&apos;";
            } else if (this.charAt(i) === "=") {
                result += "&equals;";
            } else {
                result += this.charAt(i);
            }
        }
        return result;
    }
    }(this));
    

相关问题