Repository navigation
Expand file tree
/
Copy pathvalidate.go
More file actions
62 lines (57 loc) · 1.73 KB
/
Copy pathvalidate.go
File metadata and controls
62 lines (57 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pdfast55
import (
"context"
"fmt"
"os"
"github.com/hazyhaar/pdfast/internal/security"
)
// SecurityReport is the Validate output.
type SecurityReport struct {
Flags []string
}
// Validate reports structural and active-content flags for an already opened Doc.
func Validate(ctx context.Context, d *Doc) (SecurityReport, error) {
select {
case <-ctx.Done():
return SecurityReport{}, ctx.Err()
default:
}
if d == nil || d.inner == nil {
return SecurityReport{}, fmt.Errorf("pdfast55: nil document")
}
rep := d.inner.Security
flags := append([]string{}, rep.Flags...)
if len(flags) == 0 {
flags = append(flags, rep.StructuralFlags...)
flags = append(flags, rep.DangerousActions...)
flags = append(flags, rep.Obliterated...)
}
return SecurityReport{Flags: flags}, nil
}
// ValidateFile opens with active content allowed and returns the same flag set as Open
// (full-file dangerous scan), without failing on active content.
func ValidateFile(ctx context.Context, path string) (SecurityReport, error) {
select {
case <-ctx.Done():
return SecurityReport{}, ctx.Err()
default:
}
raw, err := os.ReadFile(path)
if err != nil {
return SecurityReport{}, err
}
// Structural + dangerous on full bytes (aligned with Open)
limits := security.DefaultLimits()
st := security.ScanStructural(raw, limits)
danger := security.ScanDangerousActions(raw)
soft := security.ScanSoftFlags(raw)
flags := append([]string{}, st.Flags...)
flags = append(flags, danger...)
flags = append(flags, soft...)
// Also open to surface encrypt / parse errors
_, err = Open(bytesReaderAt(raw), int64(len(raw)), WithAllowActiveContent(true))
if err != nil {
return SecurityReport{Flags: flags}, err
}
return SecurityReport{Flags: flags}, nil
}