-
Notifications
You must be signed in to change notification settings - Fork 684
Initial stab at a security tab showing advisories #12311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import Route from '@ember/routing/route'; | ||
| import { service } from '@ember/service'; | ||
|
|
||
| async function fetchAdvisories(crateId) { | ||
| let url = `https://rustsec.org/packages/${crateId}.json`; | ||
| let response = await fetch(url); | ||
| if (response.status === 404) { | ||
| return []; | ||
| } else if (response.ok) { | ||
| return await response.json(); | ||
| } else { | ||
| throw new Error(`HTTP error! status: ${response}`); | ||
| } | ||
| } | ||
|
|
||
| export default class SecurityRoute extends Route { | ||
| @service sentry; | ||
|
|
||
| async model() { | ||
| let crate = this.modelFor('crate'); | ||
| try { | ||
| let [advisories, micromarkModule, gfmModule] = await Promise.all([ | ||
| fetchAdvisories(crate.id), | ||
| import('micromark'), | ||
| import('micromark-extension-gfm'), | ||
| ]); | ||
|
|
||
| const convertMarkdown = markdown => { | ||
| return micromarkModule.micromark(markdown, { | ||
| extensions: [gfmModule.gfm()], | ||
| htmlExtensions: [gfmModule.gfmHtml()], | ||
| }); | ||
| }; | ||
|
|
||
| return { crate, advisories, convertMarkdown, error: false }; | ||
| } catch (error) { | ||
| this.sentry.captureException(error); | ||
| return { crate, advisories: [], convertMarkdown: null, error: true }; | ||
| } | ||
| } | ||
|
|
||
| setupController(controller, { crate, advisories, convertMarkdown, error }) { | ||
| super.setupController(...arguments); | ||
| controller.crate = crate; | ||
| controller.advisories = advisories; | ||
| controller.convertMarkdown = convertMarkdown; | ||
| controller.error = error; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| .heading { | ||
| font-size: 1.17em; | ||
| margin-block-start: 1em; | ||
| margin-block-end: 1em; | ||
| } | ||
|
|
||
| .advisories { | ||
| list-style: none; | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
|
|
||
| .row { | ||
| margin-top: var(--space-2xs); | ||
| background-color: light-dark(white, #141413); | ||
| border-radius: var(--space-3xs); | ||
| padding: var(--space-m) var(--space-l); | ||
| list-style: none; | ||
| } | ||
|
|
||
| .no-results { | ||
| padding: var(--space-l) var(--space-s); | ||
| background-color: light-dark(white, #141413); | ||
| text-align: center; | ||
| font-size: 20px; | ||
| font-weight: 300; | ||
| overflow-wrap: break-word; | ||
| line-height: 1.5; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { htmlSafe } from '@ember/template'; | ||
|
|
||
| import CrateHeader from 'crates-io/components/crate-header'; | ||
|
|
||
| <template> | ||
| <CrateHeader @crate={{@controller.crate}} /> | ||
| {{#if @controller.advisories.length}} | ||
| <h2 class='heading'>Advisories</h2> | ||
| <ul class='advisories' data-test-list> | ||
| {{#each @controller.advisories as |advisory|}} | ||
| <li class='row'> | ||
| <h3> | ||
| <a href='https://rustsec.org/advisories/{{advisory.id}}.html'>{{advisory.id}}</a>: | ||
| {{advisory.summary}} | ||
| </h3> | ||
| {{htmlSafe (@controller.convertMarkdown advisory.details)}} | ||
| </li> | ||
| {{/each}} | ||
| </ul> | ||
| {{else if @controller.error}} | ||
| <div class='no-results' data-error> | ||
| An error occurred while fetching advisories. | ||
| </div> | ||
|
Comment on lines
+20
to
+23
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a reason for not using the |
||
| {{else}} | ||
| <div class='no-results' data-no-advisories> | ||
| No advisories found for this crate. | ||
| </div> | ||
| {{/if}} | ||
| </template> | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { expect, test } from '@/e2e/helper'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { http, HttpResponse } from 'msw'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test.describe('Acceptance | crate security page', { tag: '@acceptance' }, () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('show some advisories', async ({ page, msw, percy }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let crate = await msw.db.crate.create({ name: 'foo' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await msw.db.version.create({ crate, num: '1.0.0' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let advisories = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: 'TEST-001', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| summary: 'First test advisory', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| details: 'This is the first test advisory with **markdown** support.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: 'TEST-002', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| summary: 'Second test advisory', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| details: 'This is the second test advisory with more details.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| msw.worker.use(http.get('https://rustsec.org/packages/:crateId.json', () => HttpResponse.json(advisories))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await page.goto('/crates/foo/security'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-test-list] li')).toHaveCount(2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check first advisory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-test-list] li').nth(0).locator('h3 a')).toHaveAttribute( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'href', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'https://rustsec.org/advisories/TEST-001.html', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-test-list] li').nth(0).locator('h3 a')).toContainText('TEST-001'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-test-list] li').nth(0).locator('h3')).toContainText('First test advisory'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-test-list] li').nth(0).locator('p')).toContainText('markdown'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check second advisory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-test-list] li').nth(1).locator('h3 a')).toHaveAttribute( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'href', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'https://rustsec.org/advisories/TEST-002.html', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-test-list] li').nth(1).locator('h3 a')).toContainText('TEST-002'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-test-list] li').nth(1).locator('h3')).toContainText('Second test advisory'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+28
to
+43
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await percy.snapshot(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('show no advisory data when none exist', async ({ page, msw }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let crate = await msw.db.crate.create({ name: 'safe-crate' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await msw.db.version.create({ crate, num: '1.0.0' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| msw.worker.use( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.get('https://rustsec.org/packages/:crateId.json', () => HttpResponse.text('not found', { status: 404 })), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await page.goto('/crates/safe-crate/security'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-no-advisories]')).toBeVisible(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-no-advisories]')).toHaveText('No advisories found for this crate.'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('handles errors gracefully', async ({ page, msw }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let crate = await msw.db.crate.create({ name: 'error-crate' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await msw.db.version.create({ crate, num: '1.0.0' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| msw.worker.use( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.get('https://rustsec.org/packages/:crateId.json', () => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HttpResponse.text('Internal Server Error', { status: 500 }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await page.goto('/crates/error-crate/security'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // When there's an error, the route catches it and returns empty advisories | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-error]')).toBeVisible(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(page.locator('[data-error]')).toHaveText('An error occurred while fetching advisories.'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should add a couple of smoke tests for the
micromarklibrary, to ensure that is sanitizes correctly. if the library changes their defaults we should probably have some kind of notification mechanism to let us know about it (e.g. a failing test case).