test_plugin.py 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489
  1. import os.path
  2. import zipfile
  3. from base64 import b64encode
  4. from io import BytesIO
  5. from unittest.mock import patch
  6. from uuid import uuid4
  7. import responses
  8. from django.utils.encoding import force_bytes
  9. from sentry.models import (
  10. ArtifactBundle,
  11. DebugIdArtifactBundle,
  12. File,
  13. Release,
  14. ReleaseArtifactBundle,
  15. ReleaseFile,
  16. SourceFileType,
  17. )
  18. from sentry.models.releasefile import update_artifact_index
  19. from sentry.testutils import RelayStoreHelper, SnubaTestCase, TransactionTestCase
  20. from sentry.testutils.helpers.datetime import before_now, iso_format
  21. from sentry.utils import json
  22. BASE64_SOURCEMAP = "data:application/json;base64," + (
  23. b64encode(
  24. b'{"version":3,"file":"generated.js","sources":["/test.js"],"names":[],"mappings":"AAAA","sourcesContent":['
  25. b'"console.log(\\"hello, World!\\")"]}'
  26. )
  27. .decode("utf-8")
  28. .replace("\n", "")
  29. )
  30. INVALID_BASE64_SOURCEMAP = "data:application/json;base64,A"
  31. def get_fixture_path(name):
  32. return os.path.join(os.path.dirname(__file__), "fixtures", name)
  33. def load_fixture(name):
  34. with open(get_fixture_path(name), "rb") as fp:
  35. return fp.read()
  36. class JavascriptIntegrationTest(RelayStoreHelper, SnubaTestCase, TransactionTestCase):
  37. def setUp(self):
  38. super().setUp()
  39. self.min_ago = iso_format(before_now(minutes=1))
  40. def test_adds_contexts_without_device(self):
  41. data = {
  42. "timestamp": self.min_ago,
  43. "message": "hello",
  44. "platform": "javascript",
  45. "request": {
  46. "url": "http://example.com",
  47. "headers": [
  48. [
  49. "User-Agent",
  50. "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
  51. "Chrome/28.0.1500.72 Safari/537.36",
  52. ]
  53. ],
  54. },
  55. }
  56. event = self.post_and_retrieve_event(data)
  57. contexts = event.interfaces["contexts"].to_json()
  58. assert contexts.get("os") == {"name": "Windows", "version": "8", "type": "os"}
  59. assert contexts.get("device") is None
  60. def test_adds_contexts_with_device(self):
  61. data = {
  62. "timestamp": self.min_ago,
  63. "message": "hello",
  64. "platform": "javascript",
  65. "request": {
  66. "url": "http://example.com",
  67. "headers": [
  68. [
  69. "User-Agent",
  70. "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SCH-R530U Build/JSS15J) AppleWebKit/534.30 ("
  71. "KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 USCC-R530U",
  72. ]
  73. ],
  74. },
  75. }
  76. event = self.post_and_retrieve_event(data)
  77. contexts = event.interfaces["contexts"].to_json()
  78. assert contexts.get("os") == {"name": "Android", "type": "os", "version": "4.3"}
  79. assert contexts.get("browser") == {"name": "Android", "type": "browser", "version": "4.3"}
  80. assert contexts.get("device") == {
  81. "family": "Samsung SCH-R530U",
  82. "type": "device",
  83. "model": "SCH-R530U",
  84. "brand": "Samsung",
  85. }
  86. def test_adds_contexts_with_ps4_device(self):
  87. data = {
  88. "timestamp": self.min_ago,
  89. "message": "hello",
  90. "platform": "javascript",
  91. "request": {
  92. "url": "http://example.com",
  93. "headers": [
  94. [
  95. "User-Agent",
  96. "Mozilla/5.0 (PlayStation 4 3.55) AppleWebKit/537.78 (KHTML, like Gecko)",
  97. ]
  98. ],
  99. },
  100. }
  101. event = self.post_and_retrieve_event(data)
  102. contexts = event.interfaces["contexts"].to_json()
  103. assert contexts.get("os") is None
  104. assert contexts.get("browser") is None
  105. assert contexts.get("device") == {
  106. "family": "PlayStation 4",
  107. "type": "device",
  108. "model": "PlayStation 4",
  109. "brand": "Sony",
  110. }
  111. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  112. def test_source_expansion(self, mock_fetch_by_url):
  113. data = {
  114. "timestamp": self.min_ago,
  115. "message": "hello",
  116. "platform": "javascript",
  117. "exception": {
  118. "values": [
  119. {
  120. "type": "Error",
  121. "stacktrace": {
  122. "frames": [
  123. {
  124. "abs_path": "http://example.com/foo.js",
  125. "filename": "foo.js",
  126. "lineno": 4,
  127. "colno": 0,
  128. },
  129. {
  130. "abs_path": "http://example.com/foo.js",
  131. "filename": "foo.js",
  132. "lineno": 1,
  133. "colno": 0,
  134. },
  135. ]
  136. },
  137. }
  138. ]
  139. },
  140. }
  141. mock_fetch_by_url.return_value.body = force_bytes("\n".join("hello world"))
  142. mock_fetch_by_url.return_value.encoding = None
  143. mock_fetch_by_url.return_value.headers = {}
  144. event = self.post_and_retrieve_event(data)
  145. mock_fetch_by_url.assert_called_once_with("http://example.com/foo.js")
  146. exception = event.interfaces["exception"]
  147. frame_list = exception.values[0].stacktrace.frames
  148. frame = frame_list[0]
  149. assert frame.pre_context == ["h", "e", "l"]
  150. assert frame.context_line == "l"
  151. assert frame.post_context == ["o", " ", "w", "o", "r"]
  152. frame = frame_list[1]
  153. assert not frame.pre_context
  154. assert frame.context_line == "h"
  155. assert frame.post_context == ["e", "l", "l", "o", " "]
  156. # no source map means no raw_stacktrace
  157. assert exception.values[0].raw_stacktrace is None
  158. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  159. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  160. def test_inlined_sources(self, mock_discover_sourcemap, mock_fetch_by_url):
  161. data = {
  162. "timestamp": self.min_ago,
  163. "message": "hello",
  164. "platform": "javascript",
  165. "exception": {
  166. "values": [
  167. {
  168. "type": "Error",
  169. "stacktrace": {
  170. "frames": [
  171. {
  172. "abs_path": "http://example.com/test.min.js",
  173. "filename": "test.js",
  174. "lineno": 1,
  175. "colno": 1,
  176. }
  177. ]
  178. },
  179. }
  180. ]
  181. },
  182. }
  183. mock_discover_sourcemap.return_value = BASE64_SOURCEMAP
  184. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  185. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  186. mock_fetch_by_url.return_value.encoding = None
  187. event = self.post_and_retrieve_event(data)
  188. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  189. exception = event.interfaces["exception"]
  190. frame_list = exception.values[0].stacktrace.frames
  191. frame = frame_list[0]
  192. assert not frame.pre_context
  193. assert frame.context_line == 'console.log("hello, World!")'
  194. assert not frame.post_context
  195. assert frame.data["sourcemap"] == "http://example.com/test.min.js"
  196. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  197. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  198. def test_invalid_base64_sourcemap_returns_an_error(
  199. self, mock_discover_sourcemap, mock_fetch_by_url
  200. ):
  201. data = {
  202. "timestamp": self.min_ago,
  203. "message": "hello",
  204. "platform": "javascript",
  205. "exception": {
  206. "values": [
  207. {
  208. "type": "Error",
  209. "stacktrace": {
  210. "frames": [
  211. {
  212. "abs_path": "http://example.com/test.min.js",
  213. "filename": "test.js",
  214. "lineno": 1,
  215. "colno": 1,
  216. },
  217. ]
  218. },
  219. }
  220. ]
  221. },
  222. }
  223. mock_discover_sourcemap.return_value = INVALID_BASE64_SOURCEMAP
  224. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  225. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  226. mock_fetch_by_url.return_value.encoding = None
  227. event = self.post_and_retrieve_event(data)
  228. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  229. assert len(event.data["errors"]) == 1
  230. assert event.data["errors"][0] == {
  231. "url": "<base64>",
  232. "reason": "Invalid base64-encoded string: "
  233. "number of data characters (1) cannot be 1 more than a multiple of 4",
  234. "type": "js_invalid_source",
  235. }
  236. @patch("sentry.lang.javascript.processor.SmCache.from_bytes")
  237. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  238. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  239. def test_sourcemap_cache_is_constructed_only_once_if_an_error_is_raised(
  240. self, mock_discover_sourcemap, mock_fetch_by_url, mock_from_bytes
  241. ):
  242. data = {
  243. "timestamp": self.min_ago,
  244. "message": "hello",
  245. "platform": "javascript",
  246. "exception": {
  247. "values": [
  248. {
  249. "type": "Error",
  250. "stacktrace": {
  251. "frames": [
  252. {
  253. "abs_path": "http://example.com/test.min.js",
  254. "filename": "test.js",
  255. "lineno": 1,
  256. "colno": 1,
  257. },
  258. {
  259. "abs_path": "http://example.com/test.min.js",
  260. "filename": "test.js",
  261. "lineno": 1,
  262. "colno": 1,
  263. },
  264. {
  265. "abs_path": "http://example.com/test.min.js",
  266. "filename": "test.js",
  267. "lineno": 1,
  268. "colno": 1,
  269. },
  270. ]
  271. },
  272. }
  273. ]
  274. },
  275. }
  276. mock_discover_sourcemap.return_value = BASE64_SOURCEMAP
  277. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  278. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  279. mock_fetch_by_url.return_value.encoding = None
  280. mock_from_bytes.side_effect = Exception()
  281. self.post_and_retrieve_event(data)
  282. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  283. mock_from_bytes.assert_called_once()
  284. @responses.activate
  285. def test_error_message_translations(self):
  286. data = {
  287. "timestamp": self.min_ago,
  288. "message": "hello",
  289. "platform": "javascript",
  290. "logentry": {
  291. "formatted": "ReferenceError: Impossible de d\xe9finir une propri\xe9t\xe9 \xab foo \xbb : objet non "
  292. "extensible"
  293. },
  294. "exception": {
  295. "values": [
  296. {"type": "Error", "value": "P\u0159\xedli\u0161 mnoho soubor\u016f"},
  297. {
  298. "type": "Error",
  299. "value": "foo: wyst\u0105pi\u0142 nieoczekiwany b\u0142\u0105d podczas pr\xf3by uzyskania "
  300. "informacji o metadanych",
  301. },
  302. ]
  303. },
  304. }
  305. event = self.post_and_retrieve_event(data)
  306. message = event.interfaces["logentry"]
  307. assert (
  308. message.formatted
  309. == "ReferenceError: Cannot define property 'foo': object is not extensible"
  310. )
  311. exception = event.interfaces["exception"]
  312. assert exception.values[0].value == "Too many files"
  313. assert (
  314. exception.values[1].value
  315. == "foo: an unexpected failure occurred while trying to obtain metadata information"
  316. )
  317. @responses.activate
  318. def test_sourcemap_source_expansion(self):
  319. responses.add(
  320. responses.GET,
  321. "http://example.com/file.min.js",
  322. body=load_fixture("file.min.js"),
  323. content_type="application/javascript; charset=utf-8",
  324. )
  325. responses.add(
  326. responses.GET,
  327. "http://example.com/file1.js",
  328. body=load_fixture("file1.js"),
  329. content_type="application/javascript; charset=utf-8",
  330. )
  331. responses.add(
  332. responses.GET,
  333. "http://example.com/file2.js",
  334. body=load_fixture("file2.js"),
  335. content_type="application/javascript; charset=utf-8",
  336. )
  337. responses.add(
  338. responses.GET,
  339. "http://example.com/file.sourcemap.js",
  340. body=load_fixture("file.sourcemap.js"),
  341. content_type="application/javascript; charset=utf-8",
  342. )
  343. responses.add(responses.GET, "http://example.com/index.html", body="Not Found", status=404)
  344. data = {
  345. "timestamp": self.min_ago,
  346. "message": "hello",
  347. "platform": "javascript",
  348. "exception": {
  349. "values": [
  350. {
  351. "type": "Error",
  352. "stacktrace": {
  353. "frames": [
  354. {
  355. "abs_path": "http://example.com/file.min.js",
  356. "filename": "file.min.js",
  357. "lineno": 1,
  358. "colno": 39,
  359. },
  360. # NOTE: Intentionally source is not retrieved from this HTML file
  361. {
  362. "function": 'function: "HTMLDocument.<anonymous>"',
  363. "abs_path": "http//example.com/index.html",
  364. "filename": "index.html",
  365. "lineno": 283,
  366. "colno": 17,
  367. "in_app": False,
  368. },
  369. ]
  370. },
  371. }
  372. ]
  373. },
  374. }
  375. event = self.post_and_retrieve_event(data)
  376. assert event.data["errors"] == [
  377. {"type": "js_no_source", "url": "http//example.com/index.html"}
  378. ]
  379. exception = event.interfaces["exception"]
  380. frame_list = exception.values[0].stacktrace.frames
  381. frame = frame_list[0]
  382. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  383. expected = "\treturn a + b; // fôo"
  384. assert frame.context_line == expected
  385. assert frame.post_context == ["}", ""]
  386. raw_frame_list = exception.values[0].raw_stacktrace.frames
  387. raw_frame = raw_frame_list[0]
  388. assert not raw_frame.pre_context
  389. assert (
  390. raw_frame.context_line
  391. == 'function add(a,b){"use strict";return a+b}function multiply(a,b){"use strict";return a*b}function '
  392. 'divide(a,b){"use strict";try{return multip {snip}'
  393. )
  394. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js", ""]
  395. assert raw_frame.lineno == 1
  396. # Since we couldn't expand source for the 2nd frame, both
  397. # its raw and original form should be identical
  398. assert raw_frame_list[1] == frame_list[1]
  399. @responses.activate
  400. def test_sourcemap_embedded_source_expansion(self):
  401. responses.add(
  402. responses.GET,
  403. "http://example.com/embedded.js",
  404. body=load_fixture("embedded.js"),
  405. content_type="application/javascript; charset=utf-8",
  406. )
  407. responses.add(
  408. responses.GET,
  409. "http://example.com/embedded.js.map",
  410. body=load_fixture("embedded.js.map"),
  411. content_type="application/json; charset=utf-8",
  412. )
  413. responses.add(responses.GET, "http://example.com/index.html", body="Not Found", status=404)
  414. data = {
  415. "timestamp": self.min_ago,
  416. "message": "hello",
  417. "platform": "javascript",
  418. "exception": {
  419. "values": [
  420. {
  421. "type": "Error",
  422. "stacktrace": {
  423. "frames": [
  424. {
  425. "abs_path": "http://example.com/embedded.js",
  426. "filename": "file.min.js",
  427. "lineno": 1,
  428. "colno": 39,
  429. },
  430. # NOTE: Intentionally source is not retrieved from this HTML file
  431. {
  432. "function": 'function: "HTMLDocument.<anonymous>"',
  433. "abs_path": "http//example.com/index.html",
  434. "filename": "index.html",
  435. "lineno": 283,
  436. "colno": 17,
  437. "in_app": False,
  438. },
  439. ]
  440. },
  441. }
  442. ]
  443. },
  444. }
  445. event = self.post_and_retrieve_event(data)
  446. assert event.data["errors"] == [
  447. {"type": "js_no_source", "url": "http//example.com/index.html"}
  448. ]
  449. exception = event.interfaces["exception"]
  450. frame_list = exception.values[0].stacktrace.frames
  451. frame = frame_list[0]
  452. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  453. expected = "\treturn a + b; // fôo"
  454. assert frame.context_line == expected
  455. assert frame.post_context == ["}", ""]
  456. @responses.activate
  457. def test_sourcemap_nofiles_source_expansion(self):
  458. project = self.project
  459. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  460. release.add_project(project)
  461. with open(get_fixture_path("nofiles.js"), "rb") as f:
  462. f_minified = File.objects.create(
  463. name="nofiles.js", type="release.file", headers={"Content-Type": "application/json"}
  464. )
  465. f_minified.putfile(f)
  466. ReleaseFile.objects.create(
  467. name=f"~/{f_minified.name}",
  468. release_id=release.id,
  469. organization_id=project.organization_id,
  470. file=f_minified,
  471. )
  472. with open(get_fixture_path("nofiles.js.map"), "rb") as f:
  473. f_sourcemap = File.objects.create(
  474. name="nofiles.js.map",
  475. type="release.file",
  476. headers={"Content-Type": "application/json"},
  477. )
  478. f_sourcemap.putfile(f)
  479. ReleaseFile.objects.create(
  480. name=f"app:///{f_sourcemap.name}",
  481. release_id=release.id,
  482. organization_id=project.organization_id,
  483. file=f_sourcemap,
  484. )
  485. data = {
  486. "timestamp": self.min_ago,
  487. "message": "hello",
  488. "platform": "javascript",
  489. "release": "abc",
  490. "exception": {
  491. "values": [
  492. {
  493. "type": "Error",
  494. "stacktrace": {
  495. "frames": [{"abs_path": "app:///nofiles.js", "lineno": 1, "colno": 39}]
  496. },
  497. }
  498. ]
  499. },
  500. }
  501. event = self.post_and_retrieve_event(data)
  502. assert "errors" not in event.data
  503. exception = event.interfaces["exception"]
  504. frame_list = exception.values[0].stacktrace.frames
  505. assert len(frame_list) == 1
  506. frame = frame_list[0]
  507. assert frame.abs_path == "app:///nofiles.js"
  508. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  509. assert frame.context_line == "\treturn a + b; // fôo"
  510. assert frame.post_context == ["}", ""]
  511. @responses.activate
  512. def test_indexed_sourcemap_source_expansion(self):
  513. responses.add(
  514. responses.GET,
  515. "http://example.com/indexed.min.js",
  516. body=load_fixture("indexed.min.js"),
  517. content_type="application/javascript; charset=utf-8",
  518. )
  519. responses.add(
  520. responses.GET,
  521. "http://example.com/file1.js",
  522. body=load_fixture("file1.js"),
  523. content_type="application/javascript; charset=utf-8",
  524. )
  525. responses.add(
  526. responses.GET,
  527. "http://example.com/file2.js",
  528. body=load_fixture("file2.js"),
  529. content_type="application/javascript; charset=utf-8",
  530. )
  531. responses.add(
  532. responses.GET,
  533. "http://example.com/indexed.sourcemap.js",
  534. body=load_fixture("indexed.sourcemap.js"),
  535. content_type="application/json; charset=utf-8",
  536. )
  537. data = {
  538. "timestamp": self.min_ago,
  539. "message": "hello",
  540. "platform": "javascript",
  541. "exception": {
  542. "values": [
  543. {
  544. "type": "Error",
  545. "stacktrace": {
  546. "frames": [
  547. {
  548. "abs_path": "http://example.com/indexed.min.js",
  549. "filename": "indexed.min.js",
  550. "lineno": 1,
  551. "colno": 39,
  552. },
  553. {
  554. "abs_path": "http://example.com/indexed.min.js",
  555. "filename": "indexed.min.js",
  556. "lineno": 2,
  557. "colno": 44,
  558. },
  559. ]
  560. },
  561. }
  562. ]
  563. },
  564. }
  565. event = self.post_and_retrieve_event(data)
  566. assert "errors" not in event.data
  567. exception = event.interfaces["exception"]
  568. frame_list = exception.values[0].stacktrace.frames
  569. frame = frame_list[0]
  570. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  571. expected = "\treturn a + b; // fôo"
  572. assert frame.context_line == expected
  573. assert frame.post_context == ["}", ""]
  574. raw_frame_list = exception.values[0].raw_stacktrace.frames
  575. raw_frame = raw_frame_list[0]
  576. assert not raw_frame.pre_context
  577. assert raw_frame.context_line == 'function add(a,b){"use strict";return a+b}'
  578. assert raw_frame.post_context == [
  579. 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  580. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}",
  581. "//# sourceMappingURL=indexed.sourcemap.js",
  582. "",
  583. ]
  584. assert raw_frame.lineno == 1
  585. frame = frame_list[1]
  586. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  587. assert frame.context_line == "\treturn a * b;"
  588. assert frame.post_context == [
  589. "}",
  590. "function divide(a, b) {",
  591. '\t"use strict";',
  592. "\ttry {",
  593. "\t\treturn multiply(add(a, b), a, b) / c;",
  594. ]
  595. raw_frame = raw_frame_list[1]
  596. assert raw_frame.pre_context == ['function add(a,b){"use strict";return a+b}']
  597. assert (
  598. raw_frame.context_line
  599. == 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  600. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}"
  601. )
  602. assert raw_frame.post_context == ["//# sourceMappingURL=indexed.sourcemap.js", ""]
  603. assert raw_frame.lineno == 2
  604. @responses.activate
  605. def test_expansion_via_debug(self):
  606. project = self.project
  607. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  608. release.add_project(project)
  609. # file.min.js
  610. # ------------
  611. with open(get_fixture_path("file.min.js"), "rb") as f:
  612. f_minified = File.objects.create(
  613. name="file.min.js",
  614. type="release.file",
  615. headers={"Content-Type": "application/json"},
  616. )
  617. f_minified.putfile(f)
  618. # Intentionally omit hostname - use alternate artifact path lookup instead
  619. # /file1.js vs http://example.com/file1.js
  620. ReleaseFile.objects.create(
  621. name=f"~/{f_minified.name}?foo=bar",
  622. release_id=release.id,
  623. organization_id=project.organization_id,
  624. file=f_minified,
  625. )
  626. # file1.js
  627. # ---------
  628. with open(get_fixture_path("file1.js"), "rb") as f:
  629. f1 = File.objects.create(
  630. name="file1.js", type="release.file", headers={"Content-Type": "application/json"}
  631. )
  632. f1.putfile(f)
  633. ReleaseFile.objects.create(
  634. name=f"http://example.com/{f1.name}",
  635. release_id=release.id,
  636. organization_id=project.organization_id,
  637. file=f1,
  638. )
  639. # file2.js
  640. # ----------
  641. with open(get_fixture_path("file2.js"), "rb") as f:
  642. f2 = File.objects.create(
  643. name="file2.js", type="release.file", headers={"Content-Type": "application/json"}
  644. )
  645. f2.putfile(f)
  646. ReleaseFile.objects.create(
  647. name=f"http://example.com/{f2.name}",
  648. release_id=release.id,
  649. organization_id=project.organization_id,
  650. file=f2,
  651. )
  652. # To verify that the full url has priority over the relative url,
  653. # we will also add a second ReleaseFile alias for file2.js (f3) w/o
  654. # hostname that points to an empty file. If the processor chooses
  655. # this empty file over the correct file2.js, it will not locate
  656. # context for the 2nd frame.
  657. with open(get_fixture_path("empty.js"), "rb") as f:
  658. f2_empty = File.objects.create(
  659. name="empty.js", type="release.file", headers={"Content-Type": "application/json"}
  660. )
  661. f2_empty.putfile(f)
  662. ReleaseFile.objects.create(
  663. name=f"~/{f2.name}", # intentionally using f2.name ("file2.js")
  664. release_id=release.id,
  665. organization_id=project.organization_id,
  666. file=f2_empty,
  667. )
  668. # sourcemap
  669. # ----------
  670. with open(get_fixture_path("file.sourcemap.js"), "rb") as f:
  671. f_sourcemap = File.objects.create(
  672. name="file.sourcemap.js",
  673. type="release.file",
  674. headers={"Content-Type": "application/json"},
  675. )
  676. f_sourcemap.putfile(f)
  677. ReleaseFile.objects.create(
  678. name=f"http://example.com/{f_sourcemap.name}",
  679. release_id=release.id,
  680. organization_id=project.organization_id,
  681. file=f_sourcemap,
  682. )
  683. data = {
  684. "timestamp": self.min_ago,
  685. "message": "hello",
  686. "platform": "javascript",
  687. "release": "abc",
  688. "exception": {
  689. "values": [
  690. {
  691. "type": "Error",
  692. "stacktrace": {
  693. "frames": [
  694. {
  695. "abs_path": "http://example.com/file.min.js?foo=bar",
  696. "filename": "file.min.js",
  697. "lineno": 1,
  698. "colno": 39,
  699. },
  700. {
  701. "abs_path": "http://example.com/file.min.js?foo=bar",
  702. "filename": "file.min.js",
  703. "lineno": 1,
  704. "colno": 79,
  705. },
  706. ]
  707. },
  708. }
  709. ]
  710. },
  711. }
  712. event = self.post_and_retrieve_event(data)
  713. assert "errors" not in event.data
  714. exception = event.interfaces["exception"]
  715. frame_list = exception.values[0].stacktrace.frames
  716. frame = frame_list[0]
  717. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  718. assert frame.context_line == "\treturn a + b; // fôo"
  719. assert frame.post_context == ["}", ""]
  720. frame = frame_list[1]
  721. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  722. assert frame.context_line == "\treturn a * b;"
  723. assert frame.post_context == [
  724. "}",
  725. "function divide(a, b) {",
  726. '\t"use strict";',
  727. "\ttry {",
  728. "\t\treturn multiply(add(a, b), a, b) / c;",
  729. ]
  730. @responses.activate
  731. def test_expansion_via_distribution_release_artifacts(self):
  732. project = self.project
  733. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  734. release.add_project(project)
  735. dist = release.add_dist("foo")
  736. # file.min.js
  737. # ------------
  738. with open(get_fixture_path("file.min.js"), "rb") as f:
  739. f_minified = File.objects.create(
  740. name="file.min.js",
  741. type="release.file",
  742. headers={"Content-Type": "application/json"},
  743. )
  744. f_minified.putfile(f)
  745. # Intentionally omit hostname - use alternate artifact path lookup instead
  746. # /file1.js vs http://example.com/file1.js
  747. ReleaseFile.objects.create(
  748. name=f"~/{f_minified.name}?foo=bar",
  749. release_id=release.id,
  750. dist_id=dist.id,
  751. organization_id=project.organization_id,
  752. file=f_minified,
  753. )
  754. # file1.js
  755. # ---------
  756. with open(get_fixture_path("file1.js"), "rb") as f:
  757. f1 = File.objects.create(
  758. name="file1.js", type="release.file", headers={"Content-Type": "application/json"}
  759. )
  760. f1.putfile(f)
  761. ReleaseFile.objects.create(
  762. name=f"http://example.com/{f1.name}",
  763. release_id=release.id,
  764. dist_id=dist.id,
  765. organization_id=project.organization_id,
  766. file=f1,
  767. )
  768. # file2.js
  769. # ----------
  770. with open(get_fixture_path("file2.js"), "rb") as f:
  771. f2 = File.objects.create(
  772. name="file2.js", type="release.file", headers={"Content-Type": "application/json"}
  773. )
  774. f2.putfile(f)
  775. ReleaseFile.objects.create(
  776. name=f"http://example.com/{f2.name}",
  777. release_id=release.id,
  778. dist_id=dist.id,
  779. organization_id=project.organization_id,
  780. file=f2,
  781. )
  782. # To verify that the full url has priority over the relative url,
  783. # we will also add a second ReleaseFile alias for file2.js (f3) w/o
  784. # hostname that points to an empty file. If the processor chooses
  785. # this empty file over the correct file2.js, it will not locate
  786. # context for the 2nd frame.
  787. with open(get_fixture_path("empty.js"), "rb") as f:
  788. f2_empty = File.objects.create(
  789. name="empty.js", type="release.file", headers={"Content-Type": "application/json"}
  790. )
  791. f2_empty.putfile(f)
  792. ReleaseFile.objects.create(
  793. name=f"~/{f2.name}", # intentionally using f2.name ("file2.js")
  794. release_id=release.id,
  795. dist_id=dist.id,
  796. organization_id=project.organization_id,
  797. file=f2_empty,
  798. )
  799. # sourcemap
  800. # ----------
  801. with open(get_fixture_path("file.sourcemap.js"), "rb") as f:
  802. f_sourcemap = File.objects.create(
  803. name="file.sourcemap.js",
  804. type="release.file",
  805. headers={"Content-Type": "application/json"},
  806. )
  807. f_sourcemap.putfile(f)
  808. ReleaseFile.objects.create(
  809. name=f"http://example.com/{f_sourcemap.name}",
  810. release_id=release.id,
  811. dist_id=dist.id,
  812. organization_id=project.organization_id,
  813. file=f_sourcemap,
  814. )
  815. data = {
  816. "timestamp": self.min_ago,
  817. "message": "hello",
  818. "platform": "javascript",
  819. "release": "abc",
  820. "dist": "foo",
  821. "exception": {
  822. "values": [
  823. {
  824. "type": "Error",
  825. "stacktrace": {
  826. "frames": [
  827. {
  828. "abs_path": "http://example.com/file.min.js?foo=bar",
  829. "filename": "file.min.js",
  830. "lineno": 1,
  831. "colno": 39,
  832. },
  833. {
  834. "abs_path": "http://example.com/file.min.js?foo=bar",
  835. "filename": "file.min.js",
  836. "lineno": 1,
  837. "colno": 79,
  838. },
  839. ]
  840. },
  841. }
  842. ]
  843. },
  844. }
  845. event = self.post_and_retrieve_event(data)
  846. assert "errors" not in event.data
  847. exception = event.interfaces["exception"]
  848. frame_list = exception.values[0].stacktrace.frames
  849. frame = frame_list[0]
  850. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  851. assert frame.context_line == "\treturn a + b; // fôo"
  852. assert frame.post_context == ["}", ""]
  853. frame = frame_list[1]
  854. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  855. assert frame.context_line == "\treturn a * b;"
  856. assert frame.post_context == [
  857. "}",
  858. "function divide(a, b) {",
  859. '\t"use strict";',
  860. "\ttry {",
  861. "\t\treturn multiply(add(a, b), a, b) / c;",
  862. ]
  863. @responses.activate
  864. def test_sourcemap_expansion_with_missing_source(self):
  865. """
  866. Tests a successful sourcemap expansion that points to source files
  867. that are not found.
  868. """
  869. responses.add(
  870. responses.GET,
  871. "http://example.com/file.min.js",
  872. body=load_fixture("file.min.js"),
  873. content_type="application/javascript; charset=utf-8",
  874. )
  875. responses.add(
  876. responses.GET,
  877. "http://example.com/file.sourcemap.js",
  878. body=load_fixture("file.sourcemap.js"),
  879. content_type="application/json; charset=utf-8",
  880. )
  881. responses.add(responses.GET, "http://example.com/file1.js", body="Not Found", status=404)
  882. data = {
  883. "timestamp": self.min_ago,
  884. "message": "hello",
  885. "platform": "javascript",
  886. "exception": {
  887. "values": [
  888. {
  889. "type": "Error",
  890. "stacktrace": {
  891. # Add two frames. We only want to see the
  892. # error once though.
  893. "frames": [
  894. {
  895. "abs_path": "http://example.com/file.min.js",
  896. "filename": "file.min.js",
  897. "lineno": 1,
  898. "colno": 39,
  899. },
  900. {
  901. "abs_path": "http://example.com/file.min.js",
  902. "filename": "file.min.js",
  903. "lineno": 1,
  904. "colno": 39,
  905. },
  906. ]
  907. },
  908. }
  909. ]
  910. },
  911. }
  912. event = self.post_and_retrieve_event(data)
  913. assert event.data["errors"] == [
  914. {"url": "http://example.com/file1.js", "type": "fetch_invalid_http_code", "value": 404}
  915. ]
  916. exception = event.interfaces["exception"]
  917. frame_list = exception.values[0].stacktrace.frames
  918. frame = frame_list[0]
  919. # no context information ...
  920. assert not frame.pre_context
  921. assert not frame.context_line
  922. assert not frame.post_context
  923. # ... but line, column numbers are still correctly mapped
  924. assert frame.lineno == 3
  925. assert frame.colno == 9
  926. @responses.activate
  927. def test_failed_sourcemap_expansion(self):
  928. """
  929. Tests attempting to parse an indexed source map where each section has a "url"
  930. property - this is unsupported and should fail.
  931. """
  932. responses.add(
  933. responses.GET,
  934. "http://example.com/unsupported.min.js",
  935. body=load_fixture("unsupported.min.js"),
  936. content_type="application/javascript; charset=utf-8",
  937. )
  938. responses.add(
  939. responses.GET,
  940. "http://example.com/unsupported.sourcemap.js",
  941. body=load_fixture("unsupported.sourcemap.js"),
  942. content_type="application/json; charset=utf-8",
  943. )
  944. data = {
  945. "timestamp": self.min_ago,
  946. "message": "hello",
  947. "platform": "javascript",
  948. "exception": {
  949. "values": [
  950. {
  951. "type": "Error",
  952. "stacktrace": {
  953. "frames": [
  954. {
  955. "abs_path": "http://example.com/unsupported.min.js",
  956. "filename": "indexed.min.js",
  957. "lineno": 1,
  958. "colno": 39,
  959. }
  960. ]
  961. },
  962. }
  963. ]
  964. },
  965. }
  966. event = self.post_and_retrieve_event(data)
  967. assert event.data["errors"] == [
  968. {"url": "http://example.com/unsupported.sourcemap.js", "type": "js_invalid_source"}
  969. ]
  970. def test_failed_sourcemap_expansion_data_url(self):
  971. data = {
  972. "timestamp": self.min_ago,
  973. "message": "hello",
  974. "platform": "javascript",
  975. "exception": {
  976. "values": [
  977. {
  978. "type": "Error",
  979. "stacktrace": {
  980. "frames": [
  981. {
  982. "abs_path": "data:application/javascript,base46,asfasf",
  983. "filename": "indexed.min.js",
  984. "lineno": 1,
  985. "colno": 39,
  986. }
  987. ]
  988. },
  989. }
  990. ]
  991. },
  992. }
  993. event = self.post_and_retrieve_event(data)
  994. assert event.data["errors"] == [{"url": "<data url>", "type": "js_no_source"}]
  995. @responses.activate
  996. def test_failed_sourcemap_expansion_missing_location_entirely(self):
  997. responses.add(
  998. responses.GET,
  999. "http://example.com/indexed.min.js",
  1000. body="//# sourceMappingURL=indexed.sourcemap.js",
  1001. )
  1002. responses.add(responses.GET, "http://example.com/indexed.sourcemap.js", body="{}")
  1003. data = {
  1004. "timestamp": self.min_ago,
  1005. "message": "hello",
  1006. "platform": "javascript",
  1007. "exception": {
  1008. "values": [
  1009. {
  1010. "type": "Error",
  1011. "stacktrace": {
  1012. "frames": [
  1013. {
  1014. "abs_path": "http://example.com/indexed.min.js",
  1015. "filename": "indexed.min.js",
  1016. "lineno": 1,
  1017. "colno": 1,
  1018. },
  1019. {
  1020. "abs_path": "http://example.com/indexed.min.js",
  1021. "filename": "indexed.min.js",
  1022. },
  1023. ]
  1024. },
  1025. }
  1026. ]
  1027. },
  1028. }
  1029. event = self.post_and_retrieve_event(data)
  1030. assert "errors" not in event.data
  1031. @responses.activate
  1032. def test_html_response_for_js(self):
  1033. responses.add(
  1034. responses.GET,
  1035. "http://example.com/file1.js",
  1036. body=" <!DOCTYPE html><html><head></head><body></body></html>",
  1037. )
  1038. responses.add(
  1039. responses.GET,
  1040. "http://example.com/file2.js",
  1041. body="<!doctype html><html><head></head><body></body></html>",
  1042. )
  1043. responses.add(
  1044. responses.GET,
  1045. "http://example.com/file.html",
  1046. body=(
  1047. "<!doctype html><html><head></head><body><script>/*legit case*/</script></body></html>"
  1048. ),
  1049. )
  1050. data = {
  1051. "timestamp": self.min_ago,
  1052. "message": "hello",
  1053. "platform": "javascript",
  1054. "exception": {
  1055. "values": [
  1056. {
  1057. "type": "Error",
  1058. "stacktrace": {
  1059. "frames": [
  1060. {
  1061. "abs_path": "http://example.com/file1.js",
  1062. "filename": "file.min.js",
  1063. "lineno": 1,
  1064. "colno": 39,
  1065. },
  1066. {
  1067. "abs_path": "http://example.com/file2.js",
  1068. "filename": "file.min.js",
  1069. "lineno": 1,
  1070. "colno": 39,
  1071. },
  1072. {
  1073. "abs_path": "http://example.com/file.html",
  1074. "filename": "file.html",
  1075. "lineno": 1,
  1076. "colno": 1,
  1077. },
  1078. ]
  1079. },
  1080. }
  1081. ]
  1082. },
  1083. }
  1084. event = self.post_and_retrieve_event(data)
  1085. assert event.data["errors"] == [
  1086. {"url": "http://example.com/file1.js", "type": "js_invalid_content"},
  1087. {"url": "http://example.com/file2.js", "type": "js_invalid_content"},
  1088. ]
  1089. def _test_expansion_via_release_archive(self, link_sourcemaps: bool):
  1090. project = self.project
  1091. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1092. release.add_project(project)
  1093. manifest = {
  1094. "org": self.organization.slug,
  1095. "release": release.version,
  1096. "files": {
  1097. "files/_/_/file.min.js": {
  1098. "url": "http://example.com/file.min.js",
  1099. },
  1100. "files/_/_/file1.js": {
  1101. "url": "http://example.com/file1.js",
  1102. },
  1103. "files/_/_/file2.js": {
  1104. "url": "http://example.com/file2.js",
  1105. },
  1106. "files/_/_/file.sourcemap.js": {
  1107. "url": "http://example.com/file.sourcemap.js",
  1108. },
  1109. },
  1110. }
  1111. file_like = BytesIO()
  1112. with zipfile.ZipFile(file_like, "w") as zip:
  1113. for rel_path, entry in manifest["files"].items():
  1114. name = os.path.basename(rel_path)
  1115. content = load_fixture(name)
  1116. if name == "file.min.js" and not link_sourcemaps:
  1117. # Remove link to source map, add to header instead
  1118. content = content.replace(b"//@ sourceMappingURL=file.sourcemap.js", b"")
  1119. entry["headers"] = {"SourceMap": "/file.sourcemap.js"}
  1120. zip.writestr(rel_path, content)
  1121. zip.writestr("manifest.json", json.dumps(manifest))
  1122. file_like.seek(0)
  1123. file = File.objects.create(name="doesnt_matter", type="release.bundle")
  1124. file.putfile(file_like)
  1125. update_artifact_index(release, None, file)
  1126. data = {
  1127. "timestamp": self.min_ago,
  1128. "message": "hello",
  1129. "platform": "javascript",
  1130. "release": "abc",
  1131. "exception": {
  1132. "values": [
  1133. {
  1134. "type": "Error",
  1135. "stacktrace": {
  1136. "frames": [
  1137. {
  1138. "abs_path": "http://example.com/file.min.js",
  1139. "filename": "file.min.js",
  1140. "lineno": 1,
  1141. "colno": 39,
  1142. },
  1143. {
  1144. "abs_path": "http://example.com/file.min.js",
  1145. "filename": "file.min.js",
  1146. "lineno": 1,
  1147. "colno": 79,
  1148. },
  1149. ]
  1150. },
  1151. }
  1152. ]
  1153. },
  1154. }
  1155. event = self.post_and_retrieve_event(data)
  1156. assert "errors" not in event.data
  1157. exception = event.interfaces["exception"]
  1158. frame_list = exception.values[0].stacktrace.frames
  1159. frame = frame_list[0]
  1160. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1161. assert frame.context_line == "\treturn a + b; // fôo"
  1162. assert frame.post_context == ["}", ""]
  1163. frame = frame_list[1]
  1164. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1165. assert frame.context_line == "\treturn a * b;"
  1166. assert frame.post_context == [
  1167. "}",
  1168. "function divide(a, b) {",
  1169. '\t"use strict";',
  1170. "\ttry {",
  1171. "\t\treturn multiply(add(a, b), a, b) / c;",
  1172. ]
  1173. def test_expansion_via_release_archive(self):
  1174. self._test_expansion_via_release_archive(link_sourcemaps=True)
  1175. def test_expansion_via_release_archive_no_sourcemap_link(self):
  1176. self._test_expansion_via_release_archive(link_sourcemaps=False)
  1177. def test_node_processing(self):
  1178. project = self.project
  1179. release = Release.objects.create(
  1180. organization_id=project.organization_id, version="nodeabc123"
  1181. )
  1182. release.add_project(project)
  1183. with open(get_fixture_path("dist.bundle.js"), "rb") as f:
  1184. f_minified = File.objects.create(
  1185. name="dist.bundle.js",
  1186. type="release.file",
  1187. headers={"Content-Type": "application/javascript"},
  1188. )
  1189. f_minified.putfile(f)
  1190. ReleaseFile.objects.create(
  1191. name=f"~/{f_minified.name}",
  1192. release_id=release.id,
  1193. organization_id=project.organization_id,
  1194. file=f_minified,
  1195. )
  1196. with open(get_fixture_path("dist.bundle.js.map"), "rb") as f:
  1197. f_sourcemap = File.objects.create(
  1198. name="dist.bundle.js.map",
  1199. type="release.file",
  1200. headers={"Content-Type": "application/javascript"},
  1201. )
  1202. f_sourcemap.putfile(f)
  1203. ReleaseFile.objects.create(
  1204. name=f"~/{f_sourcemap.name}",
  1205. release_id=release.id,
  1206. organization_id=project.organization_id,
  1207. file=f_sourcemap,
  1208. )
  1209. data = {
  1210. "timestamp": self.min_ago,
  1211. "message": "hello",
  1212. "platform": "node",
  1213. "release": "nodeabc123",
  1214. "exception": {
  1215. "values": [
  1216. {
  1217. "type": "Error",
  1218. "stacktrace": {
  1219. "frames": [
  1220. {
  1221. "filename": "app:///dist.bundle.js",
  1222. "function": "bar",
  1223. "lineno": 9,
  1224. "colno": 2321,
  1225. },
  1226. {
  1227. "filename": "app:///dist.bundle.js",
  1228. "function": "foo",
  1229. "lineno": 3,
  1230. "colno": 2308,
  1231. },
  1232. {
  1233. "filename": "app:///dist.bundle.js",
  1234. "function": "App",
  1235. "lineno": 3,
  1236. "colno": 1011,
  1237. },
  1238. {
  1239. "filename": "app:///dist.bundle.js",
  1240. "function": "Object.<anonymous>",
  1241. "lineno": 1,
  1242. "colno": 1014,
  1243. },
  1244. {
  1245. "filename": "app:///dist.bundle.js",
  1246. "function": "__webpack_require__",
  1247. "lineno": 20,
  1248. "colno": 30,
  1249. },
  1250. {
  1251. "filename": "app:///dist.bundle.js",
  1252. "function": "<unknown>",
  1253. "lineno": 18,
  1254. "colno": 63,
  1255. },
  1256. ]
  1257. },
  1258. }
  1259. ]
  1260. },
  1261. }
  1262. event = self.post_and_retrieve_event(data)
  1263. exception = event.interfaces["exception"]
  1264. frame_list = exception.values[0].stacktrace.frames
  1265. assert len(frame_list) == 6
  1266. assert frame_list[0].abs_path == "webpack:///webpack/bootstrap d9a5a31d9276b73873d3"
  1267. assert frame_list[0].function == "bar"
  1268. assert frame_list[0].lineno == 8
  1269. assert frame_list[1].abs_path == "webpack:///webpack/bootstrap d9a5a31d9276b73873d3"
  1270. assert frame_list[1].function == "foo"
  1271. assert frame_list[1].lineno == 2
  1272. assert frame_list[2].abs_path == "webpack:///webpack/bootstrap d9a5a31d9276b73873d3"
  1273. assert frame_list[2].function == "App"
  1274. assert frame_list[2].lineno == 2
  1275. assert frame_list[3].abs_path == "webpack:///webpack/bootstrap d9a5a31d9276b73873d3"
  1276. assert frame_list[3].function == "Object.<anonymous>"
  1277. assert frame_list[3].lineno == 1
  1278. assert frame_list[4].abs_path == "webpack:///webpack/bootstrap d9a5a31d9276b73873d3"
  1279. assert frame_list[4].function == "__webpack_require__"
  1280. assert frame_list[4].lineno == 19
  1281. assert frame_list[5].abs_path == "webpack:///webpack/bootstrap d9a5a31d9276b73873d3"
  1282. assert frame_list[5].function == "<unknown>"
  1283. assert frame_list[5].lineno == 16
  1284. @responses.activate
  1285. def test_no_fetch_from_http(self):
  1286. responses.add(
  1287. responses.GET,
  1288. "http://example.com/node_app.min.js",
  1289. body=load_fixture("node_app.min.js"),
  1290. content_type="application/javascript; charset=utf-8",
  1291. )
  1292. responses.add(
  1293. responses.GET,
  1294. "http://example.com/node_app.min.js.map",
  1295. body=load_fixture("node_app.min.js.map"),
  1296. content_type="application/javascript; charset=utf-8",
  1297. )
  1298. data = {
  1299. "timestamp": self.min_ago,
  1300. "message": "hello",
  1301. "platform": "node",
  1302. "exception": {
  1303. "values": [
  1304. {
  1305. "type": "Error",
  1306. "stacktrace": {
  1307. "frames": [
  1308. {
  1309. "abs_path": "node_bootstrap.js",
  1310. "filename": "node_bootstrap.js",
  1311. "lineno": 1,
  1312. "colno": 38,
  1313. },
  1314. {
  1315. "abs_path": "timers.js",
  1316. "filename": "timers.js",
  1317. "lineno": 1,
  1318. "colno": 39,
  1319. },
  1320. {
  1321. "abs_path": "webpack:///internal",
  1322. "filename": "internal",
  1323. "lineno": 1,
  1324. "colno": 43,
  1325. },
  1326. {
  1327. "abs_path": "webpack:///~/some_dep/file.js",
  1328. "filename": "file.js",
  1329. "lineno": 1,
  1330. "colno": 41,
  1331. },
  1332. {
  1333. "abs_path": "webpack:///./node_modules/file.js",
  1334. "filename": "file.js",
  1335. "lineno": 1,
  1336. "colno": 42,
  1337. },
  1338. {
  1339. "abs_path": "http://example.com/node_app.min.js",
  1340. "filename": "node_app.min.js",
  1341. "lineno": 1,
  1342. "colno": 40,
  1343. },
  1344. ]
  1345. },
  1346. }
  1347. ]
  1348. },
  1349. }
  1350. event = self.post_and_retrieve_event(data)
  1351. exception = event.interfaces["exception"]
  1352. frame_list = exception.values[0].stacktrace.frames
  1353. # This one should not process, so this one should be none.
  1354. assert exception.values[0].raw_stacktrace is None
  1355. # None of the in app should update
  1356. for x in range(6):
  1357. assert not frame_list[x].in_app
  1358. @responses.activate
  1359. def test_html_file_with_query_param_ending_with_js_extension(self):
  1360. responses.add(
  1361. responses.GET,
  1362. "http://example.com/file.html",
  1363. body=(
  1364. "<!doctype html><html><head></head><body><script>/*legit case*/</script></body></html>"
  1365. ),
  1366. )
  1367. data = {
  1368. "timestamp": self.min_ago,
  1369. "message": "hello",
  1370. "platform": "javascript",
  1371. "exception": {
  1372. "values": [
  1373. {
  1374. "type": "Error",
  1375. "stacktrace": {
  1376. "frames": [
  1377. {
  1378. "abs_path": "http://example.com/file.html?sw=iddqd1337.js",
  1379. "filename": "file.html",
  1380. "lineno": 1,
  1381. "colno": 1,
  1382. },
  1383. ]
  1384. },
  1385. }
  1386. ]
  1387. },
  1388. }
  1389. event = self.post_and_retrieve_event(data)
  1390. assert "errors" not in event.data
  1391. def test_expansion_with_debug_id(self):
  1392. project = self.project
  1393. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1394. release.add_project(project)
  1395. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1396. compressed = BytesIO()
  1397. with zipfile.ZipFile(compressed, mode="w") as zip_file:
  1398. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1399. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1400. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1401. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1402. zip_file.writestr(
  1403. "files/_/_/file.wc.sourcemap.js", load_fixture("file.wc.sourcemap.js")
  1404. )
  1405. zip_file.writestr(
  1406. "manifest.json",
  1407. json.dumps(
  1408. {
  1409. "org": self.organization.slug,
  1410. "release": release.version,
  1411. "files": {
  1412. "files/_/_/file.min.js": {
  1413. "url": "~/file.min.js",
  1414. "type": "minified_source",
  1415. "headers": {
  1416. "content-type": "application/json",
  1417. "debug-id": debug_id,
  1418. "sourcemap": "file.sourcemap.js",
  1419. },
  1420. },
  1421. "files/_/_/file1.js": {
  1422. "url": "~/file1.js",
  1423. "type": "source",
  1424. "headers": {
  1425. "content-type": "application/json",
  1426. },
  1427. },
  1428. "files/_/_/file2.js": {
  1429. "url": "~/file2.js",
  1430. "type": "source",
  1431. "headers": {
  1432. "content-type": "application/json",
  1433. },
  1434. },
  1435. "files/_/_/empty.js": {
  1436. "url": "~/empty.js",
  1437. "type": "source",
  1438. "headers": {
  1439. "content-type": "application/json",
  1440. },
  1441. },
  1442. "files/_/_/file.wc.sourcemap.js": {
  1443. "url": "~/file.wc.sourcemap.js",
  1444. "type": "source_map",
  1445. "headers": {
  1446. "content-type": "application/json",
  1447. "debug-id": debug_id,
  1448. },
  1449. },
  1450. },
  1451. }
  1452. ),
  1453. )
  1454. compressed.seek(0)
  1455. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1456. file.putfile(compressed)
  1457. # We want to also store the release files for this bundle, to check if they work together.
  1458. compressed.seek(0)
  1459. file_for_release = File.objects.create(name="bundle.zip", type="release.bundle")
  1460. file_for_release.putfile(compressed)
  1461. update_artifact_index(release, None, file_for_release)
  1462. artifact_bundle = ArtifactBundle.objects.create(
  1463. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1464. )
  1465. DebugIdArtifactBundle.objects.create(
  1466. organization_id=self.organization.id,
  1467. debug_id=debug_id,
  1468. artifact_bundle=artifact_bundle,
  1469. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1470. )
  1471. DebugIdArtifactBundle.objects.create(
  1472. organization_id=self.organization.id,
  1473. debug_id=debug_id,
  1474. artifact_bundle=artifact_bundle,
  1475. source_file_type=SourceFileType.SOURCE_MAP.value,
  1476. )
  1477. data = {
  1478. "timestamp": self.min_ago,
  1479. "message": "hello",
  1480. "platform": "javascript",
  1481. "release": "abc",
  1482. "exception": {
  1483. "values": [
  1484. {
  1485. "type": "Error",
  1486. "stacktrace": {
  1487. "frames": [
  1488. {
  1489. "abs_path": "http://example.com/file.min.js",
  1490. "filename": "file.min.js",
  1491. "lineno": 1,
  1492. "colno": 39,
  1493. },
  1494. {
  1495. "abs_path": "http://example.com/file.min.js",
  1496. "filename": "file.min.js",
  1497. "lineno": 1,
  1498. "colno": 79,
  1499. },
  1500. # We want also to test the source without minification.
  1501. {
  1502. "abs_path": "http://example.com/file1.js",
  1503. "filename": "file1.js",
  1504. "lineno": 3,
  1505. "colno": 12,
  1506. },
  1507. ]
  1508. },
  1509. }
  1510. ]
  1511. },
  1512. "debug_meta": {
  1513. "images": [
  1514. {
  1515. "type": "sourcemap",
  1516. "debug_id": debug_id,
  1517. "code_file": "http://example.com/file.min.js",
  1518. }
  1519. ]
  1520. },
  1521. }
  1522. event = self.post_and_retrieve_event(data)
  1523. assert "errors" not in event.data
  1524. exception = event.interfaces["exception"]
  1525. frame_list = exception.values[0].stacktrace.frames
  1526. frame = frame_list[0]
  1527. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1528. assert frame.context_line == "\treturn a + b; // fôo"
  1529. assert frame.post_context == ["}", ""]
  1530. frame = frame_list[1]
  1531. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1532. assert frame.context_line == "\treturn a * b;"
  1533. assert frame.post_context == [
  1534. "}",
  1535. "function divide(a, b) {",
  1536. '\t"use strict";',
  1537. "\ttry {",
  1538. "\t\treturn multiply(add(a, b), a, b) / c;",
  1539. ]
  1540. frame = frame_list[2]
  1541. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1542. assert frame.context_line == "\treturn a + b; // fôo"
  1543. assert frame.post_context == ["}", ""]
  1544. def test_expansion_with_debug_id_and_sourcemap_without_sources_content(self):
  1545. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1546. compressed = BytesIO()
  1547. with zipfile.ZipFile(compressed, mode="w") as zip_file:
  1548. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1549. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1550. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1551. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1552. zip_file.writestr("files/_/_/file.sourcemap.js", load_fixture("file.sourcemap.js"))
  1553. zip_file.writestr(
  1554. "manifest.json",
  1555. json.dumps(
  1556. {
  1557. "files": {
  1558. "files/_/_/file.min.js": {
  1559. "url": "~/file.min.js",
  1560. "type": "minified_source",
  1561. "headers": {
  1562. "content-type": "application/json",
  1563. "debug-id": debug_id,
  1564. "sourcemap": "file.sourcemap.js",
  1565. },
  1566. },
  1567. "files/_/_/file1.js": {
  1568. "url": "~/file1.js",
  1569. "type": "source",
  1570. "headers": {
  1571. "content-type": "application/json",
  1572. },
  1573. },
  1574. "files/_/_/file2.js": {
  1575. "url": "~/file2.js",
  1576. "type": "source",
  1577. "headers": {
  1578. "content-type": "application/json",
  1579. },
  1580. },
  1581. "files/_/_/empty.js": {
  1582. "url": "~/empty.js",
  1583. "type": "source",
  1584. "headers": {
  1585. "content-type": "application/json",
  1586. },
  1587. },
  1588. "files/_/_/file.sourcemap.js": {
  1589. "url": "~/file.sourcemap.js",
  1590. "type": "source_map",
  1591. "headers": {
  1592. "content-type": "application/json",
  1593. "debug-id": debug_id,
  1594. },
  1595. },
  1596. }
  1597. }
  1598. ),
  1599. )
  1600. compressed.seek(0)
  1601. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1602. file.putfile(compressed)
  1603. artifact_bundle = ArtifactBundle.objects.create(
  1604. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1605. )
  1606. DebugIdArtifactBundle.objects.create(
  1607. organization_id=self.organization.id,
  1608. debug_id=debug_id,
  1609. artifact_bundle=artifact_bundle,
  1610. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1611. )
  1612. DebugIdArtifactBundle.objects.create(
  1613. organization_id=self.organization.id,
  1614. debug_id=debug_id,
  1615. artifact_bundle=artifact_bundle,
  1616. source_file_type=SourceFileType.SOURCE_MAP.value,
  1617. )
  1618. data = {
  1619. "timestamp": self.min_ago,
  1620. "message": "hello",
  1621. "platform": "javascript",
  1622. "release": "abc",
  1623. "exception": {
  1624. "values": [
  1625. {
  1626. "type": "Error",
  1627. "stacktrace": {
  1628. "frames": [
  1629. {
  1630. "abs_path": "http://example.com/file.min.js",
  1631. "filename": "file.min.js",
  1632. "lineno": 1,
  1633. "colno": 39,
  1634. },
  1635. {
  1636. "abs_path": "http://example.com/file.min.js",
  1637. "filename": "file.min.js",
  1638. "lineno": 1,
  1639. "colno": 79,
  1640. },
  1641. ]
  1642. },
  1643. }
  1644. ]
  1645. },
  1646. "debug_meta": {
  1647. "images": [
  1648. {
  1649. "type": "sourcemap",
  1650. "debug_id": debug_id,
  1651. "code_file": "http://example.com/file.min.js",
  1652. }
  1653. ]
  1654. },
  1655. }
  1656. event = self.post_and_retrieve_event(data)
  1657. assert len(event.data["errors"]) == 3
  1658. assert event.data["errors"][0] == {
  1659. "type": "js_missing_sources_content",
  1660. "source": "http://example.com/file.min.js",
  1661. "sourcemap": f"debug-id://{debug_id}/~/file.sourcemap.js",
  1662. }
  1663. def test_expansion_with_debug_id_and_malformed_sourcemap(self):
  1664. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1665. compressed = BytesIO()
  1666. with zipfile.ZipFile(compressed, mode="w") as zip_file:
  1667. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1668. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1669. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1670. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1671. zip_file.writestr(
  1672. "files/_/_/file.malformed.sourcemap.js", load_fixture("file.malformed.sourcemap.js")
  1673. )
  1674. zip_file.writestr(
  1675. "manifest.json",
  1676. json.dumps(
  1677. {
  1678. "files": {
  1679. "files/_/_/file.min.js": {
  1680. "url": "~/file.min.js",
  1681. "type": "minified_source",
  1682. "headers": {
  1683. "content-type": "application/json",
  1684. "debug-id": debug_id,
  1685. "sourcemap": "file.malformed.sourcemap.js",
  1686. },
  1687. },
  1688. "files/_/_/file1.js": {
  1689. "url": "~/file1.js",
  1690. "type": "source",
  1691. "headers": {
  1692. "content-type": "application/json",
  1693. },
  1694. },
  1695. "files/_/_/file2.js": {
  1696. "url": "~/file2.js",
  1697. "type": "source",
  1698. "headers": {
  1699. "content-type": "application/json",
  1700. },
  1701. },
  1702. "files/_/_/empty.js": {
  1703. "url": "~/empty.js",
  1704. "type": "source",
  1705. "headers": {
  1706. "content-type": "application/json",
  1707. },
  1708. },
  1709. "files/_/_/file.malformed.sourcemap.js": {
  1710. "url": "~/file.malformed.sourcemap.js",
  1711. "type": "source_map",
  1712. "headers": {
  1713. "content-type": "application/json",
  1714. "debug-id": debug_id,
  1715. },
  1716. },
  1717. }
  1718. }
  1719. ),
  1720. )
  1721. compressed.seek(0)
  1722. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1723. file.putfile(compressed)
  1724. artifact_bundle = ArtifactBundle.objects.create(
  1725. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1726. )
  1727. DebugIdArtifactBundle.objects.create(
  1728. organization_id=self.organization.id,
  1729. debug_id=debug_id,
  1730. artifact_bundle=artifact_bundle,
  1731. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1732. )
  1733. DebugIdArtifactBundle.objects.create(
  1734. organization_id=self.organization.id,
  1735. debug_id=debug_id,
  1736. artifact_bundle=artifact_bundle,
  1737. source_file_type=SourceFileType.SOURCE_MAP.value,
  1738. )
  1739. data = {
  1740. "timestamp": self.min_ago,
  1741. "message": "hello",
  1742. "platform": "javascript",
  1743. "release": "abc",
  1744. "exception": {
  1745. "values": [
  1746. {
  1747. "type": "Error",
  1748. "stacktrace": {
  1749. "frames": [
  1750. {
  1751. "abs_path": "http://example.com/file.min.js",
  1752. "filename": "file.min.js",
  1753. "lineno": 1,
  1754. "colno": 39,
  1755. },
  1756. {
  1757. "abs_path": "http://example.com/file.min.js",
  1758. "filename": "file.min.js",
  1759. "lineno": 1,
  1760. "colno": 79,
  1761. },
  1762. ]
  1763. },
  1764. }
  1765. ]
  1766. },
  1767. "debug_meta": {
  1768. "images": [
  1769. {
  1770. "type": "sourcemap",
  1771. "debug_id": debug_id,
  1772. "code_file": "http://example.com/file.min.js",
  1773. }
  1774. ]
  1775. },
  1776. }
  1777. event = self.post_and_retrieve_event(data)
  1778. assert len(event.data["errors"]) == 1
  1779. assert event.data["errors"][0] == {
  1780. "type": "js_invalid_source",
  1781. "debug_id": f"debug-id://{debug_id}/~/file.malformed.sourcemap.js",
  1782. }
  1783. def test_expansion_with_debug_id_not_found(self):
  1784. project = self.project
  1785. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1786. release.add_project(project)
  1787. manifest = {
  1788. "org": self.organization.slug,
  1789. "release": release.version,
  1790. "files": {
  1791. "files/_/_/file.min.js": {
  1792. "url": "http://example.com/file.min.js",
  1793. },
  1794. "files/_/_/file1.js": {
  1795. "url": "http://example.com/file1.js",
  1796. },
  1797. "files/_/_/file2.js": {
  1798. "url": "http://example.com/file2.js",
  1799. },
  1800. "files/_/_/file.sourcemap.js": {
  1801. "url": "http://example.com/file.sourcemap.js",
  1802. },
  1803. },
  1804. }
  1805. file_like = BytesIO()
  1806. with zipfile.ZipFile(file_like, "w") as zip:
  1807. for rel_path, entry in manifest["files"].items():
  1808. name = os.path.basename(rel_path)
  1809. content = load_fixture(name)
  1810. if name == "file.min.js":
  1811. # Remove link to source map, add to header instead
  1812. content = content.replace(b"//@ sourceMappingURL=file.sourcemap.js", b"")
  1813. entry["headers"] = {"SourceMap": "/file.sourcemap.js"}
  1814. zip.writestr(rel_path, content)
  1815. zip.writestr("manifest.json", json.dumps(manifest))
  1816. file_like.seek(0)
  1817. file = File.objects.create(name="release_bundle.zip", type="release.bundle")
  1818. file.putfile(file_like)
  1819. update_artifact_index(release, None, file)
  1820. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1821. data = {
  1822. "timestamp": self.min_ago,
  1823. "message": "hello",
  1824. "platform": "javascript",
  1825. "release": "abc",
  1826. "exception": {
  1827. "values": [
  1828. {
  1829. "type": "Error",
  1830. "stacktrace": {
  1831. "frames": [
  1832. {
  1833. "abs_path": "http://example.com/file.min.js",
  1834. "filename": "file.min.js",
  1835. "lineno": 1,
  1836. "colno": 39,
  1837. },
  1838. {
  1839. "abs_path": "http://example.com/file.min.js",
  1840. "filename": "file.min.js",
  1841. "lineno": 1,
  1842. "colno": 79,
  1843. },
  1844. # We want also to test the source without minification.
  1845. {
  1846. "abs_path": "http://example.com/file1.js",
  1847. "filename": "file1.js",
  1848. "lineno": 3,
  1849. "colno": 12,
  1850. },
  1851. ]
  1852. },
  1853. }
  1854. ]
  1855. },
  1856. "debug_meta": {
  1857. "images": [
  1858. {
  1859. "type": "sourcemap",
  1860. "debug_id": debug_id,
  1861. "code_file": "http://example.com/file.min.js",
  1862. }
  1863. ]
  1864. },
  1865. }
  1866. event = self.post_and_retrieve_event(data)
  1867. assert "errors" not in event.data
  1868. exception = event.interfaces["exception"]
  1869. frame_list = exception.values[0].stacktrace.frames
  1870. frame = frame_list[0]
  1871. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1872. assert frame.context_line == "\treturn a + b; // fôo"
  1873. assert frame.post_context == ["}", ""]
  1874. frame = frame_list[1]
  1875. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1876. assert frame.context_line == "\treturn a * b;"
  1877. assert frame.post_context == [
  1878. "}",
  1879. "function divide(a, b) {",
  1880. '\t"use strict";',
  1881. "\ttry {",
  1882. "\t\treturn multiply(add(a, b), a, b) / c;",
  1883. ]
  1884. frame = frame_list[2]
  1885. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1886. assert frame.context_line == "\treturn a + b; // fôo"
  1887. assert frame.post_context == ["}", ""]
  1888. def test_expansion_with_release_dist_pair(self):
  1889. project = self.project
  1890. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1891. release.add_project(project)
  1892. dist = release.add_dist("android")
  1893. # We want to also add debug_id information inside the manifest but not in the stack trace to replicate a
  1894. # real edge case that we can incur in.
  1895. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1896. compressed = BytesIO()
  1897. with zipfile.ZipFile(compressed, mode="w") as zip_file:
  1898. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1899. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1900. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1901. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1902. zip_file.writestr(
  1903. "files/_/_/file.wc.sourcemap.js", load_fixture("file.wc.sourcemap.js")
  1904. )
  1905. zip_file.writestr(
  1906. "manifest.json",
  1907. json.dumps(
  1908. {
  1909. "files": {
  1910. "files/_/_/file.min.js": {
  1911. "url": "~/file.min.js",
  1912. "type": "minified_source",
  1913. "headers": {
  1914. "content-type": "application/json",
  1915. "sourcemap": "file.wc.sourcemap.js",
  1916. "debug-id": debug_id,
  1917. },
  1918. },
  1919. "files/_/_/file1.js": {
  1920. "url": "~/file1.js",
  1921. "type": "source",
  1922. "headers": {
  1923. "content-type": "application/json",
  1924. },
  1925. },
  1926. "files/_/_/file2.js": {
  1927. "url": "~/file2.js",
  1928. "type": "source",
  1929. "headers": {
  1930. "content-type": "application/json",
  1931. },
  1932. },
  1933. "files/_/_/empty.js": {
  1934. "url": "~/empty.js",
  1935. "type": "source",
  1936. "headers": {
  1937. "content-type": "application/json",
  1938. },
  1939. },
  1940. "files/_/_/file.wc.sourcemap.js": {
  1941. "url": "~/file.wc.sourcemap.js",
  1942. "type": "source_map",
  1943. "headers": {
  1944. "content-type": "application/json",
  1945. "debug-id": debug_id,
  1946. },
  1947. },
  1948. },
  1949. "debug_meta": {
  1950. "images": [
  1951. {
  1952. "type": "sourcemap",
  1953. "debug_id": debug_id,
  1954. "code_file": "http://example.com/file.min.js",
  1955. }
  1956. ]
  1957. },
  1958. }
  1959. ),
  1960. )
  1961. compressed.seek(0)
  1962. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1963. file.putfile(compressed)
  1964. artifact_bundle = ArtifactBundle.objects.create(
  1965. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1966. )
  1967. ReleaseArtifactBundle.objects.create(
  1968. organization_id=self.organization.id,
  1969. release_name=release.version,
  1970. dist_name=dist.name,
  1971. artifact_bundle=artifact_bundle,
  1972. )
  1973. data = {
  1974. "timestamp": self.min_ago,
  1975. "message": "hello",
  1976. "platform": "javascript",
  1977. "release": release.version,
  1978. "dist": dist.name,
  1979. "exception": {
  1980. "values": [
  1981. {
  1982. "type": "Error",
  1983. "stacktrace": {
  1984. "frames": [
  1985. {
  1986. "abs_path": "http://example.com/file.min.js",
  1987. "filename": "file.min.js",
  1988. "lineno": 1,
  1989. "colno": 39,
  1990. },
  1991. {
  1992. "abs_path": "http://example.com/file.min.js",
  1993. "filename": "file.min.js",
  1994. "lineno": 1,
  1995. "colno": 79,
  1996. },
  1997. # We want also to test the source without minification.
  1998. {
  1999. "abs_path": "http://example.com/file1.js",
  2000. "filename": "file1.js",
  2001. "lineno": 3,
  2002. "colno": 12,
  2003. },
  2004. ]
  2005. },
  2006. }
  2007. ]
  2008. },
  2009. }
  2010. event = self.post_and_retrieve_event(data)
  2011. assert "errors" not in event.data
  2012. exception = event.interfaces["exception"]
  2013. frame_list = exception.values[0].stacktrace.frames
  2014. frame = frame_list[0]
  2015. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2016. assert frame.context_line == "\treturn a + b; // fôo"
  2017. assert frame.post_context == ["}", ""]
  2018. frame = frame_list[1]
  2019. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2020. assert frame.context_line == "\treturn a * b;"
  2021. assert frame.post_context == [
  2022. "}",
  2023. "function divide(a, b) {",
  2024. '\t"use strict";',
  2025. "\ttry {",
  2026. "\t\treturn multiply(add(a, b), a, b) / c;",
  2027. ]
  2028. frame = frame_list[2]
  2029. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2030. assert frame.context_line == "\treturn a + b; // fôo"
  2031. assert frame.post_context == ["}", ""]
  2032. def test_expansion_with_release_dist_pair_and_sourcemap_without_sources_content(self):
  2033. project = self.project
  2034. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2035. release.add_project(project)
  2036. dist = release.add_dist("android")
  2037. compressed = BytesIO()
  2038. with zipfile.ZipFile(compressed, mode="w") as zip_file:
  2039. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2040. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2041. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2042. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2043. zip_file.writestr("files/_/_/file.sourcemap.js", load_fixture("file.sourcemap.js"))
  2044. zip_file.writestr(
  2045. "manifest.json",
  2046. json.dumps(
  2047. {
  2048. "files": {
  2049. "files/_/_/file.min.js": {
  2050. "url": "~/file.min.js",
  2051. "type": "minified_source",
  2052. "headers": {
  2053. "content-type": "application/json",
  2054. "sourcemap": "file.sourcemap.js",
  2055. },
  2056. },
  2057. "files/_/_/file1.js": {
  2058. "url": "~/file1.js",
  2059. "type": "source",
  2060. "headers": {
  2061. "content-type": "application/json",
  2062. },
  2063. },
  2064. "files/_/_/file2.js": {
  2065. "url": "~/file2.js",
  2066. "type": "source",
  2067. "headers": {
  2068. "content-type": "application/json",
  2069. },
  2070. },
  2071. "files/_/_/empty.js": {
  2072. "url": "~/empty.js",
  2073. "type": "source",
  2074. "headers": {
  2075. "content-type": "application/json",
  2076. },
  2077. },
  2078. "files/_/_/file.sourcemap.js": {
  2079. "url": "~/file.sourcemap.js",
  2080. "type": "source_map",
  2081. "headers": {
  2082. "content-type": "application/json",
  2083. },
  2084. },
  2085. }
  2086. }
  2087. ),
  2088. )
  2089. compressed.seek(0)
  2090. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2091. file.putfile(compressed)
  2092. artifact_bundle = ArtifactBundle.objects.create(
  2093. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2094. )
  2095. ReleaseArtifactBundle.objects.create(
  2096. organization_id=self.organization.id,
  2097. release_name=release.version,
  2098. dist_name=dist.name,
  2099. artifact_bundle=artifact_bundle,
  2100. )
  2101. data = {
  2102. "timestamp": self.min_ago,
  2103. "message": "hello",
  2104. "platform": "javascript",
  2105. "release": release.version,
  2106. "dist": dist.name,
  2107. "exception": {
  2108. "values": [
  2109. {
  2110. "type": "Error",
  2111. "stacktrace": {
  2112. "frames": [
  2113. {
  2114. "abs_path": "http://example.com/file.min.js",
  2115. "filename": "file.min.js",
  2116. "lineno": 1,
  2117. "colno": 39,
  2118. },
  2119. {
  2120. "abs_path": "http://example.com/file.min.js",
  2121. "filename": "file.min.js",
  2122. "lineno": 1,
  2123. "colno": 79,
  2124. },
  2125. ]
  2126. },
  2127. }
  2128. ]
  2129. },
  2130. }
  2131. event = self.post_and_retrieve_event(data)
  2132. assert len(event.data["errors"]) == 3
  2133. assert event.data["errors"][0] == {
  2134. "type": "js_missing_sources_content",
  2135. "source": "http://example.com/file.min.js",
  2136. "sourcemap": "http://example.com/file.sourcemap.js",
  2137. }
  2138. def test_expansion_with_release_and_malformed_sourcemap(self):
  2139. project = self.project
  2140. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2141. release.add_project(project)
  2142. compressed = BytesIO()
  2143. with zipfile.ZipFile(compressed, mode="w") as zip_file:
  2144. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2145. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2146. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2147. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2148. zip_file.writestr(
  2149. "files/_/_/file.malformed.sourcemap.js", load_fixture("file.malformed.sourcemap.js")
  2150. )
  2151. zip_file.writestr(
  2152. "manifest.json",
  2153. json.dumps(
  2154. {
  2155. "files": {
  2156. "files/_/_/file.min.js": {
  2157. "url": "~/file.min.js",
  2158. "type": "minified_source",
  2159. "headers": {
  2160. "content-type": "application/json",
  2161. "sourcemap": "file.malformed.sourcemap.js",
  2162. },
  2163. },
  2164. "files/_/_/file1.js": {
  2165. "url": "~/file1.js",
  2166. "type": "source",
  2167. "headers": {
  2168. "content-type": "application/json",
  2169. },
  2170. },
  2171. "files/_/_/file2.js": {
  2172. "url": "~/file2.js",
  2173. "type": "source",
  2174. "headers": {
  2175. "content-type": "application/json",
  2176. },
  2177. },
  2178. "files/_/_/empty.js": {
  2179. "url": "~/empty.js",
  2180. "type": "source",
  2181. "headers": {
  2182. "content-type": "application/json",
  2183. },
  2184. },
  2185. "files/_/_/file.malformed.sourcemap.js": {
  2186. "url": "~/file.malformed.sourcemap.js",
  2187. "type": "source_map",
  2188. "headers": {
  2189. "content-type": "application/json",
  2190. },
  2191. },
  2192. }
  2193. }
  2194. ),
  2195. )
  2196. compressed.seek(0)
  2197. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2198. file.putfile(compressed)
  2199. artifact_bundle = ArtifactBundle.objects.create(
  2200. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2201. )
  2202. ReleaseArtifactBundle.objects.create(
  2203. organization_id=self.organization.id,
  2204. release_name=release.version,
  2205. artifact_bundle=artifact_bundle,
  2206. )
  2207. data = {
  2208. "timestamp": self.min_ago,
  2209. "message": "hello",
  2210. "platform": "javascript",
  2211. "release": release.version,
  2212. "exception": {
  2213. "values": [
  2214. {
  2215. "type": "Error",
  2216. "stacktrace": {
  2217. "frames": [
  2218. {
  2219. "abs_path": "http://example.com/file.min.js",
  2220. "filename": "file.min.js",
  2221. "lineno": 1,
  2222. "colno": 39,
  2223. },
  2224. {
  2225. "abs_path": "http://example.com/file.min.js",
  2226. "filename": "file.min.js",
  2227. "lineno": 1,
  2228. "colno": 79,
  2229. },
  2230. ]
  2231. },
  2232. }
  2233. ]
  2234. },
  2235. }
  2236. event = self.post_and_retrieve_event(data)
  2237. assert len(event.data["errors"]) == 1
  2238. assert event.data["errors"][0] == {
  2239. "type": "js_invalid_source",
  2240. "url": "http://example.com/file.malformed.sourcemap.js",
  2241. }