NS_DECL_ISUPPORTS // we want to make sure that the names of file can't reach // outside of the type of storage the user asked for. bool isSafePath() { nsAString::const_iterator start, end; mPath.BeginReading(start); mPath.EndReading(end); // if the path has a ~ or \ in it, return false. NS_NAMED_LITERAL_STRING(tilde, "~"); NS_NAMED_LITERAL_STRING(bslash, "\\"); if (FindInReadable(tilde, start, end) || FindInReadable(bslash, start, end)) { return false; } // split on /. if any token is "", ., or .., return false. NS_ConvertUTF16toUTF8 cname(mPath); char* buffer = cname.BeginWriting(); const char* token; while ((token = nsCRT::strtok(buffer, "/", &buffer))) { if (PL_strcmp(token, "") == 0 || PL_strcmp(token, ".") == 0 || PL_strcmp(token, "..") == 0 ) { return false; } } return true; }
nsresult nsWebSocket::ConvertTextToUTF8(const nsString& aMessage, nsCString& buf) { NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread"); nsresult rv; nsCOMPtr<nsICharsetConverterManager> ccm = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); SUCCESS_OR_FAIL_WEBSOCKET(rv, rv); nsCOMPtr<nsIUnicodeEncoder> converter; rv = ccm->GetUnicodeEncoder("UTF-8", getter_AddRefs(converter)); SUCCESS_OR_FAIL_WEBSOCKET(rv, rv); rv = converter->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace, nsnull, UTF_8_REPLACEMENT_CHAR); SUCCESS_OR_FAIL_WEBSOCKET(rv, rv); PRInt32 inLen = aMessage.Length(); PRInt32 maxLen; rv = converter->GetMaxLength(aMessage.BeginReading(), inLen, &maxLen); SUCCESS_OR_FAIL_WEBSOCKET(rv, rv); buf.SetLength(maxLen); TRUE_OR_FAIL_WEBSOCKET(buf.Length() == static_cast<PRUint32>(maxLen), NS_ERROR_OUT_OF_MEMORY); char* start = buf.BeginWriting(); PRInt32 outLen = maxLen; rv = converter->Convert(aMessage.BeginReading(), &inLen, start, &outLen); if (NS_SUCCEEDED(rv)) { PRInt32 outLen2 = maxLen - outLen; rv = converter->Finish(start + outLen, &outLen2); outLen += outLen2; } if (NS_FAILED(rv) || rv == NS_ERROR_UENC_NOMAPPING) { // Yes, NS_ERROR_UENC_NOMAPPING is a success code return NS_ERROR_DOM_SYNTAX_ERR; } buf.SetLength(outLen); TRUE_OR_FAIL_WEBSOCKET(buf.Length() == static_cast<PRUint32>(outLen), NS_ERROR_UNEXPECTED); return NS_OK; }
bool JavaScriptShared::convertGeckoStringToId(JSContext *cx, const nsString &from, JS::MutableHandleId to) { RootedString str(cx, JS_NewUCStringCopyN(cx, from.BeginReading(), from.Length())); if (!str) return false; return JS_ValueToId(cx, StringValue(str), to.address()); }
PLDHashOperator PR_CALLBACK SB_SerializeSelectionListCallback(nsStringHashKey::KeyType aKey, nsString aEntry, void* aUserData) { NS_ASSERTION(aUserData, "Null userData!"); nsIObjectOutputStream* stream = static_cast<nsIObjectOutputStream*>(aUserData); NS_ASSERTION(stream, "Could not cast user data"); nsresult rv; rv = stream->WriteWStringZ(aKey.BeginReading()); NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP); rv = stream->WriteWStringZ(aEntry.BeginReading()); NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP); return PL_DHASH_NEXT; }
nsresult nsWebSocketEstablishedConnection::PostMessage(const nsString& aMessage) { NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread"); if (!mOwner) { return NS_OK; } // only send messages when connected NS_ENSURE_STATE(mStatus >= CONN_CONNECTED_AND_READY); nsresult rv; nsCOMPtr<nsICharsetConverterManager> ccm = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv); nsCOMPtr<nsIUnicodeEncoder> converter; rv = ccm->GetUnicodeEncoder("UTF-8", getter_AddRefs(converter)); ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv); rv = converter->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace, nsnull, UTF_8_REPLACEMENT_CHAR); ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv); PRInt32 inLen = aMessage.Length(); PRInt32 maxLen; rv = converter->GetMaxLength(aMessage.BeginReading(), inLen, &maxLen); ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv); nsCString buf; buf.SetLength(maxLen); ENSURE_TRUE_AND_FAIL_IF_FAILED(buf.Length() == static_cast<PRUint32>(maxLen), NS_ERROR_OUT_OF_MEMORY); char* start = buf.BeginWriting(); PRInt32 outLen = maxLen; rv = converter->Convert(aMessage.BeginReading(), &inLen, start, &outLen); if (NS_SUCCEEDED(rv)) { PRInt32 outLen2 = maxLen - outLen; rv = converter->Finish(start + outLen, &outLen2); outLen += outLen2; } if (NS_FAILED(rv) || rv == NS_ERROR_UENC_NOMAPPING) { // Yes, NS_ERROR_UENC_NOMAPPING is a success code return NS_ERROR_DOM_SYNTAX_ERR; } buf.SetLength(outLen); ENSURE_TRUE_AND_FAIL_IF_FAILED(buf.Length() == static_cast<PRUint32>(outLen), NS_ERROR_UNEXPECTED); if (mStatus == CONN_CLOSED) { NS_ABORT_IF_FALSE(mOwner, "Posting data after disconnecting the websocket"); // the tcp connection has been closed, but the main thread hasn't received // the event for disconnecting the object yet. rv = NS_BASE_STREAM_CLOSED; } else { mOutgoingBufferedAmount += buf.Length(); mWebSocketChannel->SendMsg(buf); rv = NS_OK; } UpdateMustKeepAlive(); ENSURE_SUCCESS_AND_FAIL_IF_FAILED(rv, rv); return NS_OK; }