Repository navigation
[AI-4157] Refuse Word and OpenDocument XML nested too deep #2706
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5dae1dc
AI-4157: refuse a Word or OpenDocument part nested past the XML depth…
kanat 89d59d9
AI-4157: keep each format's wording when a part is nested too deep
kanat 4faa184
AI-4157: reword four comments that named only workbooks or only Word …
kanat c8609bb
AI-4157: pin that a broken Word or OpenDocument part stays corrupt
kanat 7c64389
AI-4157: keep the depth error, and test the OpenDocument body reader'…
kanat 5973741
AI-4157: word a too-large refusal of a file sent with a question by i…
kanat 874f1fc
AI-4157: pin the page-limit wording for a Word file over the page limit
kanat 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
This file contains hidden or 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 hidden or 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,85 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "archive/zip" | ||
| "bytes" | ||
| "context" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/suite" | ||
|
|
||
| "github.com/GetStream/Vision-Agents/acceleration/internal/knowledge/extract" | ||
| ) | ||
|
|
||
| // DocumentFailureSuite covers how a refused file is answered to the person who sent it. | ||
| type DocumentFailureSuite struct { | ||
| suite.Suite | ||
| } | ||
|
|
||
| func TestDocumentFailureSuite(t *testing.T) { | ||
| suite.Run(t, new(DocumentFailureSuite)) | ||
| } | ||
|
|
||
| // Defect: every too-large refusal said the file had too many pages, so a Word file refused for its | ||
| // nesting told the sender to cut pages that were not the problem. | ||
| func (s *DocumentFailureSuite) TestAFileRefusedForItsNestingIsNotToldItHasTooManyPages() { | ||
| var archive bytes.Buffer | ||
| writer := zip.NewWriter(&archive) | ||
| part, err := writer.Create("word/document.xml") | ||
| s.Require().NoError(err) | ||
| _, err = part.Write([]byte(`<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>` + | ||
| strings.Repeat("<x>", 1<<12) + strings.Repeat("</x>", 1<<12) + `</w:body></w:document>`)) | ||
| s.Require().NoError(err) | ||
| s.Require().NoError(writer.Close()) | ||
| _, err = NewDocumentReader(extract.Options{}).Extract(context.Background(), "a.docx", archive.Bytes()) | ||
| s.Require().Error(err) | ||
|
|
||
| failure := documentFailure(err) | ||
|
|
||
| var refused APIError | ||
| s.Require().ErrorAs(failure, &refused) | ||
| s.Equal(codeDocumentTooLarge, refused.Code) | ||
| s.Contains(refused.Message, "nested") | ||
| s.NotContains(refused.Message, "pages") | ||
| } | ||
|
|
||
| func (s *DocumentFailureSuite) TestAFileOverThePageLimitIsToldHowManyPagesAreRead() { | ||
| data, err := os.ReadFile("../knowledge/extract/testdata/manual.pdf") | ||
| s.Require().NoError(err) | ||
| _, err = extract.New(extract.Options{MaxPages: 1}).Extract(context.Background(), "manual.pdf", data) | ||
| s.Require().Error(err) | ||
|
|
||
| failure := documentFailure(err) | ||
|
|
||
| var refused APIError | ||
| s.Require().ErrorAs(failure, &refused) | ||
| s.Equal(codeDocumentTooLarge, refused.Code) | ||
| s.Contains(refused.Message, "(at most 100 pages)") | ||
| } | ||
|
|
||
| // Defect: a Word file with more pages than the limit was refused by the generic too-large wording, | ||
| // so the sender was not told how many pages are read. | ||
| func (s *DocumentFailureSuite) TestAWordFileOverThePageLimitIsToldHowManyPagesAreRead() { | ||
| var archive bytes.Buffer | ||
| writer := zip.NewWriter(&archive) | ||
| part, err := writer.Create("word/document.xml") | ||
| s.Require().NoError(err) | ||
| _, err = part.Write([]byte(`<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>` + | ||
| `<w:p><w:r><w:t>one</w:t></w:r></w:p>` + | ||
| `<w:p><w:r><w:lastRenderedPageBreak/><w:t>two</w:t></w:r></w:p>` + | ||
| `<w:p><w:r><w:lastRenderedPageBreak/><w:t>three</w:t></w:r></w:p>` + | ||
| `</w:body></w:document>`)) | ||
| s.Require().NoError(err) | ||
| s.Require().NoError(writer.Close()) | ||
| _, err = extract.New(extract.Options{MaxPages: 1}).Extract(context.Background(), "a.docx", archive.Bytes()) | ||
| s.Require().Error(err) | ||
|
|
||
| failure := documentFailure(err) | ||
|
|
||
| var refused APIError | ||
| s.Require().ErrorAs(failure, &refused) | ||
| s.Equal(codeDocumentTooLarge, refused.Code) | ||
| s.Contains(refused.Message, "(at most 100 pages)") | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.