test_plugin.py 97 KB

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