test_plugin.py 95 KB

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