Skip to content
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

682 dataset task list fetch data from all active resources #717

Conversation

GeorgeGoodall-GovUk
Copy link
Contributor

@GeorgeGoodall-GovUk GeorgeGoodall-GovUk commented Dec 4, 2024

What type of PR is this? (check all applicable)

  • Refactor
  • Bug Fix

Description

Updates the dataset task list to look at all active resources when looking at entity issues and only the latest resource when looking at entry issues

Related Tickets & Documents

QA Instructions, Screenshots, Recordings

no styling or changes should be present. however the data we are fetching should be different.

Before:
image

After:
image

Added/updated tests?

We encourage you to keep the code coverage percentage at 80% and above.

  • Yes
  • No, and this is why: Please replace this line with details on why tests have not been included
  • I need help with writing tests

QA sign off

  • Code has been checked and approved
  • Design has been checked and approved
  • Product and business logic has been checked and proved

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced issue tracking and reporting capabilities.
    • Improved data entry issue management with new functions for fetching entry issues and counts.
    • Updated task list preparation mechanism to streamline dataset tasks.
  • Bug Fixes

    • Refined issue filtering and categorisation.
    • Improved error handling for dataset tasks, including better validation and logging.
    • Updated parameter handling in various middleware functions.
  • Refactoring

    • Streamlined middleware functions to focus on entry issues and task preparation.
    • Consolidated issue tracking logic across multiple components.
    • Updated database query structures for improved efficiency.
  • Documentation

    • Updated JSDoc comments for modified functions.
    • Improved code clarity and consistency.

These changes enhance the application's ability to track, manage, and report issues more effectively, with a focus on improving data entry and task management processes.

@GeorgeGoodall-GovUk GeorgeGoodall-GovUk linked an issue Dec 4, 2024 that may be closed by this pull request
3 tasks
Copy link
Contributor

coderabbitai bot commented Dec 4, 2024

Warning

Rate limit exceeded

@GeorgeGoodall-GovUk has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 25 minutes and 6 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 5a65eb8 and edb4eea.

📒 Files selected for processing (1)
  • test/unit/middleware/entryIssueDetails.middleware.test.js (3 hunks)

Walkthrough

This pull request introduces significant modifications to middleware and utility files, focusing on enhancing issue tracking and resource management. The changes primarily involve restructuring functions in common.middleware.js, datasetTaskList.middleware.js, and entryIssueDetails.middleware.js. New functions for fetching and processing entry issues have been added, and existing functions have been updated to improve issue filtering, counting, and reporting. The modifications aim to provide more precise and comprehensive handling of data entry issues across different components.

Changes

File Change Summary
src/middleware/common.middleware.js Added fetchEntryIssues, fetchEntryIssueCounts, addEntityCountsToResources; Updated fetchEntityIssuesForFieldAndType, getErrorSummaryItems
src/middleware/datasetTaskList.middleware.js Added prepareTasks, fetchEntityIssueCounts, fetchEntryIssueCounts; Removed several methods like fetchOrgInfoWithStatGeo
src/middleware/entryIssueDetails.middleware.js Updated prepareEntry to use entryIssues instead of issues
src/middleware/issueTable.middleware.js Imported entryIssueGroups; Removed redirectIssueGroups
src/services/performanceDbApi.js Renamed entityCount parameter to rowCount in getTaskMessage
src/utils/utils.js Added entryIssueGroups constant
src/services/datasette.js Added default database parameter to runQuery method

Sequence Diagram

sequenceDiagram
    participant MW as Middleware
    participant DB as Database
    participant Service as Performance DB API
    participant Utils as Utils

    MW->>Utils: Import entryIssueGroups
    MW->>DB: Fetch Entry Issues
    DB-->>MW: Return Entry Issues
    MW->>Service: Get Task Message
    Service-->>MW: Generate Task List
    MW->>MW: Prepare Template Params
Loading

Possibly related PRs

Suggested labels

bug, enhancement, tech debt

Suggested reviewers

  • DilwoarH

Poem

🐰 In middleware's grand design,
Issues dance, their stories align
Entry counts and tasks take flight
Code refined with rabbit's might!
Middleware magic, clean and bright 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@GeorgeGoodall-GovUk GeorgeGoodall-GovUk changed the base branch from main to 684-issue-details-fetch-data-from-all-active-resources December 4, 2024 13:44
Copy link

