test_plugin.py 97 KB

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