2012-02-25 59 views
0

使用JavaScript函數encodeURI/escape和encodeURIComponent似乎存在一個錯誤。示例:Spotify Apps Api - encodeURI/escape

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

註釋顯示輸出。在任何瀏覽器中按預期方式運行Spotify以外的代碼(以+或%20替換空格)執行此代碼。

其他人可以確認這是一個錯誤嗎?或者我在這裏做錯了什麼...?是否有地方報告Spotify應用程序的錯誤?

編輯:顯然上面的例子工作,因爲他們應該。但是,將它們合併到alert()中會顯示一個混亂的字符串,而實際上它是正確的。

回答

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()));

應用不使用這些方法將一個)不能夠顯示元數據或任何其他來自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)); 
+0

好吧,我錯過了一部分。但是,這似乎並沒有解決我個案中的任何問題。只要用文字「The Frames」來嘗試一下,你就會發現不管你做什麼,它都會在調用escape時把它搞亂(即使與decodeForText()結合使用) – 2012-02-29 20:46:02

+0

檢查這個http:// i .imgur.com/NpU36.png – 2012-02-29 20:55:27

+0

你是完全正確的。但不知何故,當你將它放入alert(),那麼alert(escape('The Frames'))時Spotify會弄亂String。仍然失敗。不過這意味着我的'問題'已經解決了。謝謝。 – 2012-02-29 22:29:34