github-actions bot commented Dec 4, 2024

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 70.32% 5432 / 7724
🔵 Statements 70.32% 5432 / 7724
🔵 Functions 68.13% 216 / 317
🔵 Branches 82.15% 700 / 852
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/middleware/common.middleware.js 73.81% 90.09% 41.3% 73.81% 22-31, 35, 53-54, 56-57, 69-71, 82, 98-105, 186-187, 199-205, 211-233, 331, 340-342, 372-374, 412-426, 466-470, 475-478, 508-522, 529-540, 546-554, 558-574, 635-636, 638-639, 641-642, 706-761
src/middleware/datasetTaskList.middleware.js 92.35% 76.92% 60% 92.35% 82-83, 110-119
src/middleware/entryIssueDetails.middleware.js 90.27% 79.16% 66.66% 90.27% 22-24, 46-52, 78-81
src/middleware/issueTable.middleware.js 98.44% 95.83% 85.71% 98.44% 44-45
src/services/datasette.js 100% 100% 100% 100%
src/services/performanceDbApi.js 63.5% 92.59% 33.33% 63.5% 49-77, 114-116, 179-183, 234-240, 251-265, 275-284, 293-313, 317-325, 334-346, 357-384, 394-399
src/utils/utils.js 100% 89.47% 100% 100%
Generated in workflow #604 for commit edb4eea by the Vitest Coverage Report Action

…set-task-list-fetch-data-from-all-active-resources
…set-task-list-fetch-data-from-all-active-resources
got the tasklist page working with all active endpoints
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/middleware/datasetTaskList.middleware.js (1)

67-72: Consider using type-safe null checks.

The current filtering logic could be more robust by using type-safe null checks.

-    issue => issue.issue_type !== '' &&
-    issue.issue_type !== undefined &&
-    issue.field !== '' &&
-    issue.field !== undefined
+    issue => Boolean(issue.issue_type?.trim()) && Boolean(issue.field?.trim())
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6737606 and 7d1cf92.

