-
Notifications
You must be signed in to change notification settings - Fork 8
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
Fix header normalization for xlsx #612
Open
hypermurea
wants to merge
1
commit into
main
Choose a base branch
from
fix/xlsx-extractor-header-normalization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
129 changes: 129 additions & 0 deletions
129
plugins/xlsx-extractor/src/header.normalization.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import api from '@flatfile/api' | ||
import { | ||
setupListener, | ||
setupSpace, | ||
getEnvironmentId, | ||
} from '@flatfile/utils-testing' | ||
import { ExcelExtractor } from '.' | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { FlatfileEvent } from '@flatfile/listener' | ||
|
||
describe('xlsx-extractor plugin', () => { | ||
|
||
const listener = setupListener() | ||
let spaceId: string | ||
|
||
beforeAll(async () => { | ||
const space = await setupSpace() | ||
spaceId = space.id | ||
}) | ||
afterAll(async () => { | ||
await api.spaces.delete(spaceId) | ||
}) | ||
|
||
beforeEach(async () => { | ||
listener.use(ExcelExtractor()) | ||
}) | ||
|
||
it('Upload file with headers that require normalization', async () => { | ||
|
||
listener.on("**", async (event: FlatfileEvent) => { | ||
console.log(event.topic) | ||
}) | ||
|
||
await api.files.upload(fs.createReadStream(path.join(__dirname,'../ref/test-headers.xlsx')), { | ||
environmentId: getEnvironmentId(), | ||
spaceId, | ||
}) | ||
|
||
const failure = async () => { | ||
await listener.waitFor("job:failed", 1) | ||
return false | ||
} | ||
const success = async () => { | ||
await listener.waitFor("sheet:counts-updated", 3) | ||
return true | ||
} | ||
|
||
const ok = await Promise.race([failure(), success()]) | ||
if(!ok) { | ||
throw new Error("Job should not fail") | ||
} else { | ||
const { data: workbooks } = await api.workbooks.list({spaceId}) | ||
expect(workbooks.length).toBe(1) | ||
const { data: sheets } = await api.sheets.list({workbookId: workbooks[0].id}) | ||
expect(sheets.length).toBe(1) | ||
const EXPECTED_FIELDS = [{ | ||
"description": "", | ||
"key": "Code", | ||
"label": "Code", | ||
"type": "string", | ||
}, | ||
{ | ||
"description": "", | ||
"key": "Amount_DOLLAR_", | ||
"label": "Amount_DOLLAR_", | ||
"type": "string", | ||
}, | ||
{ | ||
"description": "", | ||
"key": "Amount_DOLLAR__1", | ||
"label": "Amount_DOLLAR__1", | ||
"type": "string", | ||
}, | ||
{ | ||
"description": "", | ||
"key": "Rate_PERCENT_", | ||
"label": "Rate_PERCENT_", | ||
"type": "string", | ||
}, | ||
{ | ||
"description": "", | ||
"key": "empty", | ||
"label": "empty", | ||
"type": "string", | ||
}, | ||
{ | ||
"description": "", | ||
"key": "empty_1", | ||
"label": "empty_1", | ||
"type": "string", | ||
}] | ||
|
||
expect(sheets[0].config.fields).toEqual(EXPECTED_FIELDS) | ||
|
||
const { data: { records } } = await api.records.get(sheets[0].id) | ||
expect(records.length).toBe(2) | ||
const data = records.map((record) => | ||
EXPECTED_FIELDS.reduce((acc, field) => { | ||
acc[field.key] = record.values[field.key].value | ||
return acc | ||
}, {}) | ||
) | ||
expect(data).toEqual([ | ||
{ | ||
"Amount_DOLLAR_": "100", | ||
"Amount_DOLLAR__1": "300", | ||
"Code": "ABC", | ||
"Rate_PERCENT_": "5%", | ||
"empty": undefined, | ||
"empty_1": undefined, | ||
}, | ||
{ | ||
"Amount_DOLLAR_": "200", | ||
"Amount_DOLLAR__1": "400", | ||
"Code": "DEF", | ||
"Rate_PERCENT_": "3%", | ||
"empty": undefined, | ||
"empty_1": undefined, | ||
}, | ||
]) | ||
|
||
} | ||
|
||
}) | ||
|
||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nullify Code
Language: TypeScript
🔵 MEDIUM Severity
CWE-22
Javascript pathtraversal rule non literal fs filename
The application dynamically constructs file or path information. If the path
information comes from user-supplied input, it could be abused to read sensitive files,
access other users' data, or aid in exploitation to gain further system access.
User input should never be used in constructing paths or files for interacting
with the filesystem. This includes filenames supplied by user uploads or downloads.
If possible, consider hashing user input or using unique values and
use
path.normalize
to resolve and validate the path informationprior to processing any file functionality.
Example using
path.normalize
and not allowing direct user input:For more information on path traversal issues see OWASP:
https://owasp.org/www-community/attacks/Path_Traversal
⚡ Here's how you might fix this potential vulnerability
The modified code first constructs the full path of the file, then uses 'path.basename()' to get the filename. This ensures that only the filename is used in the filesystem operation, preventing directory traversal. Even though '__dirname' is not controllable by an attacker, it's a good practice to always validate or sanitize filenames in filesystem operations.
autoFixesExperimental
Use path.basename() to get the filename and prevent directory traversal
poweredByNullify
Reply with
/nullify
to interact with me like another developer(you will need to refresh the page for updates)