test_plugin.py 94 KB

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