📒 Files selected for processing (2)
  • src/middleware/datasetTaskList.middleware.js (4 hunks)
  • src/middleware/entryIssueDetails.middleware.js (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: run-tests / test
🔇 Additional comments (7)
src/middleware/entryIssueDetails.middleware.js (3)

2-2: LGTM! Import changes align with PR objectives.

The addition of fetchEntryIssues to the imports is consistent with the refactoring goals to update how entry issues are fetched.


64-76: LGTM! Robust error handling improvements.

The changes enhance error handling in two ways:

  1. Comprehensive checks for missing values with appropriate 404 status
  2. Validation of entry issue structure with appropriate 500 status

This makes the middleware more resilient and helps with debugging.


131-131: LGTM! Middleware sequence is correct.

The addition of fetchEntryIssues in the middleware pipeline is appropriately placed before pagination checks.

src/middleware/datasetTaskList.middleware.js (4)

2-8: LGTM! Well-structured imports and constant definition.

The updated imports and the extraction of SPECIAL_ISSUE_TYPES into a constant improve code maintainability and readability.

Also applies to: 41-41


44-57: LGTM! Excellent documentation.

The JSDoc documentation is thorough and clearly describes the function's purpose, parameters, and requirements.


103-103: LGTM! Secure URL parameter handling.

Proper use of encodeURIComponent for all URL parameters prevents potential injection vulnerabilities.

Also applies to: 115-115


153-161: LGTM! Well-structured middleware pipeline.

The middleware sequence is logically ordered, with entity and entry issue fetching appropriately placed after resource processing.

src/middleware/common.middleware.js Outdated Show resolved Hide resolved
src/middleware/middleware.builders.js Outdated Show resolved Hide resolved
src/middleware/middleware.builders.js Outdated Show resolved Hide resolved
src/middleware/middleware.builders.js Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

♻️ Duplicate comments (4)
src/middleware/common.middleware.js (4)

412-425: ⚠️ Potential issue

Fix multiple SQL injection vulnerabilities.

The query has multiple SQL injection vulnerabilities due to string interpolation of user inputs.

Apply this diff to use parameterised queries:

  query: ({ req, params }) => {
-    const issueTypeClause = params.issue_type ? `AND i.issue_type = '${params.issue_type}'` : ''
-    const issueFieldClause = params.issue_field ? `AND field = '${params.issue_field}'` : ''
+    const values = [req.resources[0].resource]
+    let paramCount = 2
+    const clauses = []
+    
+    if (params.issue_type) {
+      clauses.push(`AND i.issue_type = $${paramCount++}`)
+      values.push(params.issue_type)
+    }
+    if (params.issue_field) {
+      clauses.push(`AND field = $${paramCount++}`)
+      values.push(params.issue_field)
+    }
+    
+    values.push(req.params.dataset)
+    
+    return {
+      text: `
         select i.issue_type, field, entity, message, severity, value
         from issue i
         LEFT JOIN issue_type it ON i.issue_type = it.issue_type
-        WHERE resource = '${req.resources[0].resource}'
+        WHERE resource = $1
+        ${clauses.join(' ')}
         AND it.responsibility = 'external'
         AND it.severity = 'error'
-        AND i.dataset = '${req.params.dataset}'
+        AND i.dataset = $${paramCount}
         AND entity != ''
-        `
+        `,
+      values
+    }

505-525: ⚠️ Potential issue

Fix multiple SQL injection vulnerabilities.

The query has multiple SQL injection vulnerabilities due to string interpolation of user inputs.

Apply this diff to use parameterised queries:

  query: ({ req, params }) => {
-    const issueTypeClause = params.issue_type ? `AND i.issue_type = '${params.issue_type}'` : ''
-    const issueFieldClause = params.issue_field ? `AND field = '${params.issue_field}'` : ''
+    const values = [req.resources[0].resource, req.params.dataset]
+    let paramCount = 3
+    const clauses = []
+    
+    if (params.issue_type) {
+      clauses.push(`AND i.issue_type = $${paramCount++}`)
+      values.push(params.issue_type)
+    }
+    if (params.issue_field) {
+      clauses.push(`AND field = $${paramCount++}`)
+      values.push(params.issue_field)
+    }
+    
+    values.push(entryIssueGroups.map(issue => issue.type))
+    values.push(req.dataRange.pageLength)
+    values.push(req.dataRange.offset)
+    
+    return {
+      text: `
       select i.issue_type, field, entity, message, severity, value, line_number
       from issue i
       LEFT JOIN issue_type it ON i.issue_type = it.issue_type
-      WHERE resource = '${req.resources[0].resource}'
+      WHERE resource = $1
       ${issueTypeClause}
       AND it.responsibility = 'external'
       AND it.severity = 'error'
-      AND i.dataset = '${req.params.dataset}'
+      AND i.dataset = $2
       ${issueFieldClause}
-      AND (entity = '' OR entity is NULL OR i.issue_type in ('${entryIssueGroups.map(issue => issue.type).join("', '")}'))
-      LIMIT ${req.dataRange.pageLength} OFFSET ${req.dataRange.offset}
-    `
+      AND (entity = '' OR entity is NULL OR i.issue_type = ANY($${paramCount++}::text[]))
+      LIMIT $${paramCount++} OFFSET $${paramCount}
+      `,
+      values
+    }

527-543: ⚠️ Potential issue

Fix SQL injection vulnerabilities and string concatenation.

The query has multiple issues:

  1. SQL injection vulnerabilities due to string interpolation
  2. Incorrect string concatenation in COUNT

Apply this diff to fix both issues:

  query: ({ req }) => {
-    const datasetClause = req.params.dataset ? `AND i.dataset = '${req.params.dataset}'` : ''
+    const values = [req.resources.map(resource => resource.resource)]
+    let paramCount = 2
+    const clauses = []
+    
+    if (req.params.dataset) {
+      clauses.push(`AND i.dataset = $${paramCount++}`)
+      values.push(req.params.dataset)
+    }
+    
+    return {
+      text: `
-      select dataset, field, i.issue_type, COUNT(resource+line_number) as count
+      select dataset, field, i.issue_type, COUNT(resource || line_number) as count
       from issue i
       LEFT JOIN issue_type it ON i.issue_type = it.issue_type
-      WHERE resource in ('${req.resources.map(resource => resource.resource).join("', '")}')
+      WHERE resource = ANY($1::text[])
       AND (entity != '' AND entity IS NOT NULL)
       AND it.responsibility = 'external'
       AND it.severity = 'error'
-      ${datasetClause}
+      ${clauses.join(' ')}
       GROUP BY field, i.issue_type, dataset
-    `
+      `,
+      values
+    }

556-577: ⚠️ Potential issue

Fix SQL injection vulnerabilities and string concatenation.

The query has multiple issues:

  1. SQL injection vulnerabilities due to string interpolation
  2. Incorrect string concatenation in COUNT

Apply this diff to fix both issues:

  query: ({ req }) => {
-    const datasetClause = req.params.dataset ? `AND i.dataset = '${req.params.dataset}'` : ''
+    const values = []
+    let paramCount = 1
+    const clauses = []
+    
+    if (req.params.dataset) {
+      clauses.push(`AND i.dataset = $${paramCount++}`)
+      values.push(req.params.dataset)
+    }

    const mostRecentResources = getMostRecentResources(req.resources)
    const resourceIds = Object.values(mostRecentResources).map(resource => resource.resource)
+    values.push(resourceIds)

-    return `
+    return {
+      text: `
-      select dataset, field, i.issue_type, COUNT(resource + line_number) as count
+      select dataset, field, i.issue_type, COUNT(resource || line_number) as count
       from issue i
       LEFT JOIN issue_type it ON i.issue_type = it.issue_type
-      WHERE resource in ('${resourceIds.join("', '")}')
+      WHERE resource = ANY($${paramCount}::text[])
       AND (entity = '' OR entity is NULL)
       AND it.responsibility = 'external'
       AND it.severity = 'error'
-      ${datasetClause}
+      ${clauses.join(' ')}
       GROUP BY field, i.issue_type, dataset
-    `
+      `,
+      values
+    }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f0b7c7a and 309f105.

📒 Files selected for processing (1)
  • src/middleware/common.middleware.js (7 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: run-tests / test
🔇 Additional comments (3)
src/middleware/common.middleware.js (3)

4-4: LGTM!

The import statement is correctly placed and the imported utility is used appropriately in the code.


219-222: LGTM! Error handling follows best practices.

The error handling is well-implemented with:

  • Proper error logging with type and stack trace
  • Error propagation to the next middleware
  • Correct error object destructuring for logging

Also applies to: 230-231


545-553: LGTM! Correct implementation of date comparison.

The function correctly:

  • Uses Date.getTime() for comparing dates
  • Efficiently maps resources to their datasets
  • Returns the most recent resources

src/middleware/common.middleware.js Show resolved Hide resolved
src/middleware/common.middleware.js Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/middleware/entryIssueDetails.middleware.js (1)

72-76: Consider enhancing error messages and validation.

The error handling could be improved in two ways:

  1. Provide more specific error messages indicating which validation failed
  2. Move the validation to a valibot schema for consistency with IssueDetailsQueryParams

Consider this approach:

+ const EntryIssueSchema = v.object({
+   entry_number: v.optional(v.string()),
+   entity: v.optional(v.string()),
+   issue_type: v.string(),
+   line_number: v.number(),
+ })

  const issue = entryIssues[0]
- if (!(issue.entry_number || issue.entity) || !issue.issue_type || typeof issue.line_number !== 'number') {
-   const error = new Error('Invalid entry issue structure')
-   error.status = 500
-   return next(error)
- }
+ try {
+   v.parse(EntryIssueSchema, issue)
+ } catch (validationError) {
+   const error = new Error(`Invalid entry issue structure: ${validationError.message}`)
+   error.status = 500
+   return next(error)
+ }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 309f105 and 17256ce.

📒 Files selected for processing (1)
  • src/middleware/entryIssueDetails.middleware.js (3 hunks)
🔇 Additional comments (3)
src/middleware/entryIssueDetails.middleware.js (3)

2-2: LGTM! Import changes align with PR objectives.

The addition of fetchEntryIssues to the imports is consistent with the PR's goal to update dataset task list functionality.


64-68: LGTM! Robust input validation.

The validation properly checks for both empty arrays and undefined values, providing better error handling than the previous implementation.


131-131: LGTM! Correct placement in middleware chain.

The fetchEntryIssues middleware is properly positioned after data range setup and before pagination validation.

rosado
rosado previously approved these changes Jan 15, 2025
Copy link
Collaborator

@rosado rosado left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

…set-task-list-fetch-data-from-all-active-resources
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/middleware/entryIssueDetails.middleware.js (1)

75-81: Enhance error message with validation details.

While the validation is good, the error message could be more specific about which properties failed validation.

-    const error = new Error('Invalid entry issue structure')
+    const error = new Error(`Invalid entry issue structure: missing required properties. Got: ${JSON.stringify({
+      has_entry: Boolean(issue.entry_number || issue.entity),
+      has_issue_type: Boolean(issue.issue_type),
+      has_valid_line: typeof issue.line_number === 'number'
+    })}`)
src/middleware/common.middleware.js (1)

593-595: Track technical debt: Optimise removeIssuesThatHaveBeenFixed.

The comment indicates a performance issue that needs to be addressed in the future.

Would you like me to create a GitHub issue to track the optimisation of removeIssuesThatHaveBeenFixed?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 17256ce and 5a65eb8.

📒 Files selected for processing (5)
  • src/middleware/common.middleware.js (7 hunks)
  • src/middleware/entryIssueDetails.middleware.js (3 hunks)
  • src/services/datasette.js (1 hunks)
  • test/unit/middleware/common.middleware.test.js (4 hunks)
  • test/unit/middleware/entryIssueDetails.middleware.test.js (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/services/datasette.js
  • test/unit/middleware/entryIssueDetails.middleware.test.js
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: run-tests / test
🔇 Additional comments (12)
src/middleware/entryIssueDetails.middleware.js (2)

63-70: Well-structured error handling with detailed debugging information!

The error handling provides clear context about which checks failed, making it easier to diagnose issues in production.


136-136: Good placement of fetchEntryIssues in the middleware chain!

The middleware is correctly positioned after pagination setup and before 404 checks.

src/middleware/common.middleware.js (9)

218-232: Well-implemented error handling with proper logging!

The error handling includes:

  • Proper error propagation
  • Detailed error logging with type and stack traces
  • Null coalescing for entry_count

505-506: Clear documentation of business logic!

The comment clearly explains why we only fetch issues from the latest resource.


545-553: Well-implemented date comparison logic!

The function:

  • Safely compares dates using getTime()
  • Maintains a clean, functional approach
  • Properly handles resource grouping by dataset

560-561: Good use of getMostRecentResources function!

The function correctly utilises getMostRecentResources to filter resources.


214-215: ⚠️ Potential issue

Fix SQL injection vulnerability in addEntityCountsToResources.

The query is vulnerable to SQL injection as it uses string interpolation for resource.resource.

-    const query = `SELECT entry_count FROM dataset_resource WHERE resource = "${resource.resource}"`
+    const query = {
+      text: 'SELECT entry_count FROM dataset_resource WHERE resource = $1',
+      values: [resource.resource]
+    }

Likely invalid or redundant comment.


199-201: ⚠️ Potential issue

Fix SQL injection vulnerability in fetchResources.

The query is vulnerable to SQL injection as it uses string interpolation for req.params.lpa and req.params.dataset.

-    WHERE ro.organisation = '${req.params.lpa}'
-    AND rd.dataset = '${req.params.dataset}'
+    WHERE ro.organisation = $1
+    AND rd.dataset = $2`,
+    values: [req.params.lpa, req.params.dataset]

Likely invalid or redundant comment.


412-425: ⚠️ Potential issue

Fix multiple SQL injection vulnerabilities in fetchEntityIssuesForFieldAndType.

The query is vulnerable to SQL injection in multiple places.

-    const issueTypeClause = params.issue_type ? `AND i.issue_type = '${params.issue_type}'` : ''
-    const issueFieldClause = params.issue_field ? `AND field = '${params.issue_field}'` : ''
+    const values = [req.resources[0].resource]
+    let paramCount = 2
+    const clauses = []
+    
+    if (params.issue_type) {
+      clauses.push(`AND i.issue_type = $${paramCount++}`)
+      values.push(params.issue_type)
+    }
+    if (params.issue_field) {
+      clauses.push(`AND field = $${paramCount++}`)
+      values.push(params.issue_field)
+    }
+    values.push(req.params.dataset)
+    
+    return {
+      text: `
         select i.issue_type, field, entity, message, severity, value
         from issue i
         LEFT JOIN issue_type it ON i.issue_type = it.issue_type
-        WHERE resource = '${req.resources[0].resource}'
-        ${issueTypeClause}
+        WHERE resource = $1
+        ${clauses.join(' ')}
         AND it.responsibility = 'external'
         AND it.severity = 'error'
-        ${issueFieldClause}
-        AND i.dataset = '${req.params.dataset}'
+        AND i.dataset = $${paramCount}
         AND entity != ''
-        `
+        `,
+      values
+    }

Likely invalid or redundant comment.


527-543: ⚠️ Potential issue

Fix SQL injection and string concatenation in fetchEntityIssueCounts.

The query has two issues:

  1. SQL injection vulnerabilities
  2. Incorrect string concatenation in COUNT
   query: ({ req }) => {
-    const datasetClause = req.params.dataset ? `AND i.dataset = '${req.params.dataset}'` : ''
+    const values = [req.resources.map(resource => resource.resource)]
+    let paramCount = 2
+    const clauses = []
+    
+    if (req.params.dataset) {
+      clauses.push(`AND i.dataset = $${paramCount++}`)
+      values.push(req.params.dataset)
+    }
+    
+    return {
+      text: `
-        select dataset, field, i.issue_type, COUNT(resource+line_number) as count
+        select dataset, field, i.issue_type, COUNT(resource || line_number) as count
         from issue i
         LEFT JOIN issue_type it ON i.issue_type = it.issue_type
-        WHERE resource in ('${req.resources.map(resource => resource.resource).join("', '")}')
+        WHERE resource = ANY($1::text[])
         AND (entity != '' AND entity IS NOT NULL)
         AND it.responsibility = 'external'
         AND it.severity = 'error'
-        ${datasetClause}
+        ${clauses.join(' ')}
         GROUP BY field, i.issue_type, dataset
-    `
+      `,
+      values
+    }

Likely invalid or redundant comment.


556-577: ⚠️ Potential issue

Fix SQL injection and string concatenation in fetchEntryIssueCounts.

The query has two issues:

  1. SQL injection vulnerabilities
  2. Incorrect string concatenation in COUNT
   query: ({ req }) => {
-    const datasetClause = req.params.dataset ? `AND i.dataset = '${req.params.dataset}'` : ''
+    const values = []
+    let paramCount = 1
+    const clauses = []
+    
+    if (req.params.dataset) {
+      clauses.push(`AND i.dataset = $${paramCount++}`)
+      values.push(req.params.dataset)
+    }

     const mostRecentResources = getMostRecentResources(req.resources)
     const resourceIds = Object.values(mostRecentResources).map(resource => resource.resource)
+    values.push(resourceIds)

-    return `
+    return {
+      text: `
-        select dataset, field, i.issue_type, COUNT(resource + line_number) as count
+        select dataset, field, i.issue_type, COUNT(resource || line_number) as count
         from issue i
         LEFT JOIN issue_type it ON i.issue_type = it.issue_type
-        WHERE resource in ('${resourceIds.join("', '")}')
+        WHERE resource = ANY($${paramCount}::text[])
         AND (entity = '' OR entity is NULL)
         AND it.responsibility = 'external'
         AND it.severity = 'error'
-        ${datasetClause}
+        ${clauses.join(' ')}
         GROUP BY field, i.issue_type, dataset
-    `
+      `,
+      values
+    }

Likely invalid or redundant comment.

test/unit/middleware/common.middleware.test.js (1)

Line range hint 1157-1228: Well-structured tests with comprehensive coverage!

The tests:

  • Cover the parameter rename from entityCount to rowCount
  • Include edge cases
  • Validate the integration with performanceDbApi

Comment on lines +507 to +522
query: ({ req, params }) => {
const issueTypeClause = params.issue_type ? `AND i.issue_type = '${params.issue_type}'` : ''
const issueFieldClause = params.issue_field ? `AND field = '${params.issue_field}'` : ''
return `
select i.issue_type, field, entity, message, severity, value, line_number
from issue i
LEFT JOIN issue_type it ON i.issue_type = it.issue_type
WHERE resource = '${req.resources[0].resource}'
${issueTypeClause}
AND it.responsibility = 'external'
AND it.severity = 'error'
AND i.dataset = '${req.params.dataset}'
${issueFieldClause}
AND (entity = '' OR entity is NULL OR i.issue_type in ('${entryIssueGroups.map(issue => issue.type).join("', '")}'))
LIMIT ${req.dataRange.pageLength} OFFSET ${req.dataRange.offset}
`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix SQL injection vulnerabilities in fetchEntryIssues.

The query is vulnerable to SQL injection in multiple places.

-    const issueTypeClause = params.issue_type ? `AND i.issue_type = '${params.issue_type}'` : ''
-    const issueFieldClause = params.issue_field ? `AND field = '${params.issue_field}'` : ''
+    const values = [req.resources[0].resource]
+    let paramCount = 2
+    const clauses = []
+    
+    if (params.issue_type) {
+      clauses.push(`AND i.issue_type = $${paramCount++}`)
+      values.push(params.issue_type)
+    }
+    if (params.issue_field) {
+      clauses.push(`AND field = $${paramCount++}`)
+      values.push(params.issue_field)
+    }
+    values.push(req.params.dataset)
+    values.push(entryIssueGroups.map(issue => issue.type))
+    values.push(req.dataRange.pageLength)
+    values.push(req.dataRange.offset)
+    
+    return {
+      text: `
         select i.issue_type, field, entity, message, severity, value, line_number
         from issue i
         LEFT JOIN issue_type it ON i.issue_type = it.issue_type
-        WHERE resource = '${req.resources[0].resource}'
-        ${issueTypeClause}
+        WHERE resource = $1
+        ${clauses.join(' ')}
         AND it.responsibility = 'external'
         AND it.severity = 'error'
-        AND i.dataset = '${req.params.dataset}'
-        ${issueFieldClause}
-        AND (entity = '' OR entity is NULL OR i.issue_type in ('${entryIssueGroups.map(issue => issue.type).join("', '")}'))
-        LIMIT ${req.dataRange.pageLength} OFFSET ${req.dataRange.offset}
+        AND i.dataset = $${paramCount++}
+        AND (entity = '' OR entity is NULL OR i.issue_type = ANY($${paramCount++}::text[]))
+        LIMIT $${paramCount++} OFFSET $${paramCount}
+        `,
+      values
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
query: ({ req, params }) => {
const issueTypeClause = params.issue_type ? `AND i.issue_type = '${params.issue_type}'` : ''
const issueFieldClause = params.issue_field ? `AND field = '${params.issue_field}'` : ''
return `
select i.issue_type, field, entity, message, severity, value, line_number
from issue i
LEFT JOIN issue_type it ON i.issue_type = it.issue_type
WHERE resource = '${req.resources[0].resource}'
${issueTypeClause}
AND it.responsibility = 'external'
AND it.severity = 'error'
AND i.dataset = '${req.params.dataset}'
${issueFieldClause}
AND (entity = '' OR entity is NULL OR i.issue_type in ('${entryIssueGroups.map(issue => issue.type).join("', '")}'))
LIMIT ${req.dataRange.pageLength} OFFSET ${req.dataRange.offset}
`
query: ({ req, params }) => {
const values = [req.resources[0].resource]
let paramCount = 2
const clauses = []
if (params.issue_type) {
clauses.push(`AND i.issue_type = $${paramCount++}`)
values.push(params.issue_type)
}
if (params.issue_field) {
clauses.push(`AND field = $${paramCount++}`)
values.push(params.issue_field)
}
values.push(req.params.dataset)
values.push(entryIssueGroups.map(issue => issue.type))
values.push(req.dataRange.pageLength)
values.push(req.dataRange.offset)
return {
text: `
select i.issue_type, field, entity, message, severity, value, line_number
from issue i
LEFT JOIN issue_type it ON i.issue_type = it.issue_type
WHERE resource = $1
${clauses.join(' ')}
AND it.responsibility = 'external'
AND it.severity = 'error'
AND i.dataset = $${paramCount++}
AND (entity = '' OR entity is NULL OR i.issue_type = ANY($${paramCount++}::text[]))
LIMIT $${paramCount++} OFFSET $${paramCount}
`,
values
}

@eveleighoj eveleighoj temporarily deployed to submit-682-dataset-task-9ukvwn January 16, 2025 09:50 Inactive
@GeorgeGoodall-GovUk GeorgeGoodall-GovUk merged commit 817e6c1 into main Jan 16, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dataset task list: Fetch data from all active resources
5 participants