Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
POS/src/print-document-store.js at develop · Posnic/POS · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Posnic
POS
Repository navigation
Code
Issues
396
(396)
Pull requests
14
(14)
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
POS
/
src
/
print-document-store.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
91 lines (78 loc) · 2.87 KB
develop
Breadcrumbs
POS
/
src
/
print-document-store.js
Copy path
Top
File metadata and controls
Code
Blame
91 lines (78 loc) · 2.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Handing a print document to the local API so it can be served from a real
* origin instead of a data: URL.
*
* The receipt printer loads its HTML as `data:text/html;...`, which has an
* opaque origin. The invoice markup links print.css and the shop's logo from
* the local API, and an opaque origin may not load either - so that window has
* always run with webSecurity disabled. It works, and it has been flagged as a
* finding every time anyone audits the repository, correctly.
*
* The fix is not to turn the flag on. It is to stop needing it: serve the
* document from `http://127.0.0.1:<port>/print/<token>` so the page, the
* stylesheet and the logo share one origin. Then web security can be on and
* nothing is cross-origin at all.
*
* Documents live in memory only. Writing a customer's invoice to a temporary
* file to print it would leave it on disk after the job, which is a worse
* trade than the one being fixed.
*
* Each token is single-use and short-lived. A print document is fetched once,
* immediately, by a window this process just opened - so anything else asking
* for it is not the print window, and there is nothing left to give.
*/
const
crypto
=
require
(
'crypto'
)
;
/* Long enough that guessing is not a strategy, on a loopback-only endpoint
that holds each document for seconds. */
const
TOKEN_BYTES
=
24
;
/* If a window fails to open, or the print is cancelled before the page loads,
nothing collects the document. It must not sit in memory until the till is
next restarted. */
const
TTL_MS
=
60
*
1000
;
const
documents
=
new
Map
(
)
;
function
sweep
(
now
=
Date
.
now
(
)
)
{
for
(
const
[
token
,
entry
]
of
documents
)
{
if
(
entry
.
expiresAt
<=
now
)
documents
.
delete
(
token
)
;
}
}
/**
* Store a document and return the token that fetches it exactly once.
*
*
@param
{
string
} html a complete HTML document
*
@returns
{
string
} the token to put in the URL
*/
function
put
(
html
)
{
if
(
typeof
html
!==
'string'
||
!
html
)
{
throw
new
TypeError
(
'a print document must be a non-empty string'
)
;
}
sweep
(
)
;
const
token
=
crypto
.
randomBytes
(
TOKEN_BYTES
)
.
toString
(
'hex'
)
;
documents
.
set
(
token
,
{
html
,
expiresAt
:
Date
.
now
(
)
+
TTL_MS
}
)
;
return
token
;
}
/**
* Fetch and forget.
*
* Single use: the second request for a token gets nothing, whoever makes it.
*
*
@param
{
string
} token
*
@returns
{
string|null
} the document, or null if it is unknown, spent or expired
*/
function
take
(
token
)
{
sweep
(
)
;
if
(
typeof
token
!==
'string'
)
return
null
;
const
entry
=
documents
.
get
(
token
)
;
if
(
!
entry
)
return
null
;
documents
.
delete
(
token
)
;
return
entry
.
html
;
}
/** How many documents are waiting. For diagnostics and tests. */
function
size
(
)
{
sweep
(
)
;
return
documents
.
size
;
}
/** Drop everything. Used on shutdown so nothing outlives the process. */
function
clear
(
)
{
documents
.
clear
(
)
;
}
module
.
exports
=
{
put
,
take
,
size
,
clear
,
TTL_MS
}
;
You can’t perform that action at this time.